I'm new to Google's Guava library and am interested in Guava's Caching package. Currently I have version 10.0.1 downloaded. After reviewing the documentation, the JUnit tests source code and even after searching google extensively, I still can't figure out how to use the Caching package. The documentation is very short, as if it was written for someone who has been using Guava's library not for a newbie like me. I just wish there are more real world examples on how to use Caching package propertly.
Let say I want to build a cache of 10 non expiring items with Least Recently Used (LRU) eviction method. So from the example found in the api, I build my code like the following:
Cache<String, String> mycache = CacheBuilder.newBuilder() .maximumSize(10) .build( new CacheLoader<String, String>() { public String load(String key) throws Exception { return something; // ????? } });
Since the CacheLoader is required, I have to include it in the build method of CacheBuilder. But I don't know how to return the proper value from mycache.
To add item to mycache, I use the following code:
mycache.asMap().put("key123", "value123");
To get item from mycache, I use this method:
mycache.get("key123")
The get method will always return whatever value I returned from CacheLoader's load method instead of getting the value from mycache. Could someone kindly tell me what I missed?
Interface LoadingCache<K,V> A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated. Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.
The Guava project is a collection of several of Google's core libraries for string processing, caching, etc. Guava Cache is an in-memory cache used in applications.
Interface Methods Provided to satisfy the Function interface; use get(K) or getUnchecked(K) instead. Returns a view of the entries stored in this cache as a thread-safe map. Returns the value associated with key in this cache, first loading that value if necessary.
A builder of LoadingCache and Cache instances having any combination of the following features: automatic loading of entries into the cache. least-recently-used eviction when a maximum size is exceeded. time-based expiration of entries, measured since last access or last write.
Guava's Cache
type is generally intended to be used as a computing cache. You don't usually add values to it manually. Rather, you tell it how to load the expensive to calculate value for a key by giving it a CacheLoader
that contains the necessary code.
A typical example is loading a value from a database or doing an expensive calculation.
private final FooDatabase fooDatabase = ...; private final LoadingCache<Long, Foo> cache = CacheBuilder.newBuilder() .maximumSize(10) .build(new CacheLoader<Long, Foo>() { public Foo load(Long id) { return fooDatabase.getFoo(id); } }); public Foo getFoo(long id) { // never need to manually put a Foo in... will be loaded from DB if needed return cache.getUnchecked(id); }
Also, I tried the example you gave and mycache.get("key123")
returned "value123"
as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With