Asynchronous Ref
● Avoid blocking any user thr
asynchronous CacheLoad
public ListenableFuture<St
final String key, fina
ListenableFutureTask<Str
ListenableFutureTask.c
new Callable<String>
public String call
return load(key)
}
});
executor.execute(task);
return task;
}
fresh
reads by providing an
der.reload implementation
tring> reload(
al String oldValue) {
ring> task =
create(
>() {
l() {
);
Google Confidential and Proprietary
Bulk Operations
● Sometimes it's more efficie
load a set of entries simulta
time
● This can be accomplished
CacheLoader.loadAll,
LoadingCache.getAll
● Unlike LoadingCache.ge
multiple requests for the sa
○ Doing so would dramat
keys may be spread ov
ent for a CacheLoader to
aneously rather than one at a
by overriding
and then querying through
et, getAll does not block
ame key
tically increase its cost, as
ver multiple segments
Google Confidential and Proprietary
Manual Cache Writ
● So far the only way a value
is if it comes from the Cach
when creating the cache
○ Which encourages cons
● But we also support manua
● And reads from the cache
values
String v = cache.getIf
// returns null
cache.put("one", "1");
v = cache.getIfPresent
// returns "1"
tes
e can ever get into the cache
heLoader you specified
sistent cache content
al writes to the cache
which don't load missing
fPresent("one");
;
t("one");
Google Confidential and Proprietary
Get or Compute
● Alternatively a new value c
Callable on cache misse
String v = cache.get(k
new Callable<String>
public String call
return key.toLow
}
});
● Concurrent requests for the
in a single computation wh
threads
can be loaded from a
es
key,
>() {
l() {
werCase();
e same absent key will result
hich will be returned to all
Google Confidential and Proprietary
Non-Loading Cach
● In fact, you don't even need
○ We still recommend the
○ But sometimes it's impr
CacheLoader at cache
● If you call CacheBuilder.
specifying a CacheLoader
Cache
○ Which implements put
(K, Callable)
○ In fact, LoadingCache
contains all of the non-l
hes
d a CacheLoader at all
em for consistency
ractical to define a
e-creation time
.build() (without
r) you get back a non-loading
t, getIfPresent, and get
e extends Cache, which
loading methods
Google Confidential and Proprietary
Disable Caching
● Sometimes it's necessary t
● The canonical way to do th
(0)
○ Can be done without re
CacheBuilderSpec
○ Concurrent lookups of t
a single load request, b
immediately
to simply turn off caching
his is using maximumSize
ecompiling using
the same key will still result in
but the result will be evicted
Google Confidential and Proprietary
Map View
● You can view the entries st
map using Cache.asMap(
● Notice: LoadingCache.g
(Object) have similar-loo
remember that they are ve
○ Map.get is really a "ge
analogous to Cache.ge
● This can be convenient for
● All ConcurrentMap write
○ However the canonical
with a CacheLoader o
tored inside the cache as a
()
get(K) and Map.get
oking signatures, but
ery different
et if present" method,
etIfPresent
r iterating over cache content
operations are implemented
way to write to a cache is still
or a Callable
Google Confidential and Proprietary
Future Work
● AsyncLoadingCache, wh
Future<V>
● CacheBuilder.withBac
facilitate cache layering (L1
● Many performance optimiz
internals to new Concurre
here get(K) returns
ckingCache(Cache) to
1 + L2 cache)
zations, including migrating
entHashMap
Google Confidential and Proprietary
FIN
Google Confidential and Proprietary
Why not Map?
● Map.get causes a type-saf
● Map.get is a read operation
also write
● Map.equals is not symmetr
● No way to lookup without c
● Either pending computation
explicit writes or writes mus
computations
● Interface fails to convey the
fety hole
n and users don't expect it to
ric on a self-populating Map
causing computation
ns can be overwritten by
st block on pending
e intent (caching!)
Google Confidential and Proprietary