Class EntityMemcache
This is the facade used by Objectify to cache entities in the MemcacheService.
Entity cacheability and expiration are determined by a CacheControl
object.
In addition, hit/miss statistics are tracked in a MemcacheStats
.
In order to guarantee cache synchronization, getAll() *must* be able to return an IdentifiableValue, even for entries not present in the cache. Because empty cache values cannot be made into IdentifiableValue, we immediately replace them with a null value and refetch (null is a valid cache value). If this refetch doesn't work, we treat the key as uncacheable for the duration of the request.
The values put in memcache are Key -> Entity, except for negative cache entries, which are Key -> String (the value NEGATIVE).
- Author:
- Jeff Schnitzer invalid input: '<'[email protected]>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
A bucket represents memcache information for a particular Key. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionEntityMemcache
(MemcacheService memcache, String namespace) Creates a memcache which caches everything without expiry and doesn't record statistics.EntityMemcache
(MemcacheService memcache, String namespace, CacheControl cacheControl) Creates a memcache which doesn't record statsEntityMemcache
(MemcacheService memcacheService, String namespace, CacheControl cacheControl, MemcacheStats stats) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Revert a set of keys to the empty state.Map<com.google.cloud.datastore.Key,
EntityMemcache.Bucket> Gets the Buckets for the specified keys.static Set<com.google.cloud.datastore.Key>
keysOf
(Collection<EntityMemcache.Bucket> buckets) Basically a list comprehension of the keys for convenience.void
putAll
(Collection<EntityMemcache.Bucket> updates) Update a set of buckets with new values.
-
Field Details
-
NEGATIVE
The value stored in the memcache for a negative cache result.- See Also:
-
-
Constructor Details
-
EntityMemcache
Creates a memcache which caches everything without expiry and doesn't record statistics. -
EntityMemcache
Creates a memcache which doesn't record stats -
EntityMemcache
public EntityMemcache(MemcacheService memcacheService, String namespace, CacheControl cacheControl, MemcacheStats stats)
-
-
Method Details
-
getAll
public Map<com.google.cloud.datastore.Key,EntityMemcache.Bucket> getAll(Iterable<com.google.cloud.datastore.Key> keys) Gets the Buckets for the specified keys. A bucket is built around an IdentifiableValue so you can putAll() them without the risk of overwriting other threads' changes. Buckets also hide the underlying details of storage for negative, empty, and uncacheable results.
Note that worst case (a cold cache), obtaining each bucket might require three memcache requests: a getIdentifiable() which returns null, a put(null), and another getIdentifiable(). Since there is no batch getIdentifiable(), this is *per key*.
When keys are uncacheable (per CacheControl) or the memcache is down, you will still get an empty bucket back. The bucket will have null IdentifiableValue so we can identify it as uncacheable.
- Returns:
- the buckets requested. Buckets will never be null. You will always get a bucket for every key.
-
putAll
Update a set of buckets with new values. If collisions occur, resets the memcache value to null.- Parameters:
updates
- can have null Entity values, which will record a negative cache result. Buckets must have been obtained from getAll().
-
empty
Revert a set of keys to the empty state. Will loop on this several times just in case the memcache write fails - we don't want to leave the cache in a nasty state. -
keysOf
Basically a list comprehension of the keys for convenience.
-