【问题标题】:How to make cache key if method parameter is Interface如果方法参数是接口,如何制作缓存键
【发布时间】:2018-07-11 13:53:20
【问题描述】:

我有一个接受org.springframework.data.domain.Pageable 作为参数的方法。 当用户请求 page = 0 和 size = 20 等等时,我想将这些传递的值设为cache key

我做的是

@Cacheable(key = "#pageable.number")
public Person getPersons(Pageable pageable) 

它给出了异常:

EL1008E: 在类型的对象上找不到属性或字段“数字” 'org.springframework.data.domain.PageRequest' - 也许不公开?

如何制作缓存键呢?

【问题讨论】:

    标签: java spring spring-boot caching ehcache


    【解决方案1】:

    该属性被称为pageNumber 而不是number

    @Cacheable(key = "#pageable.pageNumber")
    public Person getPersons(Pageable pageable) 
    

    但是,以这种方式使用缓存的一般意义可能会受到质疑。但这可能超出了这个问题的范围。

    【讨论】:

    • 这行得通!!!什么是The general sense of using the cache this way can be questioned though。也请添加这个,会很有帮助的。
    • 首先,我希望这只是伪代码,您实际上返回的是PageCollection,而不是单个Person 对象。然后你也有非常彻底的缓存失效,否则你最终会得到陈旧的数据。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2011-07-08
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2011-05-31
    • 2021-02-20
    • 1970-01-01
    相关资源
    最近更新 更多