轉載:http://blog.csdn.net/u013725455/article/details/52129283
使用Maven項目,添加jar文件依賴:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.luo</groupId> 5 <artifactId>redis_project</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 8 <properties> 9 <!-- spring版本號 --> 10 <spring.version>3.2.8.RELEASE</spring.version> 11 <!-- junit版本號 --> 12 <junit.version>4.10</junit.version> 13 </properties> 14 15 <dependencies> 16 <!-- 添加Spring依賴 --> 17 <dependency> 18 <groupId>org.springframework</groupId> 19 <artifactId>spring-core</artifactId> 20 <version>${spring.version}</version> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-webmvc</artifactId> 25 <version>${spring.version}</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-context</artifactId> 30 <version>${spring.version}</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-context-support</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-aop</artifactId> 40 <version>${spring.version}</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-aspects</artifactId> 45 <version>${spring.version}</version> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-tx</artifactId> 50 <version>${spring.version}</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-jdbc</artifactId> 55 <version>${spring.version}</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-web</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 63 <!--單元測試依賴 --> 64 <dependency> 65 <groupId>junit</groupId> 66 <artifactId>junit</artifactId> 67 <version>${junit.version}</version> 68 <scope>test</scope> 69 </dependency> 70 71 <!--spring單元測試依賴 --> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-test</artifactId> 75 <version>${spring.version}</version> 76 <scope>test</scope> 77 </dependency> 78 79 <!-- Redis 相關依賴 --> 80 <dependency> 81 <groupId>org.springframework.data</groupId> 82 <artifactId>spring-data-redis</artifactId> 83 <version>1.6.1.RELEASE</version> 84 </dependency> 85 <dependency> 86 <groupId>redis.clients</groupId> 87 <artifactId>jedis</artifactId> 88 <version>2.7.3</version> 89 </dependency> 90 <!-- enchace --> 91 <dependency> 92 <groupId>net.sf.ehcache</groupId> 93 <artifactId>ehcache-core</artifactId> 94 <version>2.6.6</version> 95 </dependency> 96 </dependencies> 97 </project>
配置Redis參數
1 #redis中心 2 #綁定的主機地址 3 redis.host=127.0.0.1 4 #指定Redis監聽端口,默認端口為6379 5 redis.port=6379 6 #授權密碼(本例子沒有使用) 7 redis.password=123456 8 #最大空閑數:空閑鏈接數大於maxIdle時,將進行回收 9 redis.maxIdle=100 10 #最大連接數:能夠同時建立的“最大鏈接個數” 11 redis.maxActive=300 12 #最大等待時間:單位ms 13 redis.maxWait=1000 14 #使用連接時,檢測連接是否成功 15 redis.testOnBorrow=true 16 #當客戶端閑置多長時間后關閉連接,如果指定為0,表示關閉該功能 17 redis.timeout=10000
集成到spring中
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.0.xsd 13 http://www.springframework.org/schema/cache 14 http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"> 15 16 <!-- 自動掃描注解的bean --> 17 <context:component-scan base-package="com.luo" /> 18 <context:annotation-config /> 19 <cache:annotation-driven /> 20 21 <!-- 引入properties配置文件 --> 22 <bean id="propertyConfigurer" 23 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 24 <property name="locations"> 25 <list> 26 <value>classpath:properties/*.properties</value> 27 <!--要是有多個配置文件,只需在這里繼續添加即可 --> 28 </list> 29 </property> 30 </bean> 31 32 <!-- jedis 配置 --> 33 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 34 <property name="maxIdle" value="${redis.maxIdle}" /> 35 <property name="maxWaitMillis" value="${redis.maxWait}" /> 36 <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 37 </bean> 38 39 <!-- redis服務器中心 --> 40 <bean id="connectionFactory" 41 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 42 <property name="poolConfig" ref="poolConfig" /> 43 <property name="port" value="${redis.port}" /> 44 <property name="hostName" value="${redis.host}" /> 45 <!-- <property name="password" value="${redis.password}" /> --> 46 <property name="timeout" value="${redis.timeout}"></property> 47 </bean> 48 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 49 <property name="connectionFactory" ref="connectionFactory" /> 50 <property name="keySerializer"> 51 <bean 52 class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 53 </property> 54 <property name="valueSerializer"> 55 <bean 56 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 57 </property> 58 </bean> 59 <!-- 配置緩存 --> 60 <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> 61 <constructor-arg ref="redisTemplate" /> 62 </bean> 63 <bean id="methodCacheInterceptor" class="com.luo.redis.cache.MethodCacheInterceptor" > 64 <property name="redisTemplate" ref="redisTemplate" /> 65 </bean > 66 67 <bean id="redisTestService" class="com.luo.service.impl.RedisTestServiceImpl"> 68 </bean> 69 70 </beans>
MethodCacheInterceptor類:
1 package com.redis.cache; 2 3 import java.io.Serializable; 4 import java.util.concurrent.TimeUnit; 5 import org.aopalliance.intercept.MethodInterceptor; 6 import org.aopalliance.intercept.MethodInvocation; 7 import org.springframework.data.redis.core.RedisTemplate; 8 import org.springframework.data.redis.core.ValueOperations; 9 import org.springframework.stereotype.Service; 10 11 @Service 12 public class MethodCacheInterceptor implements MethodInterceptor { 13 14 private RedisTemplate<Serializable, Object> redisTemplate; 15 private Long defaultCacheExpireTime = 10l; 16 17 public Object invoke(MethodInvocation invocation) throws Throwable { 18 Object value = null; 19 20 String targetName = invocation.getThis().getClass().getName(); 21 String methodName = invocation.getMethod().getName(); 22 23 Object[] arguments = invocation.getArguments(); 24 String key = getCacheKey(targetName, methodName, arguments); 25 26 try { 27 28 if (exists(key)) { 29 return getCache(key); 30 } 31 32 value = invocation.proceed(); 33 if (value != null) { 34 final String tkey = key; 35 final Object tvalue = value; 36 new Thread(new Runnable() { 37 public void run() { 38 setCache(tkey, tvalue, defaultCacheExpireTime); 39 } 40 }).start(); 41 } 42 } catch (Exception e) { 43 e.printStackTrace(); 44 if (value == null) { 45 return invocation.proceed(); 46 } 47 } 48 return value; 49 } 50 51 /** 52 * 53 * @param targetName 54 * @param methodName 55 * @param arguments 56 */ 57 private String getCacheKey(String targetName, String methodName, 58 Object[] arguments) { 59 StringBuffer sbu = new StringBuffer(); 60 sbu.append(targetName).append("_").append(methodName); 61 if ((arguments != null) && (arguments.length != 0)) { 62 for (int i = 0; i < arguments.length; i++) { 63 sbu.append("_").append(arguments[i]); 64 } 65 } 66 return sbu.toString(); 67 } 68 69 /** 70 * 71 * @param key 72 * @return 73 */ 74 public boolean exists(final String key) { 75 return redisTemplate.hasKey(key); 76 } 77 78 /** 79 * 80 * @param key 81 * @return 82 */ 83 public Object getCache(final String key) { 84 Object result = null; 85 ValueOperations<Serializable, Object> operations = redisTemplate 86 .opsForValue(); 87 result = operations.get(key); 88 return result; 89 } 90 91 /** 92 * 93 * @param key 94 * @param value 95 * @return 96 */ 97 public boolean setCache(final String key, Object value, Long expireTime) { 98 boolean result = false; 99 try { 100 ValueOperations<Serializable, Object> operations = redisTemplate 101 .opsForValue(); 102 operations.set(key, value); 103 redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 104 result = true; 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 return result; 109 } 110 111 public void setRedisTemplate( 112 RedisTemplate<Serializable, Object> redisTemplate) { 113 this.redisTemplate = redisTemplate; 114 } 115 }
使用注解:
1 /* 2 * 文件名:UserServiceImpl.java 3 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 4 * 描述: UserServiceImpl.java 5 * 修改人:peiyu 6 * 修改時間:2016年8月5日 7 * 修改內容:新增 8 */ 9 package com.luo.service.impl; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 import org.springframework.cache.annotation.CachePut; 15 import org.springframework.cache.annotation.Cacheable; 16 import org.springframework.cache.annotation.Caching; 17 import org.springframework.stereotype.Service; 18 19 import com.luo.redis.anno.UserSaveCache; 20 import com.luo.redis.model.User; 21 import com.luo.service.UserService; 22 23 /** 24 * @author peiyu 25 */ 26 @Service 27 public class UserServiceImpl implements UserService { 28 //@CachePut(value = "user", key = "'id_'+#user.getId()") 29 /*@Caching(put = { 30 @CachePut(value = "user", key = "'user_id_'+#user.id"), 31 @CachePut(value = "user", key = "'user_username_'+#user.username"), 32 @CachePut(value = "user", key = "'user_email_'+#user.email") 33 })*/ 34 @UserSaveCache 35 public User addUser(User user) { 36 System.out.println("addUser,@CachePut注解方法被調用啦。。。。。"); 37 return user; 38 } 39 @Cacheable(value = "user", key = "'id_'+#id") //,key="'user_id_'+#id" 40 public User getUserByID(Integer id) { 41 System.out.println("@Cacheable注解方法被調用啦。。。。。"); 42 User user = new User(); 43 user.setUsername("zhangsan"); 44 user.setPassword("123456"); 45 user.setAge(10); 46 user.setId(123); 47 user.setEmail("192554785@163.com"); 48 return user; 49 } 50 @CachePut(value = "user", key = "'users_'+#user.getId()") 51 public List<User> getUsers(User user) { 52 System.out.println("@CachePut注解方法被調用啦。。。。。"); 53 List<User> users = new ArrayList<User>(); 54 for (int i = 0; i < 10; i++) { 55 User temp = new User(); 56 temp.setUsername("username" + i); 57 users.add(temp); 58 } 59 return users; 60 } 61 }
UserSaveCache自定義注解:
1 /* 2 * 文件名:UserSaveCache.java 3 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 4 * 描述: UserSaveCache.java 5 * 修改人:peiyu 6 * 修改時間:2016年8月5日 7 * 修改內容:新增 8 */ 9 package com.luo.redis.anno; 10 11 import java.lang.annotation.ElementType; 12 import java.lang.annotation.Inherited; 13 import java.lang.annotation.Retention; 14 import java.lang.annotation.RetentionPolicy; 15 import java.lang.annotation.Target; 16 17 import org.springframework.cache.annotation.CachePut; 18 import org.springframework.cache.annotation.Caching; 19 20 /** 21 * @author peiyu 22 */ 23 @Caching(put = { 24 @CachePut(value = "user", key = "'user_id_'+#user.id"), 25 @CachePut(value = "user", key = "'user_username_'+#user.username"), 26 @CachePut(value = "user", key = "'user_email_'+#user.email") 27 }) 28 @Target({ElementType.METHOD, ElementType.TYPE}) 29 @Retention(RetentionPolicy.RUNTIME) 30 @Inherited 31 public @interface UserSaveCache { 32 }
加載配置文件:
1 package com.luo.baseTest; 2 3 import org.junit.runner.RunWith; 4 import org.springframework.test.context.ContextConfiguration; 5 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 8 //指定bean注入的配置文件 9 @ContextConfiguration(locations = { "classpath:application.xml" }) 10 //使用標准的JUnit @RunWith注釋來告訴JUnit使用Spring TestRunner 11 @RunWith(SpringJUnit4ClassRunner.class) 12 public class SpringTestCase extends AbstractJUnit4SpringContextTests { 13 14 }
測試:
1 package com.luo.service; 2 3 import org.junit.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.data.redis.core.RedisTemplate; 6 import org.springframework.data.redis.core.ValueOperations; 7 8 import com.luo.baseTest.SpringTestCase; 9 import com.luo.redis.model.User; 10 11 public class RedisTestServiceTest extends SpringTestCase { 12 13 @Autowired 14 private RedisTemplate<String, Object> redisTemplate; 15 @Autowired 16 private UserService userService; 17 18 @Test 19 public void redisTest() throws InterruptedException { 20 21 User user = new User(); 22 user.setUsername("zhangsan"); 23 user.setPassword("123456"); 24 user.setAge(10); 25 user.setEmail("192554785@163.com"); 26 user.setId(10); 27 System.out.println("=======addUser==================="); 28 userService.addUser(user); 29 // System.out.println("=======第一次getUserByID============"); 30 // System.out.println(userService.getUserByID(11)); 31 // System.out.println("=======第二次getUserByID============"); 32 // System.out.println(userService.getUserByID(11)); 33 // System.out.println("==============================="); 34 // ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue(); 35 // System.out.println("set前:"+opsForValue.get("user")); 36 // opsForValue.set("user", user); 37 // System.out.println("set后:"+opsForValue.get("user")); 38 // redisTemplate.delete("user"); 39 // System.out.println("delete后:"+opsForValue.get("user")); 40 } 41 }
測試結果:
set前:null
set后:10:zhangsan:123456:10:192554785@163.com
delete后:null
測試:getUserByID
=======第一次getUserByID============
@Cacheable注解方法被調用啦。。。。。
123:zhangsan:123456:10:192554785@163.com
=======第二次getUserByID============
123:zhangsan:123456:10:192554785@163.com ===============================
測試:addUser 
1 @CachePut(value=”“,condition=”“,key=”“,unless=”“) 2 3 public @interface CachePut { 4 String[] value(); //緩存的名字,可以把數據寫到多個緩存 5 String key() default ""; //緩存key,如果不指定將使用默認的KeyGenerator生成,后邊介紹 6 String condition() default ""; //滿足緩存條件的數據才會放入緩存,condition在調用方法之前和之后都會判斷 7 String unless() default ""; //用於否決緩存更新的,不像condition,該表達只在方法執行之后判斷,此時可以拿到返回值result進行判斷了 8 } 9 10 11 @Cacheable(value=”“,condition=”“,key=”“,unless=”“) 12 13 public @interface Cacheable{ 14 String[] value(); //緩存的名字,可以把數據寫到多個緩存 15 String key() default ""; //緩存key,如果不指定將使用默認的KeyGenerator生成,后邊介紹 16 String condition() default ""; //滿足緩存條件的數據才會放入緩存,condition在調用方法之前和之后都會判斷 17 String unless() default ""; //用於否決緩存更新的,不像condition,該表達只在方法執行之后判斷,此時可以拿到返回值result進行判斷了 18 } 19 20 21 @Cacheable(value=”“,condition=”“,key=”“,unless=”“) 22 23 public @interface CacheEvict { 24 String[] value(); //緩存的名字,可以把數據寫到多個緩存 25 String key() default ""; //緩存key,如果不指定將使用默認的KeyGenerator生成,后邊介紹 26 String condition() default ""; //滿足緩存條件的數據才會放入緩存,condition在調用方法之前和之后都會判斷 27 boolean allEntries() default false; //是否移除所有數據 28 boolean beforeInvocation() default false;//是調用方法之前移除/還是調用之后移除 29 30 @Caching(value=”“,condition=”“,key=”“,unless=”“) 31 32 public @interface Caching { 33 34 Cacheable[] cacheable() default {}; //從緩存獲取多個,如果沒有則執行方法體,獲取值后加入緩存 35 36 CachePut[] put() default {}; //緩存多個 37 38 CacheEvict[] evict() default {}; //從緩存移除多個 39 40 }
例如:
1 @Caching(put = { 2 @CachePut(value = "user", key = "'user_id_'+#user.id"), 3 @CachePut(value = "user", key = "'user_username_'+#user.username"), 4 @CachePut(value = "user", key = "'user_email_'+#user.email") 5 } 6 7 @Caching(cacheable = { 8 @Cacheable(value = "user", key = "'user_id_'+#user.id"), 9 @Cacheable(value = "user", key = "'user_username_'+#user.username"), 10 @Cacheable(value = "user", key = "'user_email_'+#user.email") 11 }) 12 13 @Caching(evict={ 14 @CacheEvict(value="",key="",condition="",allEntries=false,beforeInvocation=false), 15 @CacheEvict(value="",key="",condition="",allEntries=false,beforeInvocation=false), 16 @CacheEvict(value="",key="",condition="",allEntries=false,beforeInvocation=false) 17 })
運行流程:
1、首先執行@CacheEvict(如果beforeInvocation=true且condition 通過),如果allEntries=true,則清空所有 2、接着收集@Cacheable(如果condition 通過,且key對應的數據不在緩存),放入cachePutRequests(也就是說如果cachePutRequests為空,則數據在緩存中) 3、如果cachePutRequests為空且沒有@CachePut操作,那么將查找@Cacheable的緩存,否則result=緩存數據(也就是說只要當沒有cache put請求時才會查找緩存) 4、如果沒有找到緩存,那么調用實際的API,把結果放入result 5、如果有@CachePut操作(如果condition 通過),那么放入cachePutRequests 6、執行cachePutRequests,將數據寫入緩存(unless為空或者unless解析結果為false); 7、執行@CacheEvict(如果beforeInvocation=false 且 condition 通過),如果allEntries=true,則清空所有
流程中需要注意的就是2/3/4步:
如果有@CachePut操作,即使有@Cacheable也不會從緩存中讀取;問題很明顯,如果要混合多個注解使用,不能組合使用@CachePut和@Cacheable;官方說應該避免這樣使用(解釋是如果帶條件的注解相互排除的場景);不過個人感覺還是不要考慮這個好,讓用戶來決定如何使用,否則一會介紹的場景不能滿足。
CachePut與Cacheable區別:
@CachePut:這個注釋可以確保方法被執行,同時方法的返回值也被記錄到緩存中。
@Cacheable:當重復使用相同參數調用方法的時候,方法本身不會被調用執行,即方法本身被略過了,取而代之的是方法的結果直接從緩存中找到並返回了,如果從緩存中沒有找到數據,則會執行方法,並且將返回值加入到緩存,當再次執行該方法獲取時,會直接從緩存中拿,而不會執行方法體。
@CachePut和@Cacheable這兩個標簽可以結合使用,當需要根據請求改變值的時候,利用@CachePut將值改變並寫入到緩存中,而@Cacheable標簽除了第一次之外,一直是取的緩存的值。注意結合使用時需要注意兩點:
1、必須是同一個緩存實例。
2、key值必須是相同的。
