栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring缓存注解源码分析:@Cacheable @CacheEvict @CachePut

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring缓存注解源码分析:@Cacheable @CacheEvict @CachePut

解决存入redis乱码

package com.example.demo.kang.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.*;


@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration
                .defaultCacheConfig();

        config = config.serializevaluesWith(RedisSerializationContext.SerializationPair
                .fromSerializer(new GenericJackson2JsonRedisSerializer()));

        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}

测试:

//    当方法有被调用时,先检查cache中有没有针对该方法相同参数的调用发生过,如果有,从cache中查询并返回结果。如果没有,则执行具体的方法逻辑,并把结果缓存到cache中。
    @RequestMapping(value = "testCache",method = RequestMethod.GET)
    @Cacheable(cacheNames="kang_cache_test",key = "#s")
    public String getText(@RequestParam(value = "s") String s) {
        return "1111";
    }
  //清除缓存
    @RequestMapping(value = "deleteCache",method = RequestMethod.GET)
    @CacheEvict(cacheNames="kang_cache_test",key = "#s")
    public Boolean deteteText(@RequestParam(value = "s") String s) {
        return true;
    }
// 标注在方法上,先执行方法,再用方法返回的值来更新缓存内容
    @RequestMapping(value = "putCache",method = RequestMethod.GET)
    @CachePut(cacheNames="kang_cache_test",key = "#s")
    public String putText(@RequestParam(value = "s") String s) {
        return "2222";
    }

//    @Caching
//    复杂的cache配置,可以在里面配置上面的几个注解
//
//  @CacheConfig
//    标注在类上,对类中的缓存操作作统一配置

关键词:
cacheNames/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,是数组的方式,可以指定多个缓存;
key:缓存数据使用的key;可以用它来指定。默认是使用方法参数的值 1-方法的返回值, 编写SpEL; #i d;参数id的值 #a0 #p0 #root.args[0] getEmp[2]
keyGenerator:key的生成器;可以自己指定key的生成器的组件id,key/keyGenerator:二选一使用;
cacheManager:指定缓存管理器;或者cacheResolver指定获取解析器
condition:指定符合条件的情况下才缓存; condition = “#a0>1”:第一个参数的值》1的时候才进行缓存
unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;可以获取到结果进行判断

源码:为什么能定位到redis中

redisCacheConfiguration被加载
里面加载 了cacheManager


@cache拦截器中拦截,获取对应manager,就获得了redis的配置

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号