spring中使用redis做缓存
2017-01-13 19:36
218 查看
pom.xml
<!--redis配置--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>spring配置
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxIdle" value="${redis.maxIdle}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.hostName}" p:poolConfig-ref="poolConfig" p:timeout="${redis.timeout}" p:usePool="true" p:port="${redis.port}" > </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> <!--缓存的操作类--> <bean id="redisUtil" class="com.groupds.log.RedisUtil" > <property name="redisTemplate" ref="redisTemplate" /></[b]bean>redis.properties[/b]
##applicationContext-redis.xml redis.hostName=192.168.0.111 redis.port=6379 redis.timeout=1000 redis.maxIdle=100 redis.minIdle=20 redis.maxTotal=300 redis.maxWaitMillis=1000 redis.testOnBorrow=true #缓存存活时间 redis.time=500
RedisUtil
public class RedisUtil { private Logger logger = Logger.getLogger(RedisUtil.class); private RedisTemplate<Serializable, Object> redisTemplate; /** * 批量删除对应的value * * @param keys */ public void remove( String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern( String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove( String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get( 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( 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( 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; } }
本人在使用redis做缓存是通过aop的环绕通知来实现的,通过对指定的方法进行拦截,判断缓存中是否有值,有的话就读取缓存,没有就查询数据库中的数据,再放入缓存中。本人使用的key是以方法名和参数组合成的,
我个人认为redis可以做为缓存其实是因为它可以设定数据存活的时间,因为它本身也是一个数据库。
在使用环绕通知时使用到redisUtil时最好在spring.xml中赋值,不然在调用redisUtil类时可能会出现空指针异常。
相关文章推荐
- Redis整合Spring结合使用缓存实例
- Redis整合Spring结合使用缓存实例
- 分布式缓存技术redis学习系列(六)—— 深入理解Spring Redis的使用
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(六)Spring中Redis的缓存的使用
- Redis整合Spring结合使用缓存实例
- Redis整合Spring结合使用缓存实例
- Spring Boot使用redis做数据缓存
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Redis整合Spring结合使用缓存实例
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Redis学习总结(3)——Redis整合Spring结合使用缓存实例
- Redis整合Spring结合使用缓存实例(三)
- Redis整合Spring结合使用缓存实例
- Spring客户端对Redis 缓存的使用(Java客户端等Redis3.X RedisCluster模式的支持)
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- Redis学习总结(3)——Redis整合Spring结合使用缓存实例
- Spring Boot使用redis做数据缓存
- Redis整合Spring结合使用缓存实例(转)
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- 使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署