1. 在配置文件中添加 注解 <cache:annotation-driven cache-manager="cacheManager" key-generator="keyGenerator" />

2.定义缓存管理侧率

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<ref bean="sharedCache1" />
<ref bean="sharedCache2" />
</set>
</property>
</bean>

3 实际的缓存处理类

<bean id="sharedCache1" class="cache.redis.DataCache">
<constructor-arg name="name" value="default" />
<constructor-arg name="cacheService" ref="cacheService" />
</bean>
<bean id="sharedCache2" class="cache.redis.DataCache">
<constructor-arg name="name" value="cache2s" />
<constructor-arg name="cacheService" ref="cacheService" />
<property name="expire" value="2" />
</bean>

4. 配置jedisPoolConfig

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxActive(Integer.parseInt(cachebundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.parseInt(cachebundle.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.parseLong(cachebundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.parseBoolean(cachebundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(cachebundle.getString("redis.pool.testOnReturn")));

5 生成一个jedisPool

JedisPool pool4master = new JedisPool(config, cachebundle.getString("cache.redis.host"), Integer.parseInt(cachebundle.getString("cache.redis.port")), Integer.parseInt(cachebundle.getString("redis.pool.timeout")));

6.得到一个jedis连接

jedis = pools.get(MASTER_KEY_PREFIX).getResource();

7.释放一个jedis连接

pools.get(MASTER_KEY_PREFIX).returnBrokenResource(jedis);

8.设置redis主库值

public static void set(Object key, Object value, int expire) {
Jedis redis = getRedisConnection();
byte[] skey = SerializationUtils.serialize(toSerializable(key));
byte[] svalue = SerializationUtils.serialize(toSerializable(value));
redis.set(skey, svalue);
redis.expire(skey, expire);
returnResource(redis);
}

9.从redis丛库得到一个值

public static Object get(Object key) {
Jedis redis = getSlaveRedisConnection();
byte[] result = redis.get(SerializationUtils.serialize(toSerializable(key)));
returnSlaveResource(redis);
return result == null ? null : SerializationUtils.deserialize(result);
}

总结:

  1、用了spring的cache标签 ,cache:annotation-driven,定义了cache管理器  cache-manager,及key的生成策略

  2.cachemanage的策略为org.springframework.cache.support.SimpleCacheManager,具体的实现类为DataCache

  3.DataCache 这个引用了CacheService的服务

  4.CacheService调用了CacheRedisAccessor,针对redis客户端的一个包装,初始化JedisPoolConfig及 JedisPool,实现了主从结构,初始化两个jedispool,从pool中取jedis连接。其中的key和value都为byte[]类型,用序列化和反序列化。

redis分布式缓存集群配置参考:

https://www.jerrylou.me/redis/redis-cache-intro-20170125.html

http://www.thinksaas.cn/topics/0/515/515811.html

http://dbaplus.cn/news-21-239-1.html

http://dbaplus.cn/news-21-774-1.html

https://juejin.im/entry/56baa0cfc4c97100522945d3

最新文章

  1. apache和tomcat有什么不同,为什么要整合apache 和tomcat
  2. wget 扒站
  3. python中set集合
  4. 精通javascript(看书笔记)
  5. GSM Sniffing入门之软件篇:GSMTAP抓取与SMS(Short Message Service)
  6. XAML概览 1(译自JeremyBytes.com)
  7. 2014-9-17二班----11 web project
  8. IOS把文件保存进沙盒目录
  9. Machine Learning &amp; Data Mining 资料整合
  10. android体系架构
  11. (转) 值不能为空。参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explorer)
  12. ajax动态加入的元素不被jquerymobile渲染问题
  13. 网页启动Windows服务
  14. Qt终结者之粒子系统
  15. async await 的使用。 其实就是和then一样,只不过改变了链式写法
  16. linux:scp从入门到刚入门
  17. 事务的ACID性质
  18. 网络中TCP、IP、MAC、UDP的头部格式信息
  19. C# 实现屏幕截屏
  20. iOS开发UI之Quartz2D使用(绘制基本图形)

热门文章

  1. Windows 使用之那些你还不知道操作
  2. 数据返回(数据共享,即从后端返回到前端调用,四种(requesst、ModelAndView、Model、Map))
  3. 重构27-Remove God Classes(去掉神类)
  4. Eclipse 编译java文件后出错 左树无红叉
  5. grunt 全局使用
  6. Tcl之Read files for synthesis
  7. Java软件开发中迭代的含义
  8. CAD如何直接打印,不出现打印对话框?
  9. 在TWaver的Tree节点上画线
  10. 低版本ie兼容问题的解决方案