redis与spring的完全集成

下载spring-data-redis,gav如下:

[html]  view plain copy print ?
 
  1.                 <dependency>  
  2.     <groupId>org.springframework.data</groupId>  
  3.     <artifactId>spring-data-redis</artifactId>  
  4.     <version>1.0.1.RELEASE</version>  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <groupId>org.slf4j</groupId>  
  8.             <artifactId>slf4j-log4j12</artifactId>  
  9.         </exclusion>  
  10.         <exclusion>  
  11.             <groupId>org.slf4j</groupId>  
  12.             <artifactId>jcl-over-slf4j</artifactId>  
  13.         </exclusion>  
  14.     </exclusions>  
  15.   
  16. </dependency>  


其中exclusion了两个包,原因是与项目里其它包冲突。

bean配置如下,可在web.xml里配置加载bean文件:

[html]  view plain copy print ?
 
  1. <bean id="redisCacheManager" class="com.cr.common.cache.base.RedisCacheManger">  
  2.     <property name="pool" ref="shardedJedisPool"/>  
  3. </bean>  
  4. <!-- jedis 连接池配置-->  
  5. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">    
  6.     <property name="maxActive"     value="${redis.pool.maxActive}" />    
  7.     <property name="maxIdle"       value="${redis.pool.maxIdle}" />    
  8.     <property name="maxWait"       value="${redis.pool.maxWait}" />    
  9.     <property name="testOnBorrow"  value="${redis.pool.testOnBorrow}" />    
  10. </bean>    
  11. <!-- jedis 多个服务器配置-->  
  12. <bean id="jedisShardInfo1" class="redis.clients.jedis.JedisShardInfo">    
  13.     <constructor-arg index="0" value="${redis2.ip}" />    
  14.     <constructor-arg index="1" value="${redis.port}" type="int" />    
  15. </bean>     
  16.   
  17. <bean id="jedisShardInfo2" class="redis.clients.jedis.JedisShardInfo">    
  18.     <constructor-arg index="0" value="${redis.ip}" />    
  19.     <constructor-arg index="1" value="${redis.port}" type="int" />    
  20. </bean>     
  21.   
  22. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">    
  23.     <constructor-arg index="0" ref="jedisPoolConfig" />    
  24.     <constructor-arg index="1">  
  25.         <list>  
  26.             <ref bean="jedisShardInfo1" />  
  27.             <ref bean="jedisShardInfo2"/>  
  28.         </list>  
  29.     </constructor-arg>    
  30. </bean>  
  31.   
  32. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  33.     >  
  34.     <property name="hostName"   value="${redis.ip}" />    
  35.     <property name="port"       value="${redis.port}" />    
  36.     <property name="poolConfig" ref="jedisPoolConfig" />   
  37.   
  38.     <!--<property name="shardInfo"  ref="shardedJedisPool"></property>-->  
  39. </bean>   
  40.       
  41. <context:property-placeholder  location="/WEB-INF/spring/SystemContext.properties"/>   
  42. <context:component-scan base-package="org.springframework.data.redis.samples"/>  
  43.          

 

属性文件内容如下:

[plain]  view plain copy print ?
 
  1. redis.ip=192.168.1.110  
  2. redis2.ip=192.168.1.112  
  3. #Port     
  4. redis.port=6379  
  5.   
  6. #最大分配的对象数   
  7. redis.pool.maxActive=1024  
  8. #最大能够保持idel状态的对象数  
  9. redis.pool.maxIdle=200  
  10. #当池内没有返回对象时,最大等待时间  
  11. redis.pool.maxWait=1000  
  12. #当调用borrow Object方法时,是否进行有效性检查   
  13. redis.pool.testOnBorrow=true  
  14. #当调用return Object方法时,是否进行有效性检查     
  15. redis.pool.testOnReturn=true  


缓存管理接口:

[java]  view plain copy print ?
 
  1. public interface RedisCache {  
  2.       
  3.     public <T> T getRedisCacheInfo(String key);  
  4.   
  5.     public <T> boolean setRedisCacheInfo(String key, T value);  
  6.       
  7. }  


缓存管理实现:

