zoukankan      html  css  js  c++  java
  • Spring集成Redis使用注解

    转载: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值必须是相同的。

     
  • 相关阅读:
    spring事务调用失效问题
    redis的主从、哨兵配置
    Lucene介绍与入门使用
    超详细“零”基础kafka入门篇
    消息队列——RabbitMQ学习笔记
    linux中RabbitMQ安装教程
    JAVA正则
    JAVA String类
    JAVA lang包介绍
    JAVA枚举
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/6539978.html
Copyright © 2011-2022 走看看