com.octo.android.robospice.persistence
Class CacheManager

java.lang.Object
  extended by com.octo.android.robospice.persistence.CacheManager
All Implemented Interfaces:
ICacheManager

public class CacheManager
extends Object
implements ICacheManager

An entity responsible for loading/saving data from/to cache. It implements a Chain of Responsibility pattern, delegating loading and saving operations to ObjectPersister or ObjectPersisterFactory elements. The chain of responsibility is ordered. This means that the order used to register elements matters. All elements in the chain of responsibility are questioned in order. The first element that can handle a given class for persistence will be used to persist data of this class.

Author:
sni

Constructor Summary
CacheManager()
           
 
Method Summary
 void addPersister(Persister persister)
          
<T> List<Object>
getAllCacheKeys(Class<T> clazz)
          Get all cache keys associated to a given class.
 Date getDateOfDataInCache(Class<?> clazz, Object cacheKey)
          The date at which given data has been stored last in cache.
protected
<T> ObjectPersister<T>
getObjectPersister(Class<T> clazz)
           
 boolean isDataInCache(Class<?> clazz, Object cacheKey, long maxTimeInCacheBeforeExpiry)
          Test whether or not some data is in cache.
<T> List<T>
loadAllDataFromCache(Class<T> clazz)
          Loads all data stored in cache for a given class.
<T> T
loadDataFromCache(Class<T> clazz, Object cacheKey, long maxTimeInCacheBeforeExpiry)
          Loads an instance of a class clazz, that is stored in cache under the key cacheKey.
 void removeAllDataFromCache()
          Removes all data in the cache.
 void removeAllDataFromCache(Class<?> clazz)
          Removes all data in the cache that are instances of class clazz.
 boolean removeDataFromCache(Class<?> clazz, Object cacheKey)
          Removes a given data in the cache that is an instance of class clazz.
 void removePersister(Persister persister)
          
<T> T
saveDataToCacheAndReturnData(T data, Object cacheKey)
          Save an instance of a given class, into the cache identified by cacheKey.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheManager

public CacheManager()
Method Detail

addPersister

public void addPersister(Persister persister)

Specified by:
addPersister in interface ICacheManager

removePersister

public void removePersister(Persister persister)

Specified by:
removePersister in interface ICacheManager

loadDataFromCache

public <T> T loadDataFromCache(Class<T> clazz,
                               Object cacheKey,
                               long maxTimeInCacheBeforeExpiry)
                    throws CacheLoadingException,
                           CacheCreationException
Loads an instance of a class clazz, that is stored in cache under the key cacheKey.

Specified by:
loadDataFromCache in interface ICacheManager
Parameters:
clazz - the class of the object that is supposed to be stored in cache.
cacheKey - the key used to identify this item in cache.
maxTimeInCacheBeforeExpiry - the maximum time (in ms) an item can be stored in cache before being considered expired.
Returns:
an instance of a class clazz, that is stored in cache under the key cacheKey. If the item is not found in cache or is older than maxTimeInCacheBeforeExpiry, then this method will return null.
Throws:
CacheCreationException
CacheLoadingException

saveDataToCacheAndReturnData

public <T> T saveDataToCacheAndReturnData(T data,
                                          Object cacheKey)
                               throws CacheSavingException,
                                      CacheCreationException
Description copied from interface: ICacheManager
Save an instance of a given class, into the cache identified by cacheKey. Some ObjectPersister can modify the data they receive before saving it. Most ObjectPersister instances will just save the data as-is, in this case, they can even return it and save it asynchronously in a background thread for a better efficiency.

Specified by:
saveDataToCacheAndReturnData in interface ICacheManager
Parameters:
data - the data to be saved in cache.
cacheKey - the key used to identify this item in cache.
Returns:
the data that was saved.
Throws:
CacheSavingException
CacheCreationException

isDataInCache

public boolean isDataInCache(Class<?> clazz,
                             Object cacheKey,
                             long maxTimeInCacheBeforeExpiry)
                      throws CacheCreationException
Test whether or not some data is in cache.

Specified by:
isDataInCache in interface ICacheManager
Parameters:
clazz - the class of the object that is supposed to be stored in cache.
cacheKey - the key used to identify this item in cache.
maxTimeInCacheBeforeExpiry - the maximum time (in ms) an item can be stored in cache before being considered expired.
Returns:
a boolean indicating whether or not the given data is in the cache.
Throws:
CacheCreationException

getDateOfDataInCache

public Date getDateOfDataInCache(Class<?> clazz,
                                 Object cacheKey)
                          throws CacheLoadingException,
                                 CacheCreationException
The date at which given data has been stored last in cache.

Specified by:
getDateOfDataInCache in interface ICacheManager
Parameters:
clazz - the class of the object that is supposed to be stored in cache.
cacheKey - the key used to identify this item in cache.
Returns:
the date at which data has been stored last in cache. Null if no such data exists.
Throws:
CacheLoadingException
CacheCreationException

removeDataFromCache

public boolean removeDataFromCache(Class<?> clazz,
                                   Object cacheKey)
Removes a given data in the cache that is an instance of class clazz.

Specified by:
removeDataFromCache in interface ICacheManager
Parameters:
clazz - the class of the data to be removed.
cacheKey - the identifier of the data to be removed from cache.
Returns:
a boolean indicating whether or not this data could be removed.

removeAllDataFromCache

public void removeAllDataFromCache(Class<?> clazz)
Removes all data in the cache that are instances of class clazz.

Specified by:
removeAllDataFromCache in interface ICacheManager
Parameters:
clazz - the class of the data to be removed.

getAllCacheKeys

public <T> List<Object> getAllCacheKeys(Class<T> clazz)
Get all cache keys associated to a given class.

Specified by:
getAllCacheKeys in interface ICacheManager
Parameters:
clazz - the class for which to get all cache keys.
Returns:
all cache keys associated to a given class. The empty list is nothing is found in cache.

loadAllDataFromCache

public <T> List<T> loadAllDataFromCache(Class<T> clazz)
                             throws CacheLoadingException,
                                    CacheCreationException
Loads all data stored in cache for a given class.

Specified by:
loadAllDataFromCache in interface ICacheManager
Parameters:
clazz - the class for which to get all data stored in cache.
Returns:
all data stored in cache for a given class.
Throws:
CacheCreationException
CacheLoadingException

removeAllDataFromCache

public void removeAllDataFromCache()
Removes all data in the cache.

Specified by:
removeAllDataFromCache in interface ICacheManager

getObjectPersister

protected <T> ObjectPersister<T> getObjectPersister(Class<T> clazz)
                                         throws CacheCreationException
Throws:
CacheCreationException


Copyright © 2012-2014. All Rights Reserved.