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.
- 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.
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();
-
-
Field Summary
Fields Modifier and Type Field Description public final static int
DEFAULT_MAX_ENTRIES
public Context
context
public int
maxEntries
-
Constructor Summary
Constructors Constructor Description CompositeCache(Context context)
Constructs a composite cache instance with the android application context.
-
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. -
-
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.
-
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.- Returns:
This
instance of CompositeCache.
-
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 entryvalue
- value of the cache entry- Returns:
The previous value associated with the
key
ornull
if the entry did not exist before.
-
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- Returns:
The value associated with the cache entry's key or
null
if it does not exist in all levels of caches.
-
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- Returns:
A
CacheEntry
object that contains the key and value. getValue returnsnull
if the key is not found.
-
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.
-
-
-
-