Spring3.1 Cache注解

依赖jar包:

Xml代码  收藏代码
  1. <!-- redis -->  
  2.         <dependency>  
  3.             <groupId>org.springframework.data</groupId>  
  4.             <artifactId>spring-data-redis</artifactId>  
  5.             <version>1.3.4.RELEASE</version>  
  6.         </dependency>  
  7.   
  8.         <dependency>  
  9.             <groupId>redis.clients</groupId>  
  10.             <artifactId>jedis</artifactId>  
  11.             <version>2.5.2</version>  
  12.         </dependency>  

 applicationContext-cache-redis.xml

Xml代码  收藏代码
  1. <context:property-placeholder  
  2.         location="classpath:/config/properties/redis.properties" />  
  3.   
  4.     <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->  
  5.     <cache:annotation-driven cache-manager="cacheManager" />  
  6.   
  7.     <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->  
  8.     <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
  9.         <property name="caches">  
  10.             <set>  
  11.                 <bean class="org.cpframework.cache.redis.RedisCache">  
  12.                     <property name="redisTemplate" ref="redisTemplate" />  
  13.                     <property name="name" value="default"/>  
  14.                 </bean>  
  15.                 <bean class="org.cpframework.cache.redis.RedisCache">  
  16.                     <property name="redisTemplate" ref="redisTemplate02" />  
  17.                     <property name="name" value="commonCache"/>  
  18.                 </bean>  
  19.             </set>  
  20.         </property>  
  21.     </bean>  
  22.   
  23.     <!-- redis 相关配置 -->  
  24.     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  25.         <property name="maxIdle" value="${redis.maxIdle}" />        
  26.         <property name="maxWaitMillis" value="${redis.maxWait}" />  
  27.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  28.     </bean>  
  29.   
  30.     <bean id="connectionFactory"  
  31.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  32.         p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"  
  33.         p:database="${redis.database}" />  
  34.   
  35.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  36.         <property name="connectionFactory" ref="connectionFactory" />  
  37.     </bean>  
  38.       
  39.     <bean id="connectionFactory02"  
  40.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  41.         p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"  
  42.         p:database="${redis.database}" />  
  43.   
  44.     <bean id="redisTemplate02" class="org.springframework.data.redis.core.RedisTemplate">  
  45.         <property name="connectionFactory" ref="connectionFactory02" />  
  46.     </bean>  

redis.properties

Java代码  收藏代码
  1. # Redis settings    
  2. # server IP  
  3. redis.host=192.168.xx.xx  
  4. # server port  
  5. redis.port=6379     
  6. # use dbIndex  
  7. redis.database=0  
  8. # 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例  
  9. redis.maxIdle=300    
  10. # 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;  
  11. redis.maxWait=3000    
  12. # 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的  
  13. redis.testOnBorrow=true    

RedisCache.java

