当前位置:首页 > 编程笔记 > 正文
已解决

springboot配置redis、Spring cache

来自网友在路上 153853提问 提问时间:2023-10-26 20:01:43阅读次数: 53

最佳答案 问答题库538位专家为你答疑解惑

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 注解就能够将方法的返回值放到缓存中,使用该注解时可以指定以下几个参数:

参数解释examplevalue缓存的名称,每个缓存名称下可以有多个 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 注解就能够将一条或多条数据从缓存中删除,使用该注解时可以指定以下几个参数:

参数解释examplevalue缓存的名称,每个缓存名称下可以有多个 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 会先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中。使用该注解时可以指定以下几个参数:

参数解释examplevalue缓存的名称,每个缓存名称下可以有多个 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”)
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"springboot配置redis、Spring cache":http://eshow365.cn/6-25362-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!