I just have to override the way the cache selects the correct key, since some fields (for example, timestamp, message identifier, etc.) should not be taken into account when retrieving the key. I cannot change the actual hash function of the key object because it is already being used for recognition in my code.
Is this possible with Guava Caches? And with a workaround?
This is my configuration:
CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).recordStats(). expireAfterWrite(DEFAULT_AGE, TimeUnit.DAYS).build( new CacheLoader<Request, Response>() { @Override public Response load(Request request) { return request.getResponse(); } });
And this is my hash function (used somewhere else in my code):
public int hashCode() { final int prime = 31; int result = 1; result = prime * result + code; result = prime * result + messageID;
Btw, is it a cache that uses its own implementation of the hash function (for example, through introspection) or does it use it by default?
** EDIT: **
As indicated in the answers, the best way to achieve this result is a wrapper class.
My solution :
private static final class CachedRequest extends Request { private static CachedRequest fromRequest(Request request) {
Hamal000
source share