spring和redis集成
2017-01-15 21:35
363 查看
spring配置文件
spring-reids配置文件
测试类
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 使用Spring组件扫描的方式来实现自动注入bean --> <!-- <context:component-scan base-package="com.zyc"/> --> <context:property-placeholder location="classpath:redis.properties" /> <!-- 隐式地向 Spring 容器注册 --> <context:annotation-config/> <import resource="spring-redis.xml"/> </beans>
redis.properties 配置文件如下
#redis\u4E2D\u5FC3 redis.host=127.0.0.1 redis.port=6379 redis.password=123456 redis.maxIdle=100 redis.maxActive=300 redis.maxWait=1000 redis.testOnBorrow=true redis.timeout=100000 # \u4E0D\u9700\u8981\u52A0\u5165\u7F13\u5B58\u7684\u7C7B targetNames=xxxRecordManager,xxxSetRecordManager,xxxStatisticsIdentificationManager # \u4E0D\u9700\u8981\u7F13\u5B58\u7684\u65B9\u6CD5 methodNames= #\u8BBE\u7F6E\u7F13\u5B58\u5931\u6548\u65F6\u95F4 com.service.impl.xxxRecordManager= 60 com.service.impl.xxxSetRecordManager= 60 defaultCacheExpireTime=3600 fep.local.cache.capacity =10000
spring-reids配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean > <!-- redis服务器中心 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="poolConfig" ref="poolConfig" /> <property name="port" value="${redis.port}" /> <property name="hostName" value="${redis.host}" /> <!-- <property name="password" value="${redis.password}" /> --> <property name="timeout" value="${redis.timeout}" ></property> </bean > <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="connectionFactory" /> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean > <!-- cache配置 --> <bean id="redisUtil" class="com.zyc.util.RedisUtil" > <property name="redisTemplate" ref="redisTemplate" /> </bean > </beans>com.zyc.util.RedisUtil 工具类
package com.zyc.util; import java.io.Serializable; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; public class RedisUtil { private Logger logger = Logger.getLogger(RedisUtil.class); private RedisTemplate<Serializable, Object> redisTemplate; /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 获取所有的key * @return */ public Set<Serializable> keys() { return redisTemplate.keys("*"); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); result = operations.get(key); return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public void setRedisTemplate( RedisTemplate<Serializable, Object> redisTemplate) { this.redisTemplate = redisTemplate; } }
测试类
package com.zyc; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyc.service.Data1; import com.zyc.util.RedisUtil; public class MainRedisClass { public static Logger logger=Logger.getLogger(MainRedisClass.class); public static void main(String[] args) { // TODO Auto-generated method stub PropertyConfigurator.configure("C:/Users/zhao/Desktop/log4j.properties");//这里配置自己的log4j 也可不用注释了也可以 ApplicationContext context = new ClassPathXmlApplicationContext("spring-applicationContext.xml");// logger.info("ke yi yong log4j le"); RedisUtil redisUtil= (RedisUtil) context.getBean("redisUtil"); redisUtil.set("abc", "123"); } }
相关文章推荐
- Spring Boot系列教程十二:Spring boot集成Redis
- Redis 源码解析 string内部实现原理之链表
- Redis 源码解析 string内部实现原理之简单动态字符串SDS
- Redis入门很简单之六【Jedis常见操作】
- Redis的安装和部署
- centos7 redis3.2安装
- python redis list操作
- redis的五种数据类型---Sorted Set类型
- Python—redis
- Redis性能问题排查解决手册
- Redis3.2.6 单节点安装
- 网页闹钟
- Ubuntu安装redis并配置远程、密码以及开启php扩展
- redisManager逻辑
- mybatis结合redis实战二级缓存(六)
- MongoDB Redis
- redis相关操作(持续更新中)
- 2种基于简单定时任务实现分布式定时任务的技术解决方案(zookeeper、redis和内联MQ)
- 大数据技术--Redis
- redis 入门级操作----demo