当前位置: 首页 > news >正文

springboot配置redis、Spring cache

1.Jedis库

依赖库

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.0.2</version>
</dependency>

使用案例:

 @Testpublic void jedis(){Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.set("name","yi");jedis.close();}

2.springboot官方编写的整合库

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>3.1.5</version></dependency>

使用案例:

 	@Autowiredprivate RedisTemplate redisTemplate;//通过自动注入@Testpublic void t(){//使用opsFor系列方法获取xxxOperations对象ValueOperations valueOperations = redisTemplate.opsForValue();valueOperations.set("name3","yi");HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("student","name","yi");hashOperations.put("student","age","18");String age = (String) hashOperations.get("student", "age");}

RedisTemplate默认使用 JdkSerializationRedisSerializer 进行序列化。
序列化的key结果为:
在这里插入图片描述

为什么是这样一串奇怪的 16 进制? ObjectOutputStream#writeString(String str, boolean unshared) 实际就是标志位 + 字符串长度 + 字符串内容

如果需要redis中设定的key值与我们在程序中设定的值相同,则需要改变序列化的方式,即自定义RedisTemplate。
创建一个配置类来定义RedisTemplate Bean,设置序列化方式 StringRedisSerializer。

@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {@BeanRedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){RedisTemplate<Object,Object> redisTemplate = new RedisTemplate();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}
}

3.Spring cache

通过注解来新增/删除缓存。

配置信息:

引入redis架包:

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>3.1.5</version></dependency>

指定缓存的类型为redis,并配置redis IP端口号

#application.yml:
spring:redis:host: 127.0.0.1port: 6379database: 0cache:type: redis

操作步骤

  1. 在启动类上加入@EnableCaching,开启缓存功能
  2. 注入CacheManager,选择缓存实现方法
    @Autowired
    private CacheManager cacheManager;
  3. 在需要cache操作的方法上加上相关注解

注解

@CachePut

使用 @CachePut 注解就能够将方法的返回值放到缓存中,使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个@CachePut(value=“my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@CachePut(value=“testcache”, key=“#user.id”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CachePut(value=“testcache”, condition=“#userName.length()>2”)
@CacheEvict

使用 @CacheEvict 注解就能够将一条或多条数据从缓存中删除,使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个@CacheEvict(value=“my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@CacheEvict(value=“testcache”, key=“#id”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行清空缓存@CacheEvict(value=“testcache”, condition=“#userName.length()>2”)
allEntries是否清空所有缓存内容,默认为 false,如果指定为 true,则方法调用后将立即清空所有缓存@CachEvict(value=“testcache”, allEntries=true)
beforeInvocation是否在方法执行前就清空,默认为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存@CachEvict(value=“testcache”,beforeInvocation=true)
@Cacheable

使用 @Cacheable 注解,在方法执行前 Spring 会先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中。使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个例如: @Cacheable(value=“mycache”) @Cacheable(value={“cache1”, “cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@Cacheable(value=“testcache”, key=“#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@Cacheable(value=“testcache”, condition=“#userName.length()>2”)
unless与 condition 相反,满足条件的时候则不缓存数据@Cacheable(value=“testcache”, unless=“#result == null”)
http://www.lryc.cn/news/206998.html

相关文章:

  • 悟空crm安装搭建 报错[0] RedisException in Redis.php line 56问题处理办法
  • Ubuntu22.04 交叉编译阿里oss c-sdk
  • arch linux 安装 vsftpd 配置虚拟用户
  • Django的查询所有,根据用户名查询,增加用户操作
  • 【adb】adb相关命令行及adb传输文件权限问题 remote couldn‘t create file: Read-only file system
  • 基于物联网云平台的分布式光伏监控系统的设计与实现
  • 初识Node.js开发
  • 【Python入门教程】基于OpenCV视频分解成图片+图片组合成视频(视频抽帧组帧)
  • 微前端qiankun接入Vue和React项目
  • 提升技能,一触即达!全新在线题库微信小程序等你来挑战!
  • 语雀P0级故障复盘,有9个字亮了
  • 在 openresty 中使用 capnp lua 库
  • 私藏小技巧:让微信朋友圈营销方便化的小窍门!
  • Centos使用tomcat部署jenkins
  • uni-app打包apk实现自动更新
  • SRS srs-bench
  • HackTheBox-Starting Point--Tier 1---Appointment
  • 【工具】Java请求带http重定向的地址 自动进行重定向
  • 接口自动化测试方案
  • TikTok文化探索:热议时事与社会话题
  • springboot操作nosql的mongodb,或者是如何在mongodb官网创建服务器并进行操作
  • QWEN technical report
  • 提升MODBUS-RTU通信数据刷新速度的常用方法
  • PyTorch 与 TensorFlow:机器学习框架之战
  • 超简单理解冒泡排序
  • 模拟IC设计工程师成长日记
  • 修炼k8s+flink+hdfs+dlink(六:学习namespace,service)
  • 法语导游就业前景如何?
  • iOS自动混淆测试处理笔记
  • C51--单片机中断