[java]  view plain copy print ?
 
  1. public class RedisCacheManger implements RedisCache {  
  2.       
  3.     private ShardedJedisPool pool ;  
  4.       
  5.     private Logger log = Logger.getLogger(RedisCacheManger.class);   
  6.     public ShardedJedisPool getPool() {  
  7.         return pool;  
  8.     }  
  9.   
  10.     public void setPool(ShardedJedisPool pool) {  
  11.         this.pool = pool;  
  12.     }  
  13.   
  14.     public <T> T getRedisCacheInfo(String key) {  
  15.           
  16.         try {  
  17.             log.info("get from redisCache :"+key);  
  18.             System.out.println("get from rediscache");  
  19.             ShardedJedis jedis = pool.getResource();  
  20.             pool.returnResource(jedis);  
  21.             return (T)jedis.get(key);  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         return null;  
  26.     }  
  27.    
  28.   
  29.     public <T> boolean setRedisCacheInfo(String key, T value) {  
  30.   
  31.         try {  
  32.             log.info("add to redisCache :"+key);  
  33.             System.out.println("add to rediscache");  
  34.             ShardedJedis jedis = pool.getResource();  
  35.             jedis.set(key, (String)value);  
  36.             pool.returnResource(jedis);  
  37.             return true;  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return false;  
  42.     }  
  43.     public static void main(String[] args) {  
  44.         new RedisCacheManger().setRedisCacheInfo("12345""asdfg");  
  45.     }  
  46. }  


缓存切面注解:

[java]  view plain copy print ?
 
  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target(ElementType.METHOD)  
  3. public @interface NeedRedisCached {}  


缓存切面处理类:

[java]  view plain copy print ?
 
  1. @Aspect  
  2. public class RedisCacheAspect implements Ordered {  
  3.       
  4.     private static Logger log = Logger.getLogger(RedisCacheAspect.class);   
  5.     private RedisCache redisCacheManager;  
  6.     private int orderValue = 3;  
  7.     public RedisCache getRedisCacheManager() {  
  8.         return redisCacheManager;  
  9.     }  
  10.   
  11.     public void setRedisCacheManager(RedisCache redisCacheManager) {  
  12.         this.redisCacheManager = redisCacheManager;  
  13.     }  
  14.   
  15.     @Pointcut("@annotation(com.jd.bi.odp.common.cache.core.NeedRedisCached)")  
  16.     public void needRedisCached() {  
  17.   
  18.     }  
  19.       
  20.     @Around("needRedisCached() && args(filter,..)")  
  21.     public Object aroundInvoke(ProceedingJoinPoint pjp, QueryFilter filter) throws Throwable {  
  22.         log.info("enter aroundInvoke!!!");  
  23.         if (filter.getValue() == null) {  
  24.             return null;  
  25.         }  
  26.   
  27.         boolean cacheEnabled = CommonUtil.parseBoolean(HBaseConfig.getProperty("redisCache.enabled"), false);  
  28.   
  29.         if (cacheEnabled) {  
  30.               
  31.             String md5key = MD5Util.getMD5(filter.getValue().toString());  
  32.             Object value = redisCacheManager.getRedisCacheInfo(md5key);  
  33.             boolean flag = false;  
  34.             if (null != value) {  
  35.   
  36.                 JSONObject json = new JSONObject(value.toString());  
  37.                 return json;  
  38.             } else if ("null".equals(value)) {  
  39.                 return null;  
  40.             } else { //执行hbase查询  
  41.                 value = pjp.proceed();  
  42.                 if(null!=value){//此处根据业务逻辑判断不缓存的条件  
  43.                     }  
  44.                     else{  
  45.                         flag = redisCacheManager.setRedisCacheInfo(md5key, value.toString());  
  46.                           
  47.                         if(flag)  
  48.                             log.info("add a cache success by key: "+md5key);  
  49.                         else  
  50.                             log.warn("add a cache failure by key: "+md5key);  
  51.                     }  
  52.                 }  
  53.                 return value;  
  54.             }  
  55.         } else {// 执行hbase查询  
  56.             return pjp.proceed();  
  57.         }  
  58.     }  
  59.     @Override  
  60.     public int getOrder() {  
  61.         return orderValue;  
  62.     }  
  63.     public int getOrderValue() {  
  64.         return orderValue;  
  65.     }  
  66.   
  67.     public void setOrderValue(int orderValue) {  
  68.         this.orderValue = orderValue;  
  69.     }  
  70. }  


缓存存在直接返回,不存在的话执行数据源查询,此处是hbase查询,并设置缓存。

 

切面配置:

[html]  view plain copy print ?
 
  1. <!-- redis缓存运行切面 -->  
  2. <bean id="RedisCacheAspect"  
  3.     class="com.cr.common.cache.core.RedisCacheAspect">  
  4.     <property name="orderValue" value="3" />  
  5.     <property name="redisCacheManager" ref="redisCacheManager"/>  
  6. </bean>  
  7. <!-- 切面申明配置-->  
  8. <aop:aspectj-autoproxy>  
  9.     <aop:include name="RedisCacheAspect" />  
  10. </aop:aspectj-autoproxy>  


此时,前端web页面用户的访问触发的action如果满足条件,则会进入切面方法处理,完成redis缓存的使用。

http://blog.csdn.net/cuirong1986/article/details/8213159?utm_source=tuicool&utm_medium=referral

猜你喜欢

转载自m635674608.iteye.com/blog/2252918