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,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大 ...
随机推荐
- 【转】计算机信息系统安全保护等级划分准则(GB 17859-1999)
计算机信息系统安全保护等级划分准则(GB 17859-1999) 概述 计算机信息系统安全保护等级划分准则(GB 17859-1999) 1 范围 本标准规定了计算机系统安全保护能力的五个等级,即: ...
- VS2015 使用GIT同步到github
https://www.cnblogs.com/newP/p/5732431.html(参考) 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Meger) 获取(Fetch):从 ...
- 存储专栏:一句话说清RAID2.0
今天,西瓜哥来谈谈高端存储的一股势力,RAID 2.0,最近被华为HVS搞得风生水起,神奇的让人摸不着头脑.我还是从一个高端存储的江湖说起吧. 据说很久很久以前(别扔臭鸡蛋,讲故事都是这样的…),L ...
- 一个简单的python爬虫程序
python|网络爬虫 概述 这是一个简单的python爬虫程序,仅用作技术学习与交流,主要是通过一个简单的实际案例来对网络爬虫有个基础的认识. 什么是网络爬虫 简单的讲,网络爬虫就是模拟人访问web ...
- C语言volatile关键字的用法
volatile关键字的意义在于让被关键字修饰的变量每次使用时都重新去主内存里读取变量,而不是从高速缓存去读取. int a = 1; 普通变量定义之后,若要使用它,都会 先把它的值从主内存拷贝到高速 ...
- [转]TopShelf 用法
static void Main(string[] args) { { x.Service<SyncData>(s => { s.ConstructUsing(name => ...
- oracle dg 报错提示 涉及硬盘错误
###oracle dg 报错提示 涉及硬盘错误 Dec 23 03:28:01 xhisdg rsyslogd: [origin software="rsyslogd" swVe ...
- 【读后感】读《漫谈“大学生的四个learn”》之后有感
在看完<许纪霖教授在上海财经大学演讲——漫谈“大学生的四个Learn”>(下问简称<四l>)之后突然有点话想说.其实我对这篇文章有感的,所以想写这篇文章来说说看,我对四个lea ...
- Linux裸设备管理详解--
裸设备概述 裸设备:也叫裸分区(原始分区),是一种没有经过格式化,不被Unix/Linux通过文件系统来读取的特殊字符设备.裸设备可以绑定一个分区,也可以绑定一个磁盘.字符设备:对字符设备的读写不需要 ...
- 如何将composer设置为全局变量?
全局安装是将 Composer 安装到系统环境变量 PATH 所包含的路径下面,然后就能够在命令行窗口中直接执行 composer 命令了. Mac 或 Linux 系统: 打开命令行窗口并执行如下命 ...