简体   繁体   English

Ehcache自动密钥生成和@Cacheable spring注释

[英]Ehcache automatic key generation and @Cacheable spring annotation

Does anybody know how the default key generation for Ehcache works? 有谁知道Ehcache的默认密钥生成是如何工作的? If I have the following method: 如果我有以下方法:

@Cacheable(cacheName = CACHE_KEY) // CACHE_KEY is static final field.
public List<DataObject> list(
    int firstRecord, int maxRecords, int pageSize, FilterObject filter) {
    ....
}

where FilterObject is a custom POJO, what should I expect to be the actual cache key? 哪里FilterObject是自定义POJO,我应该期望什么是实际的缓存键?

What I am observing is when using different FilterObject instances and not changing the other arguments of my method call, it always produces the same result - the first call's result is cached and returned. 我观察的是当使用不同的FilterObject实例而不更改我的方法调用的其他参数时,它总是产生相同的结果 - 第一个调用的结果被缓存并返回。

Probably it is the FilterObject POJO which causes the behaviour - I suppose it is either some serialization, or .toString() issue, because I haven't overridden the relevant methods. 可能是导致行为的FilterObject POJO - 我想它是一些序列化或.toString()问题,因为我没有覆盖相关的方法。

Still I was unable to find exact information on how the cache key for such method is being formed both in Ehcache's website and in the @Cacheable annotation documentation. 我仍然无法找到有关如何在Ehcache的网站和@Cacheable注释文档中形成此类方法的缓存密钥的确切信息。 I'd appreciate any information and recommendations on this topic. 我很感激有关此主题的任何信息和建议。

This is the default key generator 这是默认密钥生成器

public class DefaultKeyGenerator implements KeyGenerator {

public static final int NO_PARAM_KEY = 0;
public static final int NULL_PARAM_KEY = 53;

public Object generate(Object target, Method method, Object... params) {
    if (params.length == 1) {
        return (params[0] == null ? NULL_PARAM_KEY : params[0]);
    }
    if (params.length == 0) {
        return NO_PARAM_KEY;
    }
    int hashCode = 17;
    for (Object object : params) {
        hashCode = 31 * hashCode + (object == null ? NULL_PARAM_KEY : object.hashCode());
    }
    return Integer.valueOf(hashCode);
}

}

As you can see, it combines the hash-codes of each method parameter. 如您所见,它结合了每个方法参数的哈希码。

Everything is explained in Spring reference documentation , namely in: 一切都在Spring 参考文档中解释,即:

28.3.1.1 Default Key Generation : 28.3.1.1默认密钥生成

[...] [...]

  • If more the one param is given, return a key computed from the hashes of all parameters . 如果给出更多的一个参数,则返回从所有参数的哈希计算出的密钥。

To provide a different default key generator, one needs to implement the org.springframework.cache.KeyGenerator interface. 要提供不同的默认密钥生成器,需要实现org.springframework.cache.KeyGenerator接口。 Once configured, the generator will be used for each declaration that doesn not specify its own key generation strategy (see below). 配置完成后,生成器将用于每个未指定自己的密钥生成策略的声明(见下文)。

and below: 及以下:

28.3.1.2 Custom Key Generation Declaration : 28.3.1.2自定义密钥生成声明

[...] the @Cacheable annotation allows the user to specify how the key is generated through its key attribute. [...] @Cacheable注释允许用户通过其键属性指定密钥的生成方式。 The developer can use SpEL to pick the arguments of interest[...] 开发人员可以使用SpEL来选择感兴趣的参数[...]

And an example from the docs: 以及来自文档的示例:

@Cacheable(value="books", key="#isbn.rawNumber")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

So in your case you should simply implement equals() and hashCode() for FilterObject . 因此,在您的情况下,您应该只为FilterObject实现equals()hashCode() Decent IDE can generate them for you. 体面的IDE可以为您生成它们。

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

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