springboot配置redis、Spring cache

1.Jedis库

依赖库

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

使用案例:

 @Test
    public 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>

使用案例:

 	@Autowired
    private RedisTemplate redisTemplate;//通过自动注入
    @Test
    public 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 {
    @Bean
    RedisTemplate<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.1
    port: 6379
    database: 0
   
  cache:
    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”)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值