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,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大 ...
随机推荐
- cmake add_custom_command 使用
cmake add_custom_command 使用 今天整理编译工程,想在编译工程前面用tolua生成c文件, 使用命令add_custom_command后,附加的命令并不执行,如下: add_ ...
- Linux基础入门-环境变量与文件查找
一.环境变量: Shell中的变量也有不同的数据类型,不用专门指定类型名,可以参与运算,有作用域限制. declare tmp 创建一个变量 可以不用专门声明,可以即用即创建 tmp=shiyanlo ...
- 内存问题-JVM调优思路
通常,内存的问题就是 GC 的问题,因为 Java 的内存由 GC 管理.有2种情况,一种是内存溢出了,一种是内存没有溢出,但 GC 不健康. 内存溢出的情况可以通过加上 -XX:+HeapDumpO ...
- Mysql中判断一个点是否落在多边形内
关于地理空间数据,经常需要处理两个空间数据的关联关系.有很多种方法可以处理,通过编写程序算法,或者是调用数据库中对应的function.在mysql数据库中,https://dev.mysql.com ...
- Axure RP 7 8
Axure RP 7.0 用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+tqEV4LG Axur ...
- 京东饭粒捡漏V1.0.8
20180617 更新 V1.0.81.捡漏策略更新:自动检测商品,有货后自动下单:2.加车自动使用最优满减券组合: 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用 ...
- apache+php+mysql安装与使用
偷个懒,用的系统自带的apache和php apache安装与使用 Mac自带apache默认路径 主程序 /usr/sbin/httpd 模块 /usr/libexec/apache2 配置 /et ...
- Spring Cloud Turbine微服务集群实时监控
本文代码下载地址: https://gitlab.com/mySpringCloud/turbine SpringBoot版本:1.5.9.RELEASE (稳定版) SpringCloud版本:Ed ...
- [转]Github 下载指定文件夹
来自:https://blog.csdn.net/qq_35860352/article/details/80313078 操作步骤 step1:转换链接地址 点开”/examples”子文件,复制浏 ...
- 数据库-Oracle的使用
数据库的作用不言而喻,Oracle 作为一个主流的数据库,了解更是需要的 安装好oracle之后,登录初始账号,一般而言呢,我就新建一个用户, --创建新用户,用户名为JohnWu ,密码为 root ...