首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

@Cacheablekey的SPEL表达式实现

Spring中的@Cacheable注解相信大家都有用过,其key属性是支持SPEL表达式的,像key="#root.args[0]"取到的就是方法第一个入参,这极大地简化了缓存key的配置。...,获取真正存入缓存中的key值        String key = parseSpelKey(cacheable, context);        //读取缓存        Object value...//设置缓存            setCache(cacheablekey, value);        }        return value;    }    private String... parseSpelKey(CacheableTtl cacheable, StandardEvaluationContext context) {        String keySpel = cacheable.key... = expression.getValue(context, String.class);        return key;    }}拦截器设置好后,在Controller层就可以像使用@Cacheable

13910
您找到你想要的搜索结果了吗?
是的
没有找到

cacheable更新_详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

@Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓存的名称,...在 spring 配置文件中定义,必须指定至少一个 例如: @Cacheable(value=”mycache”) @Cacheable(value={”cache1”,”cache2”} key 缓存的...key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value=”testcache”,key=”#userName”) condition...true,则查缓存; @Cacheable(value = “user”, key = “#id”, condition = “#id lt 10”) public User conditionFindById...= { @Cacheable(value = “user”, key = “#username”) }, put = { @CachePut(value = “user”, key = “#result.id

3.4K20

玩转Spring Cache --- @Cacheable使用在MyBatis的Mapper接口上(解决Null key returned for cache operation)【享学Spring】

比如本文的知识点,从网络的世界里你能找到有人介绍说:@Cacheable不仅仅能标注在实例方法上,也能标注在接口方法上。...顿时丈二的和尚了有木有,难道网上说法有误是个坑:@Cacheable不能使用在接口上吗? 其实都不是,而是因为Spring它只说了其一,并没有说其二。...MapperScan扫进去了 public interface CacheDemoMapper { @Select("select * from user where id = #{id}") @Cacheable...下面我给小伙伴们介绍三种,任君选择 方案一:使用a0/p0的方式去对方法入参进行引用 说了很多次了,key中使用SpEL表达式,即可用字段名,也可以用a0/p0这种按照顺序的方式去获取,形如这样: @Cacheable...{ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Cacheable

3.8K41

Spring缓存注解@Cacheable、@CacheEvict、@CachePut

@Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 参数 解释 example value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个...例如:@Cacheable(value="mycache") @Cacheable(value={"cache1","cache2"} key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写...,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value="testcache",key="#userName") condition 缓存的条件,可以为空,使用 SpEL 编写...缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @CachePut(value="testcache",key="#userName")...缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @CacheEvict(value="testcache",key="#userName"

2K20

扩展spring cache 支持缓存多租户及其自动过期

@Cacheable 使用效果 ,更具 cacheName(value) + 请求入参 (key) 组成保存redis中的key public class PigxClientDetailsService...extends JdbcClientDetailsService { @Cacheable(value = SecurityConstants.CLIENT_DETAILS_KEY, key = "...区分缓存增加租户标识 [20190321232708_WNE7UL_cache2.jpeg] A租户入参为K1 ,spring cache 维护Redis Key拼接一个租户信息重写Spring...Cache 的 cacheManager 缓存管理器 KEY = cacheName + 入参 + 租户标识 这样A/B 租户请求参数相同时,读取的也是不同的Key 里面的值,避免数据脏读,保证隔离型...从上下文中获取租户ID,重写@Cacheable value 值即可完成,然后注入这个 cacheManager @Slf4j public class RedisAutoCacheManager

1.3K20
领券