Create a Google Guava Cache with a Complex Key

I am trying to create a cache that has a “pair” as a key, with this Pair class taken from this post.

I'm trying to:

CacheLoader<Pair<String, String>, String> loader = new CacheLoader<Pair<String, String>, String>() { public String load(Pair<String, String> key) { return GetRatingIdentityByShortNameLoader(key.first, key.second); } }; _ratingIdCache = CacheBuilder.newBuilder() .concurrencyLevel(a_conclevel.intValue()) .maximumSize(a_maxsize.intValue()) .expireAfterAccess(a_maxage.intValue(), TimeUnit.MINUTES) .build(loader); 

What fails to compile in Eclipse (helios, java 1.6) with

The build (CacheLoader) method in the CacheBuilder type is not applicable for arguments (new CacheLoader, String> () {})

Does anyone have any suggestions on how to solve this? The goal is that I need to have a cache that stores an "identifier" for which the "primary key" is "Rating Agency" + "Rating".

Guava 10.0.1

+4
source share
1 answer

I had this cache, which was originally defined as Cache, and when I modify CacheBuilder.build () to use a complex key, I forgot to update the cache declaration.

So a simple change:

 Cache<String, String> _ratingAgencyId; 

to

 Cache<Pair<String, String>, String> _ratingAgencyId; 

did the trick.

+5
source

All Articles