If you think the Android project mobile2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.ecollege.android.util;
/*fromwww.java2s.com*/import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentHashMap;
import roboguice.util.Ln;
publicclass VolatileCacheManager {
privatestaticfinalint INITIAL_CACHE_CAPACITY = 255;
protectedclass CacheEntry {
public Object value;
publiclong cachedAt;
public CacheEntry(Object value, long cachedAt) {
super();
this.value = value;
this.cachedAt = cachedAt;
}
public Boolean valueIsOlderThanMillis(long expirationInMillis) {
if (System.currentTimeMillis() - cachedAt <= expirationInMillis) {
return false;
}
return true;
}
}
finalprotected ConcurrentHashMap<Object, SoftReference<CacheEntry>> cacheMap = new ConcurrentHashMap<Object, SoftReference<CacheEntry>>(INITIAL_CACHE_CAPACITY);
protectedfinallong expirationInMillis;
public VolatileCacheManager(long expirationInMillis) {
super();
this.expirationInMillis = expirationInMillis;
}
publicvoid put(Object key, Object value) {
// TODO: limit the size of the cache more manually than with SoftReference?
// TODO: Add a TTL
Ln.i(String.format("Cache put key: %s, value: %s", key, value.toString()));
cacheMap.put(key, new SoftReference<CacheEntry>(new CacheEntry(value, System.currentTimeMillis())));
}
public <CachedT> CachedT get(Object key, Class<CachedT> clazz) {
SoftReference<CacheEntry> ref = cacheMap.get(key);
CacheEntry cacheEntry = null;
Object cachedObject = null;
if (null != ref) {
cacheEntry = ref.get();
if (cacheEntry != null) {
if (cacheEntry.valueIsOlderThanMillis(expirationInMillis)) {
cacheMap.remove(key);
Ln.i(String.format("Cache key was expired: %s", key));
} else {
cachedObject = cacheEntry.value;
}
}
}
if (null == cachedObject) {
Ln.i( String.format("Cache miss for key: %s", key));
return null;
} else {
Ln.i( String.format("Cache hit for key: %s", key));
if (null != clazz) {
try {
CachedT castObject = clazz.cast(cachedObject);
return castObject;
} catch (ClassCastException cce) {
Ln.i( String.format("Cache failed to cast object to Class: ", clazz.toString()));
return null;
}
} else {
return null;
}
}
}
publicvoid clear() {
cacheMap.clear();
}
}