public interface

Cache

com.neomades.content.cache.Cache
Known Indirect Subclasses

Class Overview

An interface for data that must be saved in cache. The cache can be seen as a map where each entry is a CacheEntry associated with a key which is a String. A CacheEntry is only a byte array. Its data can be loaded from a database, a file or the phone memory relying on that you want to to.

Summary

Public Methods
abstract void clear()
Clears the cache.
abstract CacheEntry get(String key)
Retrieves a CacheEntry from the cache associated with the given key.
abstract void initialize()
Performs any potentially long-running actions needed to initialize the cache.
abstract void invalidate(String key, boolean fullExpire)
Invalidates an entry in the cache.
abstract void put(String key, CacheEntry entry)
Adds or replaces an CacheEntry in the cache.
abstract void remove(String key)
Removes an entry from the cache.

Public Methods

public abstract void clear ()

Clears the cache.

public abstract CacheEntry get (String key)

Retrieves a CacheEntry from the cache associated with the given key. Data must be returned as a CacheEntry object. If data are loaded from a file or a database, a CacheEntry object must be created here with the data. To do that, you can use or CacheEntry#setDeserializedData(Object).

The way the cache data are loaded or not when a request is performed depends of the CacheEntry type you use. The mechanism relies on the methods isExpired() and refreshNeeded(). When performing a request, if isExpired() is true, the cache data will be ignored and the data will be downloaded again. If refreshNeeded() is true, the current cache data will be sent via an intermediate response and new data will be downloaded. isExpired() is evaluated before refreshNeeded(). The basic behavior of these methods is explained in CacheEntry javadoc. The API proposes a simplest alternative implementation with the DefaultCacheEntry class. If none of the behaviors responds to your need, you can implement your own CacheEntry in the get(String) method.

Parameters
key the cache key
Returns

public abstract void initialize ()

Performs any potentially long-running actions needed to initialize the cache. Will be called from a worker thread. It's here that database or files can be created for example.

public abstract void invalidate (String key, boolean fullExpire)

Invalidates an entry in the cache.
Here is the default implementation that should be used:

 CacheEntry entry = get(key);
 if (entry != null) {
 	entry.setSoftTtl(0);
 	if (fullExpire) {
 		entry.setTtl(0);
 	}
 	put(key, entry);
 }
 

This will force the cache content to be downloaded again the next time the request to download it will be run. Note that if fullExpire is false the old data in the cache will be sent in an intermediate response in order to have something to present to the user while the new data are downloaded.

Parameters
key the cache key
fullExpire true to ignore the current cache data

public abstract void put (String key, CacheEntry entry)

Adds or replaces an CacheEntry in the cache. It's here that data in the CacheEntry must be saved in the persistent system (database, file, phone memory...).

Parameters
key the cache key
entry Data to store in the cache

public abstract void remove (String key)

Removes an entry from the cache. Do not forget to delete the CacheEntry data from the persistent associated system (database, file...)

Parameters
key the cache key