Cache Flashcards
remember: network calls are…
blocking
primary use case for caching
you find yourself frequently accessing the same data from a remote server.
define a cache ‘miss’
miss-rate
data is requested from cache, and it doesn’t have it
miss-rate: the percentage of requests that were misses
define a cache ‘hit’
hit-rate
data is requested from cache, and it has the data
hit-rate: the percentage of requests that were hits
in Guava cache framework, the 3 core classes
LoadingCache
CacheBuilder
CacheLoader
Loading Cache (Guava framework)
This class manages a cache, keeping objects in a Map and automatically retrieving new data from the data store on a cache miss
CacheBuilder (Guava framework)
This factory class creates properly configured instances of LoadingCache
CacheLoader (Guava framework)
LoadingCache uses a CacheLoader that we configure to retrieve new data from the data store.
high miss-rates indicate that…
…many requests make the expensive request AND add overhead to check and update the cache.
Cache is most helpful when…
… the system requests the same information repeatedly. You benefit from the cache only if you request the same data again and resuse the previous result.
Programs with high miss-rates may improve the cache performance by…
…adjusting the cache configuration size
adjusting the cache TTL
evaluating if users reuse the same data
“sweet spot”
amount of RAM that Guava defaults to use
Guava defaults to using all available RAM. Devs always set an upper limit on the amount of memory the cache can use, via the CacheBuilder’s method maximumSize()
maximumSize()
method in Guava’s CacheBuilder that sets the maximum amount of memory the cache will consume, in number of entries (of the pair)
where is maximumSize() set?
this is set in your CustomizedCacheDao constructor:
public CustomCacheDao(DataAccessDao accessDao)
loadingCacheVariable = CacheBuilder.newBuilder()
.maximumSize(50)
.build(CacheLoader.from(accessDao::getData)
);
how does Cache evict data?
When the cache already contains the maximum number of entries, and we request a new key, the cache evicts the least recently used entry to make space.