spring中使用redis做缓存
来源:互联网 发布:王千源事件 知乎 编辑:程序博客网 时间:2024/06/14 09:11
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" /></bean >redis.properties
##applicationContext-redis.xmlredis.hostName=192.168.0.111redis.port=6379redis.timeout=1000redis.maxIdle=100redis.minIdle=20redis.maxTotal=300redis.maxWaitMillis=1000redis.testOnBorrow=true#缓存存活时间redis.time=500RedisUtilpublic 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类时可能会出现空指针异常。
0 0
- spring中使用redis做缓存
- Spring Boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- spring cache使用redis做缓存
- Spring Boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- spring boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- 在 Django 中使用 Redis 做缓存
- Java中如何使用Redis做缓存
- spring+redis做数据缓存
- spring aop 做redis缓存
- 3. Spring Boot 1.2.5使用redis做数据缓存
- Spring Boot 1.2.5使用redis做数据缓存
- spring boot 使用redis做缓存学习记录
- Spring+redis缓存使用
- Spark快速大数据分析 第4章
- 洛谷1115 最大子段和
- IAR编译信息-size解读
- 京东消息中间件JMQ
- 关于Nessus的服务启动与插件更新
- spring中使用redis做缓存
- 约瑟夫问题
- spark本地模式和Standalone配置
- HTML每日一结1.13
- 快速排序
- 十进制小数分数与二进制的转换
- 初探HTTPS协议
- PAT-甲级-1015
- mybatis 一对多:用户与角色