前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Spring Boot 整合 Redis

Spring Boot 整合 Redis

原创
作者头像
FHAdmin
发布于 2022-03-03 01:27:44
发布于 2022-03-03 01:27:44
1K00
代码可运行
举报
文章被收录于专栏:FHADMINFHADMIN
运行总次数:0
代码可运行

准备工作(在项目中一般是要写到公共服务中)

导入依赖包

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
 
<!-- spring2.X集成redis所需common-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.0</version>
</dependency>

在配置文件中配置Redis

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
spring.redis.timeout=1800000
 
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
# 最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0

创建redis缓存配置类,配置插件(较为固定)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.xsha.servicebase;
 
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.time.Duration;
 
/** 
* 说明:redis配置类 
* 作者:FH Admin 
* from:fhadmin.cn 
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
 
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer 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);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
 
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer 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);
        // 配置序列化(解决乱码的问题),过期时间600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

因为首页接口对应获取的首页数据变化不大,但访问量较大,所以就有必要将首页接口数据缓存到redis缓存中,减少数据库压力和提高访问速度

Spring Boot缓存注解

缓存@Cacheable(一般用在查询的方法上)

根据方法对其返回结果进行缓存,下次请求时,如果缓存存在,则直接读取缓存数据;如果不存在,则执行方法,并把返回的结果存入缓存中

属性及其描述

value:缓存名称,必填,指定缓存存放在哪块命名空间

cacheNames:与value差不多,二选一即可

key:可选属性,可以使用SpEL标签自定义缓存的key

缓存@CachePut(一般用在新增的方法上)

使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库

属性及其描述

value:缓存名称,必填,指定缓存存放在哪块命名空间

cacheNames:与value差不多,二选一即可

key:可选属性,可以使用SpEL标签自定义缓存的key

缓存@CacheEvict(一般用在更新或者删除的方法上)

使用该注解标志的方法,会清空指定的缓存

属性及其描述

value:缓存名称,必填,指定缓存存放在哪块命名空间

cacheNames:与value差不多,二选一即可

key:可选属性,可以使用SpEL标签自定义缓存的key

allEntries:是否清空所有缓存,默认为false,如果指定为true,则方法调用后将立即清空所有的缓存

beforeInvocation:是否在方法执行前就清空,默认为false,如果指定为true,则在方法执行前就会清空缓存

注意事项: 属性key的值需要再价格单引号,不然会报错。如@Cacheable(key="'keyName'", value="valueName")

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Spring Boot整合 NoSQL 数据库 Redis
在日常的开发中,除了使用Spring Boot这个企业级快速构建项目的框架之外,随着业务数据量的大幅度增加,对元数据库造成的压力成倍剧增。在此背景下,Redis这个NoSQL数据库已然整个项目架构中的不可或缺的一部分,懂得如何Spring Boot整合 Redis,是当今开发人员必备的一项技能,接下来对整合步骤进行详细说明。
百思不得小赵
2022/12/01
4520
Spring Boot整合 NoSQL 数据库 Redis
springboot整合redis进行缓存的使用
看过我的文章的都应该有所了解如何使用docker方式进行redis环境的搭建过程,想要了解的可以看下历史文章。今天我们想要分享的就是如何使用redis进行缓存的使用。
码农王同学
2019/11/28
5050
springboot整合redis进行缓存的使用
springboot2.0+redis 二级缓存 乱码问题
SpringCacheRedisConfig import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.
用户5899361
2020/12/07
4891
springboot缓存之自定义CacheManager
先要说明的是上一节springboot缓存之使用redis作为缓存管理是springboot的旧版本了,最新的springboot2.x已经不这么用了,而且缓存注解一般用于service上,而不是controller上。百度了下整体代码是这样的:MyRedisConfig.java
西西嘛呦
2020/08/26
3.5K0
SpringBoot中Redis的基础使用
然后在根包下创建一个service的文件夹加,然后在里面增加redis文件夹,redis文件夹里编写redis的基础操作函数。
Kiba518
2023/07/09
3290
SpringBoot中Redis的基础使用
springboot配置RedisTemplate和RedisCacheManager
1、项目目录结构: 2、application.properties配置文件: 我这里redis连的是虚拟机上面的,你改下host地址就行 spring.datasource.url=jdbc:mysql://localhost:3306/spring_cache spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jd
别团等shy哥发育
2023/02/25
1.2K0
springboot配置RedisTemplate和RedisCacheManager
从零搭建Spring Boot脚手架(6):整合Redis作为缓存
上一文我们整合了Mybatis Plus,今天我们会把缓存也集成进来。缓存是一个系统应用必备的一种功能,除了在减轻数据库的压力之外。还在存储一些短时效的数据场景中发挥着重大作用,比如存储用户Token、短信验证码等等,目前缓存的选型还是比较多的,EHCACHE、HAZELCAST、CAFFEINE、COUCHBASE以及本文要整合的REDIS。接下来我们将会在kono脚手架项目中集成Spring Cache以及Redis。
码农小胖哥
2020/08/25
9760
从零搭建Spring Boot脚手架(6):整合Redis作为缓存
SpringCache基本配置类
benym
2024/05/18
960
springboot集成了哪些框架_redis java客户端
使用注解进行缓存操作涉及CacheManage RedisCacheManager源码
全栈程序员站长
2022/11/08
1.2K0
springboot实战之nosql整合(redis篇)
关于redis的内容,我之前已经分享过了很多了,今天这篇算是为了springboot nosql整合中的凑数篇吧,哈哈,虽然这么说,但如果点进来了,蛮看下,说不定会有一些新发现
lyb-geek
2019/10/10
8330
你知道如何在springboot中使用redis吗
特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce
用户2038589
2019/06/02
1.2K0
3 Springboot中使用redis,redis自动缓存异常处理
在上一篇中,提到了使用配置文件来定义连接信息,由于前面讲的都是框架自动使用redis缓存数据,那么如果出现了异常又该怎么处理?
天涯泪小武
2019/01/17
2K0
Spring Boot 结合 Redis 的序列化配置
默认情况下,Spring 为我们提供了一个 RedisTemplate 来进行对 Redis 的操作,但是 RedisTemplate 默认配置的是使用Java本机序列化。
Vincent-yuan
2021/10/28
4.1K0
Spring Boot 结合 Redis 的序列化配置
2 Springboot中使用redis,配置redis的key value生成策略
上一篇里讲过了redis在spring boot中的简单使用,对于单个对象的增删改查的默认操作。
天涯泪小武
2019/01/17
4K2
SpringBoot整合redis
前言 使用注解实现Redis缓存功能的具体代码 1 引入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2 添加配置 spring.redis.host=xxxxxxx spring.redis.port=6379 // 如
在水一方
2022/10/04
3000
SpringBoot集成Redis缓存
https://www.cnblogs.com/noneplus/p/11532065.html
Noneplus
2019/09/24
5960
SpringBoot集成Redis缓存
Springboot中的redis配置类
简单记录一下 首先依赖: <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X集成redis所需common-pool2-->
Tom2Code
2022/11/21
6790
Springboot整合redis +cache
redis(Remote Dictionary Server 远程数据服务) 是一个高性能的key-value数据库。
smallmayi
2022/05/12
8120
Springboot整合redis +cache
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
    小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗,还能怎么出来?”小白:“为什么躺着出来?”小明:“5米深的坑,还没有水,跳下去不死就很幸运了,残是肯定会残的,不躺着出来,那能怎么出来?”小白:“假设没死也没残呢?”小明:“你当我超人了? 那也简单,把脑子里的水放出来就可以漂出来了。”小白:“你脑子里有这么多水吗?”小明:“我脑子里没那么多水我跳下去干嘛?” 
青石路
2018/12/06
2.5K0
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
SpringBoot 整合 Redis
因为 Redis 有 这5种基本数据结构类型分别为:String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。
收心
2022/01/17
3760
SpringBoot 整合 Redis
相关推荐
Spring Boot整合 NoSQL 数据库 Redis
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文