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. php做APP接口开发,接口的安全性

    1.当用户登录APP时,使用https协议调用后台相关接口,服务器端根据用户名和密码时生成一个access_key,并将access_key保存在session(或者保存在redis)中,将生成的ac ...

  2. [Qt Creator 快速入门] 第9章 国际化、帮助系统和Qt插件

    一.国际化 国际化的英文表述为Internationalization,通常简写为I18N(首尾字母加中间的字符数),一个应用程序的国际化就是使该应用程序可以让其他国家的用户使用的过程. Qt支持现在 ...

  3. php angular/think angular/php模版引擎

    在thinphp5中发现一个好用的模版引擎—think-angular, 此模板引擎主要特点是 不需要额外的标签定义, 全部使用属性定义, 写好的模板文件在IDE格式化代码的时候很整洁, 因为套完的模 ...

  4. Selenium常用方法及函数

    新建实例driver = webdriver.Chrome() 1.获取当前页面Url的函数方法:current_url实例:driver.current_url 2.表单的提交方法:submit解释 ...

  5. oracle数据库定时备份

    现有需求,设计一批处理文件,实现周一全部备份,周二周三周五周六增量备份,周四周日累计备份.并且每日将上个月本日数据删除(如今日8月7号,则删除7月7号的数据).并将备份压缩以减小所占空间. 思路: 备 ...

  6. Codeforces_768_B_(二分)

    B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. MxCAD5.2 20181022更新

    下载地址: http://www.mxdraw.com/ndetail_10108.html 1. 开放VIP功能,无需购买即可使用 2. 修正一些图纸打开和保存出错的问题 3. 修改填充命令,对某些 ...

  8. vuex状态管理demo

    vuex状态管理主要包含四个概念  mapState,mapMutations,mapGetters,mapActions. 编写vuex文件夹下面的store.js import Vue from ...

  9. docker 1-->docker machine 转载

    Docker Machine 是 Docker 官方编排(Orchestration)项目之一,负责在多种平台上快速安装 Docker 环境. Docker Machine 是一个工具,它允许你在虚拟 ...

  10. 无插件纯Web HTML5 3D机房 进阶篇(新增设备、线缆、巡查等功能)

    前情提要 前阵子写了一篇无插件纯Web 3D机房,介绍了如何用html5在网页上创建无插件的精美3d机房场景.这两个月以来,陆续收到很多朋友的鼓(膝)励(盖),受宠若惊之余,对索要源代码的朋友都已经尽 ...