Spring 集成 redis(十一)

前言:

      目前公司项目在上一个技术架构的处理,已经搭建好了Redis,但redis只用在了做session的管理,然而 后台的对象缓存没有用上


  1. redis 和 ehcache的区别:

       简单了解了下,个人觉得 从部署上而言,redis更适合分布式部署,ehcache是在每台应用服务器上开辟一块内存做缓存,集群时还得考虑缓存的情况, redis就不需要考虑缓存了、单独部署在一台服务器中(也可以是在某一台应用服务器中)

     参考: http://blog.youkuaiyun.com/jationxiaozi/article/details/8509732 



 2. 项目配置(spring mvc+maven+mybaits+redis),这里只讲Spring 集成 redis:

        a. 配置 pom.xml 文件  (若不是maven管理项目,下载2个jar 即可 )

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <!-- redis cache related.....start -->  
  2.         <dependency>  
  3.             <groupId>org.springframework.data</groupId>  
  4.             <artifactId>spring-data-redis</artifactId>  
  5.             <version>1.6.0.RELEASE</version>  
  6.         </dependency>  
  7.         <dependency>  
  8.             <groupId>redis.clients</groupId>  
  9.             <artifactId>jedis</artifactId>  
  10.             <version>2.7.3</version>  
  11.         </dependency>  
  12.         <!-- redis cache related.....end -->  


        b.配置 applicationContext.xml文件

           先在<beans>中加入 cache缓存

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. xmlns:cache="http://www.springframework.org/schema/cache"  
  2.   
  3. xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"  

      

             在Spring加载redis配置

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <!-- ******************** redis缓存   **********************-->  
  2. <!-- 注解一定要配置,不然不起作用 -->  
  3. <cache:annotation-driven />  
  4.   
  5.   
  6. <!-- jedis 配置 -->  
  7.    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  8.        <property name="maxIdle" value="${redis.maxIdle}" />  
  9.        <!--<property name="maxWaitMillis" value="${redis.maxWait}" />-->  
  10.        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  11.    </bean>  
  12.   
  13.    <!-- redis服务器中心 -->  
  14.    <bean id="connectionFactory"  
  15.        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  16.        <property name="poolConfig" ref="poolConfig" />  
  17.        <property name="port" value="${redis.port}" />  
  18.        <property name="hostName" value="${redis.hostname}" />  
  19.        <!-- <property name="password" value="${redis.password}" /> -->  
  20.        <property name="timeout" value="${redis.timeout}"></property>  
  21.    </bean>  
  22.      
  23.    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  24.        <property name="connectionFactory" ref="connectionFactory" />  
  25.        <property name="keySerializer">  
  26.            <bean  
  27.                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  28.        </property>  
  29.        <property name="valueSerializer">  
  30.            <bean  
  31.                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  32.        </property>  
  33.    </bean>  
  34.    <!-- 配置缓存 -->  
  35.    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">  
  36.        <constructor-arg ref="redisTemplate" />  
  37.    </bean>  
  38.      
  39. <!-- ******************** redis缓存   **********************-->  


        c.配置 application.properties 资源文件

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #redis config  
  2. #redis.hostname=192.168.242.131    
  3. redis.hostname=localhost  
  4. redis.port=6379    
  5. redis.timeout=2000  
  6. redis.usePool=true  
  7. redis.default.db=0  
  8. #\u6700\u5927\u5206\u914D\u7684\u5BF9\u8C61\u6570     
  9. redis.maxTotal=600  
  10. #\u6700\u5927\u80FD\u591F\u4FDD\u6301idel\u72B6\u6001\u7684\u5BF9\u8C61\u6570    
  11. redis.maxIdle=300   
  12. #\u591A\u957F\u65F6\u95F4\u68C0\u67E5\u4E00\u6B21\u8FDE\u63A5\u6C60\u4E2D\u7A7A\u95F2\u7684\u8FDE\u63A5  
  13. redis.timeBetweenEvictionRunsMillis=30000    
  14. #\u7A7A\u95F2\u8FDE\u63A5\u591A\u957F\u65F6\u95F4\u540E\u4F1A\u88AB\u6536\u56DE  
  15. redis.minEvictableIdleTimeMillis=30000   
  16. #\u5F53\u8C03\u7528borrow Object\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u6027\u68C0\u67E5    
  17. redis.testOnBorrow=true   
  18. ########reids\u7F16\u7801\u683C\u5F0F  
  19. redis.encode=utf-8  
  20. ######\u7F13\u5B58\u8FC7\u671F\u65F6\u95F4 \u79D2  1000*60*60*24*7 \u4E03\u5929  
  21. redis.expire=604800000  
  22. ####\u662F\u5426\u5F00\u542FRedis\u670D\u52A1\u5E94\u7528  
  23. redis.unlock=false  

3. 测试

   

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. @Service("testService")  
  2. public class TestServiceImpl implements ITestService {  
  3.       
  4.     @Resource  
  5.     private ITestDao testDao;  
  6.   
  7.     @Cacheable(value="testId",key="'id_'+#id")  
  8.     public Test getTestById(int id) {  
  9.         return this.testDao.getObjById(id);  
  10.     }  
  11.       
  12.     @CacheEvict(value="testId",key="'id_'+#id")  
  13.     public void removeTestById(int id) {  
  14.           
  15.     }  
  16. }  



缓存注解

三个注解是@Cacheable()和@CacheEvict()和@CachePut()  对于缓存声明,缓存抽象类提供两个Java注解:

@Cacheable和@CacheEvict,这两个注解可以允许方法触发缓存或者缓存抽取。 

@Cacheable缓存     应用到读取数据的方法上,即可缓存的方法,如查找方法。  

 @CachePut  应用到写数据的方法上,如新增/修改方法 @CacheEvict  即应用到移除数据的方法上,如删除方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值