继承redis spring_Spring 极速集成注解 Redis 实践
- 1. 添加项目依赖
- 2. 添加 spring-redis-context 配置
- 3. 添加 redis.properties
- 4. 编写自定义 redis 配置类
- 5. 在你喜欢的地方进行注解缓存
- 《Netty 实现原理与源码解析 —— 精品合集》
- 《Spring 实现原理与源码解析 —— 精品合集》
- 《MyBatis 实现原理与源码解析 —— 精品合集》
- 《Spring MVC 实现原理与源码解析 —— 精品合集》
- 《Spring Boot 实现原理与源码解析 —— 精品合集》
- 《数据库实体设计合集》
- 《Java 面试题 —— 精品合集》
- 《Java 学习指南 —— 精品合集》
Redis 做为基于内存的 Key-Value 数据库,用来做缓存服务器性价比相当高。
官方推出的面向 Java 的 Client Jedis,提供了很多接口和方法,可以让 Java 操作使用 Redis。
Spring 团队对 Jedis 进行了封装,独立为 spring-data-redis 项目,配合 spring 特性并集成 Jedis 的一些命令和方法。
本文重点描述集成过程,能让你迅速的通过 spring-data-redis 将 redis 集成到 spring 项目中,毕竟大家都忙的。
1. 添加项目依赖
<!--redis 缓存--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.8.4.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency>
2. 添加 spring-redis-context 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><description>redis 相关类 Spring 托管</description><!--载入 redis 配置文件--><context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/><!-- 配置 JedisPoolConfig 实例 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="${redis.maxIdle}"/><property name="maxTotal" value="${redis.maxActive}"/><property name="maxWaitMillis" value="${redis.maxWait}"/><property name="testOnBorrow" value="${redis.testOnBorrow}"/></bean><!-- 配置JedisConnectionFactory --><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.host}"/><property name="port" value="${redis.port}"/><property name="password" value="${redis.pass}"/><property name="database" value="${redis.dbIndex}"/><property name="poolConfig" ref="poolConfig"/></bean><!-- 配置RedisTemplate --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory"/></bean><!-- 配置RedisCacheManager --><bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"><constructor-arg name="redisOperations" ref="redisTemplate"/><property name="defaultExpiration" value="${redis.expiration}"/></bean><!-- 配置RedisCacheConfig --><bean id="redisCacheConfig" class="com.rambo.sdh.common.util.RedisCacheConfig"><constructor-arg ref="jedisConnectionFactory"/><constructor-arg ref="redisTemplate"/><constructor-arg ref="redisCacheManager"/></bean>
</beans>
JedisConnectionFactory 为 Jedis 连接工厂,配置由单独抽象的 JedisPoolConfig 提供。
如果你熟悉 Spring 的 JdbcTemplate 对象的话,这里大概能猜出来 RedisTemplate 的作用,RedisTemplate 对 RedisConnection 进行了封装。
提供连接管理,序列化等功能,它对 Redis 的交互进行了更高层次的抽象,极大的方便和简化了 Redis 的操作。
RedisCacheManager 做为 redis 统一的调度和管理者,有兴趣可以反编译源码看看。
继承自 org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager 并实现 org.springframework.cache.CacheManager。
3. 添加 redis.properties
#============================#
#==== Redis settings ====#
#============================#
#redis 服务器 IP
redis.host=127.0.0.1
#redis 服务器端口
redis.port=6379
#redis 密码
redis.pass=redis#2017
#redis 支持16个数据库(相当于不同用户)可以使不同的应用程序数据彼此分开同时又存储在相同的实例上
redis.dbIndex=0
#redis 缓存数据过期时间单位秒
redis.expiration=3000
#控制一个 pool 最多有多少个状态为 idle 的jedis实例
redis.maxIdle=300
#控制一个 pool 可分配多少个jedis实例
redis.maxActive=600
#当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
redis.maxWait=1000
#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true
当然配置文件你也可以硬编码到程序中,只是在参数发生改变的时候比较痛苦一点而已。
其中大部分配置项都是围绕着 jedisPool ,如果你对数据库连接池比较熟,你会发现它俩的配置项有点相似。
当系统 redis 遇到问题出现故障时,理解这里的选项是个不错的选择。
更多配置项详解:
http://www.2cto.com/database/201311/254449.html
4. 编写自定义 redis 配置类
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {protected final static Logger log = LoggerFactory.getLogger(RedisCacheConfig.class);private volatile JedisConnectionFactory mJedisConnectionFactory;private volatile RedisTemplate<String, String> mRedisTemplate;private volatile RedisCacheManager mRedisCacheManager;public RedisCacheConfig() {super();}public RedisCacheConfig(JedisConnectionFactory mJedisConnectionFactory, RedisTemplate<String, String> mRedisTemplate, RedisCacheManager mRedisCacheManager) {super();this.mJedisConnectionFactory = mJedisConnectionFactory;this.mRedisTemplate = mRedisTemplate;this.mRedisCacheManager = mRedisCacheManager;}public JedisConnectionFactory redisConnectionFactory() {return mJedisConnectionFactory;}public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {return mRedisTemplate;}public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {return mRedisCacheManager;}@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object o, Method method, Object... objects) {StringBuilder sb = new StringBuilder();sb.append(o.getClass().getName());sb.append(method.getName());for (Object obj : objects) {sb.append(obj.toString());}return sb.toString();}};}
}
该配置类继承自 org.springframework.cache.annotation.CachingConfigurerSupport 并实现 org.springframework.cache.annotation.CachingConfigurer 的方法。
通俗一点,该类告诉 spring 当前使用的缓存服务为 redis 并自定义了缓存 key 生成的规则。
5. 在你喜欢的地方进行注解缓存
img
缓存一般使用在服务层,在你想缓存的方法上面添加相应的注解即可,下面三个缓存的注解你得掌握。
- @Cacheable spring 会在其被调用后将返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。
- @CachePut 标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
- @CacheEvict 用来标注在需要清除缓存元素的方法或类上的。
当然这些注解里面还有很多其他的属性配置,配合 spring-el 表达式能做的事情还有很多,大概只有你想不到,没有做不到。
在业务规则比较复杂的情况下,缓存 key 的设计相当重要,设计出色可以使你的应用飞起来。
整个集成工作就结束了,是不是很简单,上述算是 redis 的冰山一角,还有很多像 redis 路由/分布式/集群….,有机会实践慢慢体会。
来源:http://t.cn/R37tZ3S
继承redis spring_Spring 极速集成注解 Redis 实践相关推荐
- Spring 极速集成注解 Redis 实践
Redis 做为基于内存的 Key-Value 数据库,用来做缓存服务器性价比相当高. 官方推出的面向 Java 的 Client Jedis,提供了很多接口和方法,可以让 Java 操作使用 Red ...
- Springboot集成Shiro+Redis后,@Transactional注解不起作用
为什么80%的码农都做不了架构师?>>> 使用Springboot构建 mybatis+Shiro+Redis+Druid 的前后端分离web项目, 具体可以参考博客https ...
- Redis集群技术及Codis实践
前言 诚如开篇文章所言,高效运维包括管理的专业化和技术的专业化.前两篇我们主要在说些管理相关的内容,本篇说一下技术专业化.希望读者朋友们能适应这个转换,谢谢. 互联网早在几年前就已进入Web 2.0时 ...
- 函数集成redis与Spring集成
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 redis与Spring集成比较简单,本文不触及Spring的一些基本概念,读都需先具有Spring的相干知识. 先在maven中添 ...
- spring集成redis(ehcache缓存改成redis)
1.先准备要需要的四个和redis相关的jar包 commons-pool2-2.4.2.jar.jedis-2.9.0.jar.spring-data-redis-1.8.16.RELEASE.ja ...
- redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类
1 Redis安装 redis高可用的三种常见的集群方式:redis sentinel .redis cluster(多主机+分布式).redis sharding.接下来主要介绍redis sent ...
- Spring Boot(十三):整合Redis哨兵,集群模式实践
前面的两篇文章(Redis的持久化方案, 一文掌握Redis的三种集群方案)分别介绍了Redis的持久化与集群方案 -- 包括主从复制模式.哨兵模式.Cluster模式,其中主从复制模式由于不能自动做 ...
- 都在讲Redis主从复制原理,我来讲实践总结
摘要:本文将演示主从复制如何配置.实现以及实现原理,Redis主从复制三大策略,全量复制.部分复制和立即复制. 本文分享自华为云社区<Redis主从复制实践总结>,原文作者:A梦多啦A . ...
- Docker-Compose集成编排Redis、MySQL、微服务,服务容器化
一.文章核心内容 容器化时代,看山不是山,看天全是云~ 今年的端午节粽子大家吃的是甜的还是咸的?是卷的~ 文章核心内容,可快速完成SpringBoot服务+依赖服务一起编排,完成服务容器化 docke ...
最新文章
- Tensorflow之调试(Debug) tf.py_func()
- django 学习笔记(一)
- 仿京东首页上侧导航左侧地址栏布局(1)
- “Transaction rolled back because it has been marked as rollback-only”
- 10-R语言文本挖掘tm包详解
- js return 闭包为null_js 基础知识总结
- php中 $_cfg,php 中 get_cfg_var() 与 ini_get() 的异同
- 【今晚群分享预告】 58集团监控系统实践
- 微信小程序解密encryptedData 报错:pad block corrupted 解决方法
- IDEA 启动本地 Flink Web UI
- AWR实战分析之---- PX Deq Credit: send blkd
- 西科大 软件体系结构内容总结
- 分享62个PHP源码,总有一款适合您
- Unity 打包和切换平台|Build Settings窗口介绍
- python多元非线性回归_Python利用神经网络解决非线性回归问题实例详解
- python中文居中对齐处理
- 关系型数据库--关系代数
- 今日金融词汇---存货周转天数
- What is SVM algorithm
- (素材源码) 猫猫学IOS(五)UI之360等下载管理器九宫格UI
热门文章
- 【Flink】Flink 任务实时监控
- 【Flink】Flink 流计算 容错 source节点进行数据容错
- 【Elasticsearch】使用 Elasticsearch 的 44 条建议
- 【kafka】kafka 如何开启 kafka.consumer的监控指标项
- 【Java】Java 线程池 8 大拒绝策略
- Spark : ExitCodeException exitCode=15,exitCode=13
- Docker : 获取运行容器的IP地址
- 关于Maven本地仓库中存在依赖时,重新下载was cached in the local repository错误
- php 内置mail 包,PHP使用pear自带的mail类库发邮件的方法
- java list map嵌套_Java 集合-Map集合嵌套 的遍历四种方式