Reputation: 38511
I'm trying to create a simple session cache that allows me to set a maximum size on the whole cache. The cache is not a computing cache, so I think Guava's CacheBuilder
with a LoadingCache
is not appropriate. I want to be able perform the following operations:
get(key)
data from the cache that was previously stashed during a stashing operation
put(key,value)
into the cache during the stashing operation
I tried using MapMaker but the maximum size method seems to be antiquated. Does anyone know what options I have? I can always resort to just using a simple Map and rolling my own policy I suppose?
Upvotes: 1
Views: 436
Reputation: 46382
The cache is not a computing cache, so I think Guava's
CacheBuilder
with aLoadingCache
is not appropriate.
So use the CacheBuilder
without it. It works exactly the same, except that you call plain build()
instead if build(CacheLoader)
. Everything stays the same, except for you get a "normal" cache instead of a loading one. That's all.
I tried using MapMaker but the maximum size method seems to be antiquated.
Caching via MapMaper
will go away one day, forget it.
The example is very trivial, just create
private final static Cache<String, String> cache =
CacheBuilder.newBuilder().maximumSize(123).build();
and then try this:
cache.put("a", "A");
cache.put("b", "B");
System.out.println(cache.getIfPresent("a"));
for (int i=0; i<1000; ++i) cache.put("n" + i, "x");
System.out.println(cache.getIfPresent("a"));
Upvotes: 6