
com.octo.android.robospice.persistence.CacheManager Maven / Gradle / Ivy
The newest version!
package com.octo.android.robospice.persistence;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import roboguice.util.temp.Ln;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.exception.CacheLoadingException;
import com.octo.android.robospice.persistence.exception.CacheSavingException;
/**
* An entity responsible for loading/saving data from/to cache. It implements a
* Chain of Responsibility pattern, delegating loading and saving operations to
* {@link ObjectPersister} or {@link 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
*/
/*
* Note to maintainers : concurrency must be taken care of as persister can be
* created by factories at any time. Thx to Henri Tremblay from EasyMock for
* peer review and concurrency checks.
*/
@SuppressWarnings("deprecation")
public class CacheManager implements ICacheManager {
/** The Chain of Responsibility list of all {@link Persister}. */
private Collection listPersister = new ArrayList();
private Map>> mapFactoryToPersister = new HashMap>>();
/** {@inheritDoc} */
@Override
public void addPersister(Persister persister) {
listPersister.add(persister);
if (persister instanceof ObjectPersisterFactory) {
// will lead the list to be copied whenever we add a persister to it
// but there won't be any overhead while iterating through the list.
mapFactoryToPersister.put((ObjectPersisterFactory) persister, new CopyOnWriteArrayList>());
} else if (!(persister instanceof ObjectPersister)) {
throw new RuntimeException(getClass().getSimpleName() + " only supports " + ObjectPersister.class.getSimpleName() + " or " + ObjectPersisterFactory.class.getSimpleName() + " instances.");
}
}
/** {@inheritDoc} */
@Override
public void removePersister(Persister persister) {
listPersister.remove(persister);
if (persister instanceof ObjectPersisterFactory) {
mapFactoryToPersister.remove(persister);
}
}
/**
* {@inheritDoc}
* @throws CacheCreationException
*/
@Override
public T loadDataFromCache(Class clazz, Object cacheKey, long maxTimeInCacheBeforeExpiry) throws CacheLoadingException, CacheCreationException {
return getObjectPersister(clazz).loadDataFromCache(cacheKey, maxTimeInCacheBeforeExpiry);
}
@Override
@SuppressWarnings("unchecked")
/** {@inheritDoc}*/
public T saveDataToCacheAndReturnData(T data, Object cacheKey) throws CacheSavingException, CacheCreationException {
// http://stackoverflow.com/questions/4460580/java-generics-why-someobject-getclass-doesnt-return-class-extends-t
ObjectPersister classCacheManager = getObjectPersister((Class) data.getClass());
return classCacheManager.saveDataToCacheAndReturnData(data, cacheKey);
}
/**
* {@inheritDoc}
* @throws CacheCreationException
*/
@Override
public boolean isDataInCache(Class> clazz, Object cacheKey, long maxTimeInCacheBeforeExpiry) throws CacheCreationException {
return getObjectPersister(clazz).isDataInCache(cacheKey, maxTimeInCacheBeforeExpiry);
}
/**
* {@inheritDoc}
* @throws CacheLoadingException
* @throws CacheCreationException
*/
@Override
public Date getDateOfDataInCache(Class> clazz, Object cacheKey) throws CacheLoadingException, CacheCreationException {
return new Date(getObjectPersister(clazz).getCreationDateInCache(cacheKey));
}
/**
* {@inheritDoc}
*/
@Override
public boolean removeDataFromCache(Class> clazz, Object cacheKey) {
try {
return getObjectPersister(clazz).removeDataFromCache(cacheKey);
} catch (CacheCreationException e) {
Ln.e(e);
return false;
}
}
/**
* {@inheritDoc}
*/
@Override
public void removeAllDataFromCache(Class> clazz) {
try {
getObjectPersister(clazz).removeAllDataFromCache();
} catch (CacheCreationException e) {
Ln.e(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy