javaspringehcachepojomethod-parameters

Ehcache automatic key generation and @Cacheable spring annotation


Does anybody know how the default key generation for Ehcache works? 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?

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.

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.

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. I'd appreciate any information and recommendations on this topic.


Solution

  • 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.