【问题标题】:Condition on cacheable annotation on private property of Object对象私有属性上可缓存注释的条件
【发布时间】:2016-10-26 07:11:26
【问题描述】:

我有一个用例,我希望能够根据对象的属性缓存方法的结果。该属性是私有的,但公开了一个公共吸气剂。 (这可以改变,但我不想这样做)

@Cacheable(cacheNames = "detailedData", key = "#id", condition = "#currentPackage.getSellingPrice() > -1")
public Map<String, Object> getDetailedTestData(int id,PackageEntity currentPackage) {
/**
some code
*/
}

PackageEntity 类是

public class PackageEntity {

    private int sellingPrice;

    public int getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(int sellingPrice) {
        this.sellingPrice = sellingPrice;
    }
  /**
  some other fields and their getter/setter
  */
}

条件缓存的 Spring doc 指定了如何使用条件。但是,这种 cahcing 并不像条件所指示的那样工作。无论售价如何,它只是缓存所有包裹。 我无法理解我做错了什么。也没有合适的例子可以参考。

任何帮助表示赞赏。谢谢

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-cacheable-condition

【问题讨论】:

    标签: java spring annotations ehcache spring-cache


    【解决方案1】:

    如果指定的字段是私有的,SpEL 似乎也会寻找该字段的公共 getter。

    所以字段可以是公开的,也可以是公开的getter

    @Cacheable(cacheNames = "detailedData", key = "#id", condition ="#currentPackage.sellingPrice > -1")
    public Map<String, Object> getDetailedTestData(int id,PackageEntity currentPackage) {
      /**
        some code
      */
    }
    

    所以上面的代码对我来说没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2019-10-13
      相关资源
      最近更新 更多