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

redis+spring的更多相关文章

  1. redis + spring 集成

    1.pom <modelVersion>4.0.0</modelVersion> <groupId>com.x.redis</groupId> < ...

  2. Redis+Spring缓存实例

    转自:小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).lis ...

  3. Redis+Spring缓存实例(windows环境,附实例源码及详解)

    原文出处: 小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). ...

  4. redis+spring 整合

    最近在研究redis也结合了许多网上的资料分享给大家,有些不足的还望大家多补充提点,下面直接进入主题. 结构图: 几个redis的核心jar,spring的一些jar自行导入 接下来开始配置项目: 1 ...

  5. redis—Spring中redis缓存的简单使用

    这里使用的是 Spring-4.3 , redis-2.8 的版本   1.添加maven依赖 <dependency> <groupId>redis.clients</ ...

  6. Jedis+Redis+spring缓存

    Redis程序使用它?Jedis 访问redis java api Redis-server & //后台运行防火墙要关闭 ts-parent的pom.xml加上jedis依赖 <dep ...

  7. Invalid property 'sentinels' of bean class redis spring 错误修改

    /* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...

  8. Java 在spring cloud中使用Redis,spring boot同样适用

    1.本地安装redis服务,官网下载. 2.在开发中要使用redis,首先要启动本地redis服务,启动后页面如下: 3.在spring boot项目pom.xml文件中添加Redis需要的依赖包,可 ...

  9. SpringBoot集成redis + spring cache

    Spring Cache集成redis的运行原理: Spring缓存抽象模块通过CacheManager来创建.管理实际缓存组件,当SpringBoot应用程序引入spring-boot-starte ...

随机推荐

  1. jQuery setInterval倒计时精确到毫秒

    效果类似于:购物抢购倒计时-->在跳转N多个页面之后,倒计时间仍然正常显示. 思路: 结束时间是固定不变的(endTime),一直在改变的是当下的时间(curTime = new date()) ...

  2. curl 做爬虫 用服务器代理ip

    有时候会ip会封锁,所以会用一些模拟代理ip进行抓取测试 从网上找了一下代码 function curl_string ($url,$user_agent,$proxy){        $ch = ...

  3. Asp.Net 开发实战技术

    1.什么是WMI技术 WMI是一项核心的Windows管理技术,WMI作为一种规范和基础结构,通过它可以访问.配置.管理和监视几乎所有的Windows资源,比如用户可以在远程计算机器上启动一个进程:设 ...

  4. Java多线程——线程之间的同步

    Java多线程——线程之间的同步 摘要:本文主要学习多线程之间是如何同步的,如何使用volatile关键字,如何使用synchronized修饰的同步代码块和同步方法解决线程安全问题. 部分内容来自以 ...

  5. linux mint 18.3设置分辨率死机问题的解决方法

    linux mint 18.3由高分辨率设置为低分辨率的时候,会出现死机现象. 解决方法是:使用命令行: xrandr 查询所有支持的分辨率 然后通过 xrandr -s 1920x1080_59.9 ...

  6. 树莓派+百度api实现人脸识别

    title: 树莓派+百度api实现人脸识别 tags: 树莓派 date: 2018-5-31 20:06:00 --- 树莓派对接百度api 我以前玩安卓的时候一直用的讯飞的平台和api,对于百度 ...

  7. js中获取class封装

    1.封装 //封装getClass function getClass(tagName,className) //获得标签名为tagName,类名className的元素 { if(document. ...

  8. 接口与抽象类的区别-Java

    概念-抽象类: 类是对某一个对象的具体描述,抽象类则是对有共通之处的对象,描述共通之处的类;包含抽象方法的类一定的抽象类,抽象类并不一定包含抽象方法;抽象类中可以有数据成员,但数据成员必须是stati ...

  9. CAD梦想看图6.0安卓版 20181022更新

    下载地址: http://www.mxdraw.com/ndetail_10109.html 1. 保存上次的文件浏览位置和绘制颜色 2. 调整工具条按钮位置和文字 3. 增加测量距离和面积时的捕捉功 ...

  10. dom4j使用方法详解

    本文先做知识点的简单介绍,最后附完整案例. 一.解析XML文件 public class Foo { //url为XML文档地址 //自己封装了一个工具类 返回解析完成的document public ...