Java代码  收藏代码
  1. package org.cpframework.cache.redis;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8.   
  9. import org.springframework.cache.Cache;  
  10. import org.springframework.cache.support.SimpleValueWrapper;  
  11. import org.springframework.dao.DataAccessException;  
  12. import org.springframework.data.redis.connection.RedisConnection;  
  13. import org.springframework.data.redis.core.RedisCallback;  
  14. import org.springframework.data.redis.core.RedisTemplate;  
  15.   
  16.   
  17. public class RedisCache implements Cache {  
  18.   
  19.     private RedisTemplate<String, Object> redisTemplate;  
  20.     private String name;  
  21.   
  22.     public RedisTemplate<String, Object> getRedisTemplate() {  
  23.         return redisTemplate;  
  24.     }  
  25.   
  26.     public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {  
  27.         this.redisTemplate = redisTemplate;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     @Override  
  35.     public String getName() {  
  36.         // TODO Auto-generated method stub  
  37.         return this.name;  
  38.     }  
  39.   
  40.     @Override  
  41.     public Object getNativeCache() {  
  42.         // TODO Auto-generated method stub  
  43.         return this.redisTemplate;  
  44.     }  
  45.   
  46.     @Override  
  47.     public ValueWrapper get(Object key) {  
  48.         // TODO Auto-generated method stub  
  49.         final String keyf = (String) key;  
  50.         Object object = null;  
  51.         object = redisTemplate.execute(new RedisCallback<Object>() {  
  52.             public Object doInRedis(RedisConnection connection)  
  53.                     throws DataAccessException {  
  54.   
  55.                 byte[] key = keyf.getBytes();  
  56.                 byte[] value = connection.get(key);  
  57.                 if (value == null) {  
  58.                     return null;  
  59.                 }  
  60.                 return toObject(value);  
  61.   
  62.             }  
  63.         });  
  64.         return (object != null ? new SimpleValueWrapper(object) : null);  
  65.     }  
  66.   
  67.     @Override  
  68.     public void put(Object key, Object value) {  
  69.         // TODO Auto-generated method stub  
  70.         final String keyf = (String) key;  
  71.         final Object valuef = value;  
  72.         final long liveTime = 86400;  
  73.   
  74.         redisTemplate.execute(new RedisCallback<Long>() {  
  75.             public Long doInRedis(RedisConnection connection)  
  76.                     throws DataAccessException {  
  77.                 byte[] keyb = keyf.getBytes();  
  78.                 byte[] valueb = toByteArray(valuef);  
  79.                 connection.set(keyb, valueb);  
  80.                 if (liveTime > 0) {  
  81.                     connection.expire(keyb, liveTime);  
  82.                 }  
  83.                 return 1L;  
  84.             }  
  85.         });  
  86.     }  
  87.   
  88.     /** 
  89.      * 描述 : <Object转byte[]>. <br> 
  90.      * <p> 
  91.      * <使用方法说明> 
  92.      * </p> 
  93.      *  
  94.      * @param obj 
  95.      * @return 
  96.      */  
  97.     private byte[] toByteArray(Object obj) {  
  98.         byte[] bytes = null;  
  99.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  100.         try {  
  101.             ObjectOutputStream oos = new ObjectOutputStream(bos);  
  102.             oos.writeObject(obj);  
  103.             oos.flush();  
  104.             bytes = bos.toByteArray();  
  105.             oos.close();  
  106.             bos.close();  
  107.         } catch (IOException ex) {  
  108.             ex.printStackTrace();  
  109.         }  
  110.         return bytes;  
  111.     }  
  112.   
  113.     /** 
  114.      * 描述 : <byte[]转Object>. <br> 
  115.      * <p> 
  116.      * <使用方法说明> 
  117.      * </p> 
  118.      *  
  119.      * @param bytes 
  120.      * @return 
  121.      */  
  122.     private Object toObject(byte[] bytes) {  
  123.         Object obj = null;  
  124.         try {  
  125.             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);  
  126.             ObjectInputStream ois = new ObjectInputStream(bis);  
  127.             obj = ois.readObject();  
  128.             ois.close();  
  129.             bis.close();  
  130.         } catch (IOException ex) {  
  131.             ex.printStackTrace();  
  132.         } catch (ClassNotFoundException ex) {  
  133.             ex.printStackTrace();  
  134.         }  
  135.         return obj;  
  136.     }  
  137.   
  138.     @Override  
  139.     public void evict(Object key) {  
  140.         // TODO Auto-generated method stub  
  141.         final String keyf = (String) key;  
  142.         redisTemplate.execute(new RedisCallback<Long>() {  
  143.             public Long doInRedis(RedisConnection connection)  
  144.                     throws DataAccessException {  
  145.                 return connection.del(keyf.getBytes());  
  146.             }  
  147.         });  
  148.     }  
  149.   
  150.     @Override  
  151.     public void clear() {  
  152.         // TODO Auto-generated method stub  
  153.         redisTemplate.execute(new RedisCallback<String>() {  
  154.             public String doInRedis(RedisConnection connection)  
  155.                     throws DataAccessException {  
  156.                 connection.flushDb();  
  157.                 return "ok";  
  158.             }  
  159.         });  
  160.     }  
  161.   
  162. }  
原文地址:https://www.cnblogs.com/hujihon/p/5313142.html