redis+spring
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的更多相关文章
- redis + spring 集成
1.pom <modelVersion>4.0.0</modelVersion> <groupId>com.x.redis</groupId> < ...
- Redis+Spring缓存实例
转自:小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).lis ...
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
原文出处: 小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). ...
- redis+spring 整合
最近在研究redis也结合了许多网上的资料分享给大家,有些不足的还望大家多补充提点,下面直接进入主题. 结构图: 几个redis的核心jar,spring的一些jar自行导入 接下来开始配置项目: 1 ...
- redis—Spring中redis缓存的简单使用
这里使用的是 Spring-4.3 , redis-2.8 的版本 1.添加maven依赖 <dependency> <groupId>redis.clients</ ...
- Jedis+Redis+spring缓存
Redis程序使用它?Jedis 访问redis java api Redis-server & //后台运行防火墙要关闭 ts-parent的pom.xml加上jedis依赖 <dep ...
- Invalid property 'sentinels' of bean class redis spring 错误修改
/* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...
- Java 在spring cloud中使用Redis,spring boot同样适用
1.本地安装redis服务,官网下载. 2.在开发中要使用redis,首先要启动本地redis服务,启动后页面如下: 3.在spring boot项目pom.xml文件中添加Redis需要的依赖包,可 ...
- SpringBoot集成redis + spring cache
Spring Cache集成redis的运行原理: Spring缓存抽象模块通过CacheManager来创建.管理实际缓存组件,当SpringBoot应用程序引入spring-boot-starte ...
随机推荐
- 士兵杀敌 三 --- O( 1 ) 的时间复杂度 .
一看就是 十分简单的 题 , 然后上去开始无脑程序 超时~~~ 感觉时间复杂度 , 已经很低了 , 但是并没有什么卵用 . #include<stdio.h> #in ...
- [C++ STL] 迭代器(iterator)详解
背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间非连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历.因此,我们引入迭代器概念. 一.迭代器(iterator)介绍 ...
- 【SpringMVC框架】非注解的处理器映射器和适配器
参考来源: http://blog.csdn.net/acmman/article/details/46968939 处理器映射器就是根据URL来找Handler,处理器适配器就是按照它要求的 ...
- HTML基础2——综合案例3——创建考试报名表格
<html> <head> <title></title> </head> <body> <table width=&qu ...
- Axis通过wsdd部署Web Service
axis网上的教程很多,不过搜来搜去,总是只有那么几篇.仔细看了一下那几篇文章,都感觉到不是自己想要的,所以自己整理了一篇分享一下. 本文介绍axis应用的一个小例子,没有麻烦的命令行操作,只需照下面 ...
- 287 Find the Duplicate Number 寻找重复数
一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在.假设只有一个数字重复,找出这个重复的数字.注意: 不能更改数组内容(假设数 ...
- Spring(二) -- 春风拂面之 核心 AOP
”万物皆对象“是面向对象编程思想OOP(Object Oriented Programming) 的最高境界.在面向对象中,我一直将自己(开发者)放在一个至高无上的位置上,可以操纵万物(对象),犹如一 ...
- poj1923 Fourier's Lines
思路: 记忆化搜索. n条直线的交点方案数 =(n-r)条平行线与r条直线交叉的交点数+r条直线本身的交点方案 =(n-r)*r+r条直线之间本身的交点方案数(0<r<=n) 于是可以枚举 ...
- 如何向expect脚本里面传递参数
如何向expect脚本里面传递参数 比如下面脚本用来做ssh无密码登陆,自动输入确认yes和密码信息,用户名,密码,hostname通过参数来传递 ssh.exp Python代码 # ...
- Python中的排序方法
1 list.sort list.sort(key=None, reverse=False) 该方法只能用于list.就地排序,原来的list被修改.key的用法见下文.reverse控制降序还是生序 ...