【问题标题】:how spring cache define a prefix?spring缓存如何定义前缀?
【发布时间】:2018-12-17 08:31:55
【问题描述】:

我的公司使用的是 spring 版本 4.2.Release。我想用redis添加spring缓存功能。我感到困惑的是如何为我的 biz 密钥自定义前缀? 我知道有两种定义键的方法

一个。键

@Cacheable(value = CacheConstants.REDIS_CACHE, key = CacheConstants.CLASSES_PREFIX + "#classId")

但是这种类型的spring会报错:

EL1041E:(pos 2):解析有效表达式后,仍有 表达式中的更多数据:'colon(:)'

我这样使用但不是@Cacheable(value = CacheConstants.REDIS_CACHE, key = "'cn.cnt.concat'.concat(#classId)"),因为这种方式是课堂上的硬编码。

b.密钥生成器

如果使用keyGenerator,我们可以定制一个key。但不能将 bizKey 前缀传递给 keyGenerator。一旦这样,我们就不能在其他更新 biz 数据的 @CacheEvict 方法中无效(删除)一个键。

那么,这两种方式,好像没有办法自定义key的前缀?

【问题讨论】:

    标签: java spring spring-cache


    【解决方案1】:

    你可以像动态键一样这样做

    @Cacheable(value = CacheConstants.REDIS_CACHE,key="'"+CacheConstants.CLASSES_PREFIX+"'#classId")
    

    【讨论】:

    • 非常感谢,我解决了使用这种风格的问题:@Cacheable(value = CacheConstants.REDIS_CACHE, key = "'" + CacheConstants.CLASSES_PREFIX + "'+#classId")
    • @D.TS 你的最终风格和我的有什么区别。
    • 看最后一个“'+#classId”,必须有一个“+”号。
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2021-04-16
    • 1970-01-01
    • 2012-09-27
    • 2013-06-01
    • 1970-01-01
    相关资源
    最近更新 更多