依赖jar包:

  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. <dependency>
  8. <groupId>redis.clients</groupId>
  9. <artifactId>jedis</artifactId>
  10. <version>2.5.2</version>
  11. </dependency>

applicationContext-cache-redis.xml

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

redis.properties

  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

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

Spring3.1 Cache注解的更多相关文章

  1. [转载] Spring3.1 Cache注解

    需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管理cache注解,好在3.1.M1中增加了对cache注解的支持, ...

  2. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

  3. 十二:SpringBoot-基于Cache注解模式,管理Redis缓存

    SpringBoot-基于Cache注解模式,管理Redis缓存 1.Cache缓存简介 2.核心API说明 3.SpringBoot整合Cache 3.1 核心依赖 3.2 Cache缓存配置 3. ...

  4. Spring3.2新注解@ControllerAdvice

    Spring3.2新注解@ControllerAdvice   @ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@Control ...

  5. springboot整合redis-sentinel支持Cache注解

    一.前提 已经存在一个redis-sentinel集群,两个哨兵分别如下: /home/redis-sentinel-cluster/sentinel-1.conf port 26379 dir &q ...

  6. Spring的Cache注解

    Spring的Cache注解如下所示: @CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置.在这里@CacheConfig(cacheNames = "users&quo ...

  7. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  8. Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  9. spring源码分析之cache注解

    Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓存使用的抽象 ...

随机推荐

  1. Enum:Hopscotch(POJ 3050)

    跳格子 题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串? 题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可 ...

  2. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

  3. angularjs 指令(directive)详解(1)

    原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...

  4. Catch That Cow(poj 3278)

    给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 //广搜,a是队列,step记录步数,vis记录哪些数被搜到过 #include<cstdio& ...

  5. 【Chrome】手动下载和安装Adblock Plus的方法

    由于强大的GFW,导致很多Google的站点没法访问,也就没法直接安装chrome的插件了,网页都打不卡, 那么几乎是必备的Adblock Plus如何下载安装呢? 1.访问官方网站: https:/ ...

  6. Ubuntu 15.10系统安装后要做的15件事

    Ubuntu 15.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  7. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  8. [杂]DeadLock, Isolation Level, EntityFramework

    由于没有注意到EF事务的默认隔离级别是Serializable,(据说EF6.0以后默认隔离级别改成了Read_Commit_Snapshot)--这里有误,应该是加了TransactionScope ...

  9. HBase伪分布式环境下,HBase的API操作,遇到的问题

    在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示  Exception in thread "main&q ...

  10. Xamarin.Android提示aapt退出,代码为255

    Xamarin.Android提示aapt退出,代码为255 错误信息:”aapt.exe”已退出,代码为255.出现这种问题,通常是由于该项目所使用Android SDK不完整.通过SDK Mana ...