Spring3.1 Cache注解
依赖jar包:
<!-- redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.4.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.2</version> </dependency>
applicationContext-cache-redis.xml
<context:property-placeholder location="classpath:/config/properties/redis.properties" /> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager" /> <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.cpframework.cache.redis.RedisCache"> <property name="redisTemplate" ref="redisTemplate" /> <property name="name" value="default"/> </bean> <bean class="org.cpframework.cache.redis.RedisCache"> <property name="redisTemplate" ref="redisTemplate02" /> <property name="name" value="commonCache"/> </bean> </set> </property> </bean> <!-- redis 相关配置 --> <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> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig" p:database="${redis.database}" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="connectionFactory02" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig" p:database="${redis.database}" /> <bean id="redisTemplate02" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory02" /> </bean>
redis.properties
# Redis settings # server IP redis.host=192.168.xx.xx # server port redis.port=6379 # use dbIndex redis.database=0 # 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 redis.maxIdle=300 # 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException; redis.maxWait=3000 # 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 redis.testOnBorrow=true
RedisCache.java
package org.cpframework.cache.redis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; public class RedisCache implements Cache { private RedisTemplate<String, Object> redisTemplate; private String name; public RedisTemplate<String, Object> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void setName(String name) { this.name = name; } @Override public String getName() { // TODO Auto-generated method stub return this.name; } @Override public Object getNativeCache() { // TODO Auto-generated method stub return this.redisTemplate; } @Override public ValueWrapper get(Object key) { // TODO Auto-generated method stub final String keyf = (String) key; Object object = null; object = redisTemplate.execute(new RedisCallback<Object>() { public Object doInRedis(RedisConnection connection) throws DataAccessException { byte[] key = keyf.getBytes(); byte[] value = connection.get(key); if (value == null) { return null; } return toObject(value); } }); return (object != null ? new SimpleValueWrapper(object) : null); } @Override public void put(Object key, Object value) { // TODO Auto-generated method stub final String keyf = (String) key; final Object valuef = value; final long liveTime = 86400; redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { byte[] keyb = keyf.getBytes(); byte[] valueb = toByteArray(valuef); connection.set(keyb, valueb); if (liveTime > 0) { connection.expire(keyb, liveTime); } return 1L; } }); } /** * 描述 : <Object转byte[]>. <br> * <p> * <使用方法说明> * </p> * * @param obj * @return */ private byte[] toByteArray(Object obj) { byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException ex) { ex.printStackTrace(); } return bytes; } /** * 描述 : <byte[]转Object>. <br> * <p> * <使用方法说明> * </p> * * @param bytes * @return */ private Object toObject(byte[] bytes) { Object obj = null; try { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); obj = ois.readObject(); ois.close(); bis.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } return obj; } @Override public void evict(Object key) { // TODO Auto-generated method stub final String keyf = (String) key; redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { return connection.del(keyf.getBytes()); } }); } @Override public void clear() { // TODO Auto-generated method stub redisTemplate.execute(new RedisCallback<String>() { public String doInRedis(RedisConnection connection) throws DataAccessException { connection.flushDb(); return "ok"; } }); } }
相关推荐
之前整合springcache+redis的时候到网上搜了很多代码方案,但是注解都不起作用,搞了很长时间,也是很郁闷。所以,把成果分享一下,避免更多人踩坑,哈哈
本篇文章主要介绍了详解配置Spring4.0注解Cache+Redis缓存的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
博客http://blog.csdn.net/poorcoder_/article/details/59541710的代码。主要描述spring通过注解整合redis用作缓存的实例。
redis-cluster完整项目,下载导入eclipse即可
使用spring集成redis基于注解的方式来实现数据的缓存
Springboot中Spring-cache与redis整合。这也是一个不错的框架,与spring的事务使用类似,只要添加一些注解方法,就可以动态的去操作缓存了,减少代码的操作。
SpringBoot2.0整合SpringCache和Redis 1 1. SpringBoot对应(自带)RedisClient是不同的 1 2. 实战 1 2.1. maven配置 1 2.2. yml配置 1 2.3. RedisConfig配置 1 2.4. RedisController 2 3. Spring缓存注解@...
springboot1.x基于spring注解实现J2Cache两级缓存集成,一级缓存:ehcache,二级缓存:redis,代码可直接运行(根据项目中的README.md提示)
spring boot —— redis 缓存注解使用教程 示例项目地址: 依赖 在pom文件添加如下依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis 配置 在application.yml配置文件添加...
3.剖析SpringCache常用注解 4.总结springcache在开发中遇到的大坑小坑 微信文章的阅读量 1.微信文章的阅读量PV场景介绍 2.高并发的微信文章的阅读量PV技术原理 3.案例实战:SpringBoot+Redis实现微信文章的PV统计 ...
Spring+Redis实现无缝布拉格插件(com.shawntime.utils.rwdb) Redis操作封装(com.shawntime.utils.cache.redis) Redis环球锁实现(com.shawntime.utils.lock) 读写锁控制强制读取存储同步 线程池管理类 配置文件...
基于Spring cache 封装,可以使用spring 注解 @Cacheable 支持Spring 4(guava) 和Spring 5(caffeine) 二级缓存都使用redis 相关配置 注解支持 /** redis过期时间 */ redisExpires; /** 访问后过期时间,...
JetCache提供了比SpringCache更强大的注解,可以原生的支持TTL、两级缓存、分布式自动刷新,提供了Cache接口用于手工缓存操作。当前有四个实现,RedisCache、TairCache、CaffeineCache和一个简易的...
avaEE开发的颠覆者 Spring Boot实战 完整版+书签+pdf+源码.rar JavaEE开发的颠覆者 Spring Boot实战 完整版+书签+pdf+源码.rar JavaEE开发的颠覆者 Spring Boot实战 完整版+书签+pdf+源码.rar 产品参数: 定价: ...
# demoWeb 一个基于SpringMVC的web框架 1.0.5 从web项目迁移成maven项目 ...Spring Cache增加redis缓存实现 Mybatis使用二级缓存,增加redis实现 增加reactJs 增加Mybatis插件pageHelper,Mapper doc内有相关文档
1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 Spring Boot的启动器Starter详解 1.38 Spring Boot集成Redis实现缓存机制 1.39 Spring Boot Cache理论篇 1.40 Spring ...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...