简体   繁体   English

关于Object的私有属性的可缓存注释的条件

[英]Condition on cacheable annotation on private property of Object

I have a use case in which I want to be able to cache the result of a method based on the property of a object. 我有一个用例,我希望能够根据对象的属性缓存方法的结果。 The property is private but exposes a public getter. 该物业是私人的,但暴露了公共吸气剂。 (This can be changed, but i would not want to do that) (这可以改变,但我不想这样做)

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

PackageEntity class is 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 for conditional caching specifies how to use condition. 用于条件缓存的Spring doc指定了如何使用条件。 However, this cahcing does not works as indicated by condition. 但是,这种情况并不适用于条件。 It is simply caching all packages irrespective of selling price. 无论销售价格如何,它都可以简单地缓存所有包裹。 I am unable to understand what am I doing wrong. 我无法理解我做错了什么。 Neither there are suitable examples that I can refer for this. 我没有合适的例子可以参考。

Any help appreciated. 任何帮助赞赏。 Thanks 谢谢

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

It seems SpEL also looks for public getters of the field if the specified field is private. 如果指定的字段是私有的,SpEL似乎也会查找该字段的公共getter。

So the field can be either public or have a public getter 因此,该领域可以是公共的,也可以是公共吸气剂

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

So the above code worked alright for me. 所以上面的代码对我来说没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM