首页 > 其他分享 >SpringCache-KeyGenerator

SpringCache-KeyGenerator

时间:2023-06-14 19:35:59浏览次数:35  
标签:return KeyGenerator Object keyGenerator Override SpringCache public

实际项目中,如果使用统一的key的生成方式,可以自定义KeyGenerator。
参考如下代码:

点击查看代码
    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return target.getClass().getSimpleName() + "_" + method.getName() + "_" + StringUtils.arrayToDelimitedString(params, "_");
            }
        };
    }

定义好KeyGenerator之后,就可以在注解中进行引用,如下:

点击查看代码
    @Override
    @Cacheable(value = {"query1"},keyGenerator = "keyGenerator",cacheManager = "cacheManagerTTL")
    public TestUser testCacheQuery(Integer id) {
        return testUserMapper.selectById(id);
    }

调用方法后可以查看redis的key:

标签:return,KeyGenerator,Object,keyGenerator,Override,SpringCache,public
From: https://www.cnblogs.com/gengone/p/17481158.html

相关文章

  • SpringCache的常用注解-@CacheEvit
    从缓存中移除相应的数据,触发缓存删除操作value:缓存名称key:缓存的key规则,可以使用SpringEL,默认是方法参数组合beforeInvocation:缓存的清除在方法调用之前执行还是之后执行,默认为false参考如下代码:点击查看代码@Override@CacheEvict(value={"query1"},key=......
  • SpringCache的引入
    SpringCache是Spring提供的一套的缓存解决方案,它不是具体的缓存实现,提供了一整套的配置、接口、注解等规范,用来整合当下流行的多种缓存产品。SpringCache的引入点击查看代码<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-s......
  • springcache + redis 配置支持缓存ttl失效
    packagetst;importcom.fasterxml.jackson.annotation.JsonAutoDetect;importcom.fasterxml.jackson.annotation.JsonTypeInfo;importcom.fasterxml.jackson.annotation.PropertyAccessor;importcom.fasterxml.jackson.databind.DeserializationFeature;importcom.......
  • 使用 SpringCache 简化缓存代码实现
    SpriingCache实现了基于注解的缓存功能,只需要在方法上添加注解即可实现常用的缓存功能,大大简化了的业务代码的实现。SpringCache默认集成于SpringContext中,这意味着对于使用SpringBoot框架来说,不需要引入额外的jar包即可使用。SpringCache通过CacheManager接口来统一......
  • redis-SpringCache-简介
    整合&体验@Cacheable引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>......
  • springCache整合redis详细讲解和配置
    SpringCache的简介缓存,就是将数据从数据库等数据来源获取数据,将数据缓存在内存或其他设备如Redis中,为了二次查询能够快速高效的响应结果.SpringCache是3.1开始提供,......
  • 79、缓存---SpringCache---原理与不足
    /***Spring-Cache的不足:*1)读模式:*缓存穿透:查询一个null的数据;====》解决:缓存空数据:spring.cache.redis.cache-null-values=true*缓存击穿:大量并发进......
  • 78、缓存---SpringCache---@CacheEvict
    @CacheEvict(value={"catagory"},key="'OneLevelCategory'")------失效模式例如,当我们修改菜单时,就会删除catagory缓存中的OneLevelCategory缓存如果我们修改......
  • 77、缓存---SpringCache---整合&体验@Cacheable
    1、导入依赖<!--缓存--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache<......
  • Redis做缓存和SpringCache缓存
    记录一下Redis做缓存和SpringCache缓存的区别1.Redis做缓存的话,相当于是一个第三方缓存,所以项目重启之后缓存数据还是存在的2.SpringCache作缓存的话是建立在JVM上的,所以......