Class CompositeCache

  • All Implemented Interfaces:
    com.sap.cloud.mobile.foundation.cache.Cache , com.sap.cloud.mobile.foundation.cache.CacheBuilder

    
    public final class CompositeCache<K, V>
    extends CacheBase<K, V> implements CacheBuilder<K, V>
                        
    is a multi-level cache container that implements the Cache interface. The lowest level is usually configured with a persistent store.
    • The cache at each level is appended to a chain, from highest level to lowest level. Order matters, and the user should add the backing store last.
    • All levels of caches share the same Key and Value types.
    • The sizes of cache levels should be from the smallest to the largest.
    • Write Policy-- uses Write-through Policy, that is, a put operation will add the entry to all levels of cache.
    • Read-miss Policy:
      • Only when a cache entry is not found at one level, the next lower level will be queried.
      • The item found at a lower level will be propagated to all the cache levels above.
    • When a cache entry is cleared from a cache to free up space based on the Replacement Policy implemented, the removed value will not be added to the lower level caches.
    Creating and Configuring CompositeCache
    • Calls the constructor-- CompositeCache.
    • Adds levels of caches-- add
    • Completes the configuration of the composite cache-- build. Now the composite cache instance is ready for other operations.
    Example
    
        MemoryCache<String, ValueType> cache1 = new MemoryCache<>(androidContext, 16);
    
        // cache2 is a persistent cache that implements the Cache interface.
        CompositeCache<String, ValueType> compositeCache = new CompositeCache<String,
            ValueType>(androidContext)
            .add(cache1)   // The first level cache-- MemoryCache.
            .add(cache2)   // The second level cache-- a backing store.
            .build();
    
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Constructor Summary

      Constructors 
      Constructor Description
      CompositeCache(Context context) Constructs a composite cache instance with the android application context.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      <T extends Cache<K, V>> CompositeCache<K, V> add(@NonNull() T cache) Adds a concrete cache instance as one level of cache in the composite cache.
      CompositeCache<K, V> build() Completes the cache(s) configuration after add has been called at least twice, then returns this composite cache instance.
      synchronized V put(@NonNull() K key, @NonNull() V value, double cost) Cost Factor feature is not supported by Composite Cache.
      V put(@NonNull() K key, @NonNull() V value) Adds an entry to the composite cache.
      synchronized V get(@NonNull() K key) Retrieves the value associated with a cache entry identified by the specified key.
      synchronized CacheEntry<K, V> getEntry(@NonNull() K key) Has the same behavior as get, but returns an instance of CacheEntry which contains a key and a value instead of a value.
      void remove(@NonNull() K key) Goes through all levels of cache including the lowest backing store and removes an entry.
      void removeAll() Removes all entries at all cache levels.
      synchronized int getEntryCount() Not supported for Composite Cache.
      synchronized List<K> keys() Not supported for Composite Cache.
      • Methods inherited from class com.sap.cloud.mobile.foundation.cache.CacheBase

        getContext, getCostOfEntries, getMaxEntries, init, init
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CompositeCache

        CompositeCache(Context context)
        Constructs a composite cache instance with the android application context.
        Parameters:
        context - Android application context
    • Method Detail

      • add

        @NonNull() <T extends Cache<K, V>> CompositeCache<K, V> add(@NonNull() T cache)

        Adds a concrete cache instance as one level of cache in the composite cache. Depending on the level of caches desired, this method needs be called in the order of cache levels until build is called.

        Parameters:
        cache - an instance of non-null concrete Cache class
      • build

        @NonNull() CompositeCache<K, V> build()

        Completes the cache(s) configuration after add has been called at least twice, then returns this composite cache instance.

      • put

        @Nullable() synchronized V put(@NonNull() K key, @NonNull() V value, double cost)

        Cost Factor feature is not supported by Composite Cache.

      • put

        @Nullable() V put(@NonNull() K key, @NonNull() V value)

        Adds an entry to the composite cache. If the value is the same as original value, nothing will happen. Otherwise, the entry will be populated from top to bottom through cache levels.

        Parameters:
        key - key of the cache entry
        value - value of the cache entry
      • get

        @Nullable() synchronized V get(@NonNull() K key)

        Retrieves the value associated with a cache entry identified by the specified key.

        Side Effects:

        1. When Read-Miss occurs at one cache level, it continues querying through deeper levels of caches until the entry is found.

        2. The cache entry found will be propagated up through cache levels.

        Parameters:
        key - the key of the cache entry
      • getEntry

        @Nullable() synchronized CacheEntry<K, V> getEntry(@NonNull() K key)

        Has the same behavior as get, but returns an instance of CacheEntry which contains a key and a value instead of a value.

        Parameters:
        key - the key of the cache entry
      • remove

         void remove(@NonNull() K key)

        Goes through all levels of cache including the lowest backing store and removes an entry.

        Parameters:
        key - the key of the cache entry to remove
      • removeAll

         void removeAll()

        Removes all entries at all cache levels.

      • getEntryCount

         synchronized int getEntryCount()

        Not supported for Composite Cache.

      • keys

        @NonNull() synchronized List<K> keys()

        Not supported for Composite Cache.