【问题标题】:Spring @Cacheable how to configure a complex keySpring @Cacheable 如何配置一个复杂的key
【发布时间】:2014-11-26 06:06:28
【问题描述】:

我有一个返回列表的方法,我想根据传递的参数缓存它。参数是 4 和整数,如何使用 SpEL 配置它? 我正在使用春季版本 4.0.6.RELEASE。

【问题讨论】:

标签: java spring spring-el spring-cache


【解决方案1】:

默认是使用方法的所有参数,其返回值被缓存。 例如。

@Cacheable(value ="CacheService")
public ReturnType methodName(int param1, int param2, int param3, int param4) {
  //method body
}

【讨论】:

    【解决方案2】:

    你可以使用SimpleKey类(org.springframework.cache.interceptor.SimpleKey):

    @Cacheable(value="getPerson", key="new SimpleKey(#id, #age, #socialSecNo)") 
    public String getPerson(Integer id, Integer age, Integer socialSecNo){
        ...
    }
    

    【讨论】:

    • 当您在此处使用 new 关键字时,这是否会通过填充堆导致内存泄漏来不断增加内存使用量?
    【解决方案3】:

    你可以使用这样的东西

    @Cacheable(value ="CacheService", key="#par1 + #par2 +#par3 +#par4" )
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 2016-06-15
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多