SpringBoot Redis 2.0.x
- redis的安装
在笔者之前的文章中有介绍redis的安装,不会的可以去看 笔者之前写的文章redis安装 - 完成安装后如果不熟悉redis的操作,redis官方文档也有基本操作指南,redis基本操作,如果觉得没问题了就可以开始对redis的整合
- maven安装依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis自动会吧cache的依赖带过来,所有不用配置,如图

- 启动类增加@EnableCaching 注解
@SpringBootApplication
@MapperScan("com.tanoak.mapper")
@EnableCaching
public class BootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(BootRedisApplication.class, args);
}
}
- service层增加@Cacheable 注解
@Override
@Cacheable(cacheNames= "tea")
public Teacher getTeaById(Integer id) {
logger.info("进行查询实体 ID为"+id);
return teacherMapper.getTeaById(id) ;
}
- controller 查询
@GetMapping("/tea/{id}")
public Teacher getTea(@PathVariable("id")Integer id){
return teacherService.getTeaById(id) ;
}
RedisCacheManager 配置
在SpringBoot2.x中,移除了1.x中的配置,因此要配置Json序列化与1.x的差别很大,看代码
@Configuration
@EnableCaching
public class MyRedisConfig extends CachingConfigurerSupport {
/*
*自定义键生成策略
*/
@Bean
public KeyGenerator KeyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
};
}
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
//设置默认超过期时间是30秒
).entryTtl(Duration.ofMinutes(30));
return redisCacheConfiguration;
}
}


没有打印sql,说明缓存成功,与redis集成就完成了
SpringBoot Redis 2.0.x的更多相关文章
- springboot+redis
上篇整合了DB层,现在开始整合缓存层,使用redis. springboot驱动注解,使用spring注入JedisPool便可封装自己的redis工具类. package hello.configu ...
- Springboot+redis 整合
运行环境: JDK1.7. SpringBoot1.4.7 redis3.0.4 1.生成Springboot项目,分别添加web,redis依赖,具体的maven依赖如下 <dependenc ...
- SpringBoot+Redis环境搭建
写在正文前的絮叨: 其实这个环境的搭建是很简单的,照着官网给的说明很快就可以搭建测试出来.为什么又要写出来呢?只是为了记录.保留.分享这其中遇到的坑. 这个环境之前在架构一个简单系统时,也曾经搭建过, ...
- 补习系列(14)-springboot redis 整合-数据读写
目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...
- 补习系列(13)-springboot redis 与发布订阅
目录 一.订阅发布 常见应用 二.Redis 与订阅发布 三.SpringBoot 与订阅发布 A. 消息模型 B. 序列化 C. 发布消息 D. 接收消息 小结 一.订阅发布 订阅发布是一种常见的设 ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- springboot+redis实现缓存数据
在当前互联网环境下,缓存随处可见,利用缓存可以很好的提升系统性能,特别是对于查询操作,可以有效的减少数据库压力,Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存 ...
随机推荐
- centos7.0修改网卡名为ethx
场景: 由于默认的centos7网卡名对于大多数习惯于原先的命名方式的人而言是一种折磨,因此我们需要让他恢复正常! 编辑/etc/sysconfig/grub文件 vim /etc/sysconfig ...
- linux 详解useradd 命令基本用法
linux 详解useradd 命令基本用法 时间:2019-03-24 本文章向大家介绍linux 详解useradd 命令基本用法,主要包括linux 详解useradd 命令基本用法使用实例.应 ...
- nosql数据库之Redis集群
Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 ...
- 如何在idea中将项目生成API文档(超详细)(Day_32)
1.打开要生成API文档的项目,点击菜单栏中的Tools工具,选择Generate JavaDoc 2.打开如下所示的Specify Generate JavaDoc Scope 界面 3.解释下Ot ...
- C语言编程 菜鸟练习100题(51-60)
[练习51]矩阵转置 0. 题目: 矩阵的转置 1. 分析: 练习使用 for 循环嵌套,多维数组的表达. 2. 程序: #include <stdio.h> int main() { i ...
- VMware vSphere 7.0 Update 2 发布 - 数据中心虚拟化和 Kubernetes 云原生应用引擎
2021 年 3 月 9 日,VMware 发布了 vSphere 7 Update 2.它可以通过 VMware Customer Connect 和 vSphere Lifecycle Manag ...
- 关于Linux的一些基础命令
今天学习scala语言,在linux系统上运行,发现对Linux的命令不太熟悉,为了熟悉掌握,也便于查询,这些命令主要是为了收藏备用,,希望能帮助到大家 linux20个常用命令是: 1.显示日期的指 ...
- harbor搭建及使用
harbor搭建及使用 1 系统及软件版本 1.1 系统版本 # uname -a Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP ...
- grasshopper之python电池执行逻辑
在grasshopper中,需要导入的包虽然不多,但是相当绕人,所要实现的操作往往找不到,暂时做个分类. 双击输入 python 电池: # 导入rhino 包 import Rhino #Rhino ...
- 树莓派3B+常用操作
安装Docker 替换成清华源 清华源地址https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/ echo "deb [arch=armhf ...