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. INT类型知多少

    前言: 整型是MySQL中最常用的字段类型之一,通常用于存储整数,其中int是整型中最常用的,对于int类型你是否真正了解呢?本文会带你熟悉int类型相关知识,也会介绍其他整型字段的使用. 1.整型分 ...

  2. less新手入门(三) 作为函数使用的Mixin、@import 导入指令 、@import 导入选项

    五.作为函数使用的Mixin 从mixin返回变量 在mixin中定义的所有变量都是可见的,并且可以在调用者的作用范围中使用(除非调用者用相同的名称定义它自己的变量). .mixin(){ @widt ...

  3. java entity

    对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段.好处:1.对对象实体的封装,体现OO思想.2.属性可以对字段 ...

  4. 记录一次mysql导入千万条测试数据过慢的问题!

    数据库在没有做任何优化的情况下,使用存储过程,插入1千万条测试数据. CREATE PROCEDURE addmaxdata(IN n int) BEGIN DECLARE i INT DEFAULT ...

  5. VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall

    根据个人遇到这个错误时的记录,原因可以分为一下几种: 原因一: 只是在.h里面声明了某个方法, 没有在cpp里面实现 . 具体讲,有时候在头文件中声明了需要的方法,确实忘记了在源文件中实现: 有时候在 ...

  6. LN : leetcode 231 Power of Two

    lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...

  7. CF848A From Y to Y

    思路1: 每次贪心地选择满足i * (i - 1) / 2 <= k最大的i并从k中减去i * (i - 1) / 2,直至k为0.由于函数x * (x - 1) / 2的增长速度比2x要慢,所 ...

  8. CSS垂直居中和水平居中的几种方法

    垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...

  9. CMD命令行提示被禁用的情况下如何继续使用命令行工具执行命令

    1.直接在Windows搜索 左下 输入要执行的 CMD 命令单句.多句同时执行没有试出来. 暂时记录这一条.2016-12-20

  10. Vue指令的概念

    指令(Directives) 是带有v- 前缀的特殊属性,指令属性是单一的js表达式. 指令的职责就是表达式的值发生变化时,在DOM中做出相应的回应. 如下例子: 实例 <div id=&quo ...