redis使用

Spring Cache 是Spring - context-xxx.jar中提供的功能
在包含了Spring - context-xxx.jar的Spring Boot项目中,在启动类中添加@EnableCaching注解,即可开启缓存功能。默认Spring Cache是不开启。
添加Spring-boot-starter-web会包含spring-context。就可以使用Spring Cache了。
spring-boot-start-data-redis 添加后会有Redis配置环境,
新建application.yml。配置redis即可。

spring:
  redis:
    host: 192.168.8.130

启动类:
@SpringBootApplication
@EnableCaching
public class DemoApplication

redis配置类

@Configuration
public class RedisConfig {

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //缓存配置对象
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();

        redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟
                .disableCachingNullValues()             //如果是空值,不缓存
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))         //设置key序列化器
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));  //设置value序列化器

        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }
}

@Cacheable 的使用

缓存在方法上

每个方法单独配置key前缀适用于整个类中方法的前缀不统一的情况。
@Cacheable表示要对方法返回值进行缓存。缓存时key的名称为cacheName::key
cacheNames : 配置key的前缀
key:配置key的后缀。里面字符串要使用单引号

@Service
public class DemoServiceImpl implements DemoService {
    @Override
    // 固定字符串需要使用单引号
    @Cacheable(key = "'demo'",cacheNames = "com.bjsxt")
    public String demo() {
        System.out.println("demo方法被执行");
        return "demo-result";
    }
}

## 缓存在类上
一般情况下一个类中方法的对应key的前缀都是一样,可以把前缀cacheNames提出到类上,统一配置。

```java
@Service
@CacheConfig(cacheNames = "com.bjsxt")
public class DemoServiceImpl implements DemoService {
    @Override
    // 固定字符串需要使用单引号
    @Cacheable(key = "'demo'")
    public String demo() {
        System.out.println("demo方法被执行");
        return "demo-result";
    }
}

带有参数的方法缓存

通过#参数名可以获取到方法参数。key中内容Sprng EL,既然是表达式字符串要用单引号,没有被单引号包含的内容都表示变量


```java
 @Cacheable(key = "'selectById'+#id")
    public String selectById(Long id) {
        System.out.println("执行了selectById:"+id);
        return "selectById"+id;
    }
}

@Cacheable 的 condition和unless属性

@Cacheable中
condition是普通条件。如果条件成立则进行缓存。此判断为进入到方法体之前的判断,所以#result不允许用在这里。如果在condition中出现#result会导致条件恒不成立,不进行缓存。
unless是方法执行完成后的条件,当符合条件不被缓存。注意:#result只能写在这个属性中。多用在返回结果为null时不缓存效果。

@Cacheable(key = "'selectById2'+#id",unless= "#result == null ")
public String selectById2(Long id){
    System.out.println("执行selectById2");
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值