spring boot 2.0.4 Redis缓存配置
spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory; import java.time.Duration;
import java.util.HashMap;
import java.util.Map; /**
* Redis缓存配置类
*
* @author lee
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport { //缓存管理器
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//user信息缓存配置
RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)).disableCachingNullValues().prefixKeysWith("user");
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("user", userCacheConfiguration);
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); //设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
return cacheManager;
}
}
包装RedisTemplate,把常用操作写一个redis工具类
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service
public class RedisService { @Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate; public <T> T getJson2Object(String key, Class<T> clazz) { return JSON.parseObject(stringRedisTemplate.opsForValue().get(key), clazz);
} public <T> void setObject(String key, T clazz) {
redisTemplate.opsForValue().set(key, clazz);
} /**
* 设置对象,含有效期,单位为秒
*/
public <T> void setObject(String key, T clazz, long timeout) {
redisTemplate.opsForValue().set(key, clazz, timeout, TimeUnit.SECONDS);
} /**
* 设置对象,含有效期,单位可以自定义
*/
public <T> void setObject(String key, T clazz, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, clazz, timeout, unit); } @SuppressWarnings("unchecked")
public <T> T getObject(String key, Class<T> clazz) { return (T) redisTemplate.opsForValue().get(key); } public String getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
} public void setString(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
}
spring boot 2.0.4 Redis缓存配置的更多相关文章
- Spring Boot不同版本整合Redis的配置
1. Spring Boot为1.4及其他低版本 1.1 POM.XML配置 <!--引入 spring-boot-starter-redis(1.4版本前)--> <depende ...
- 搞懂分布式技术14:Spring Boot使用注解集成Redis缓存
本文内容参考网络,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutor ...
- spring boot 2.0 ribbon 负载均衡配置
1.pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId ...
- spring boot 2.0+ 错误页面配置
如果访问了错误的路径,或者后台报错 如果没有一个统一的页面! 或者说页面上展示一堆报错信息,既影响美观,又对用户不友好! 那么如何配置? 定义 ErrorPageConfig,配置错误状态与对应访问路 ...
- Spring Boot 2.0 迁移指南
:Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- Spring Boot 2.0 升级指南
Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...
- spring boot 2.0.0 + shiro + redis实现前后端分离的项目
简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大 ...
随机推荐
- 闲话ACES(修订)
最近身边的人见面就聊ACES,ACES俨然已经是行业热点了. ACES的确更高效的解决了色彩一致性的问题,这是符合历史进程的(+1s),无疑值得肯定.但由于色彩管理意识不强,关于ACES的认识就存在着 ...
- PHP swoole实现redis订阅和发布
前戏:实现用户下单,服务器通知后台接收订单...类似美团外卖 1.首先要实现一个订阅程序 $result = $client->connect('127.0.0.1', 6379, functi ...
- h5课件是什么?h5(html5)怎样实现交互动画开发?-----浅谈h5交互动画课件的优势
目前很多交互课件,尤其幼儿类的交互课件以动画和交互相结合的类型居多,越来越多的教育机构发现了这种课件对于幼儿的吸引力远大于其他类型的课件,随着flash逐渐被市场淘汰,动画和交互相结合的html5跨平 ...
- OPPO R6007在哪里打开usb调试模式的完美流程
当我们使用Pc接通安卓手机的时候,如果手机没有开启USB开发者调试模式,Pc则无法成功读到我们的手机,遇到此种情况我们需要想方设法将手机的USB开发者调试模式打开,以下内容我们讲解OPPO R6007 ...
- xamarin android 开发
开始环境vs2017 直接创建android 项目,左边是android studio 的项目目录 右边是vs创建的android 项目目录 结构基本相同,有res对应的Resources文件 加载 ...
- OkGo3.0 --真实项目使用和二次封装(转)
转载:https://blog.csdn.net/jiushiwo12340/article/details/79011480 11.OkGo3.0真实项目使用和二次封装: ==== 11.OkG ...
- spring入门-注解的使用
说明: 使用注解和使用配置文件实现的功能是一样的,都是为了解耦,但是配置文件语法属于非编程语言法语,无法调试,难以定位bug,使用注解更易定位问题. 配置步骤 编译器必须先安装了STS插件 第一步 导 ...
- SQL FOR JSON PATH 返回 json
--直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...
- Could not initialize class utils.JdbcUtils
今天用JdbcUtils时出现了一个问题,被困扰了一晚上.从网上找的原因,说什么url错了,版本不一致等等都不能解决我的问题, 我写好了一个JdbcUtils准备测试,发现从后台用Dao测试完全没问题 ...
- git push时报错refusing to merge unrelated histories
1. 删除本地项目中的.git目录,然后向远程仓库提交代码的时候,重新配置后再次提交.会有冲突. 解决方式: git remote add origin [//your giturl] git pul ...