Class EntityMemcache


  • public class EntityMemcache
    extends Object

    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
    • Field Detail

      • NEGATIVE

        public static final String NEGATIVE
        The value stored in the memcache for a negative cache result.
        See Also:
        Constant Field Values
    • Method Detail

      • 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

        public void putAll​(Collection<EntityMemcache.Bucket> updates)
        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

        public void empty​(Iterable<com.google.cloud.datastore.Key> keys)
        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

        public static Set<com.google.cloud.datastore.Key> keysOf​(Collection<EntityMemcache.Bucket> buckets)
        Basically a list comprehension of the keys for convenience.