每页的Spring可分页缓存键

时间:2017-03-22 15:35:01

标签: spring spring-data spring-cache

我正在尝试执行以下操作

@Cacheable(value = ACTIVE_DATA_CONFIGURATION_CACHE, key = "#tenant.id.concat('-').concat(#pageable.page)")
    public Page<DataConfiguration> findAllByTenant(final Pageable pageable, final Tenant tenant) {
    }

例外

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'page' cannot be found on object of type 'org.springframework.data.domain.PageRequest' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)

2 个答案:

答案 0 :(得分:2)

由于Pageable没有属性页,如果您尝试将密钥生成为object clickedLink = sharedWebDriver.ExecuteAsyncScript(@" var callback = arguments[arguments.length - 1]; clickedLink('http://google.de'); "); ,则可以将密钥值更改为{tenant.id}-{pageNumber}

答案 1 :(得分:0)

对于您的使用案例,请尝试使用KeyGenerator

@Cacheable(value = "doOneThing", keyGenerator = "CustomGenerator")

这样的事情:

public class CustomGenerator implements KeyGenerator {

 public Object generate(Object target, Method method, Object... params) {
    String code = "UNIQUE_CODE"; // implements logic from params
    return code;
 }

}