com.clickntap.tool.cache.EhCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Stripecube Show documentation
Show all versions of Stripecube Show documentation
Stripecube is an open source Java framework for Web Applications
package com.clickntap.tool.cache;
import com.clickntap.tool.types.Datetime;
import net.sf.ehcache.Element;
import java.io.Serializable;
import java.util.List;
public class EhCache implements Cache {
private net.sf.ehcache.Cache cache;
public EhCache(net.sf.ehcache.CacheManager cacheManager, String cacheName) throws Exception {
cache = cacheManager.getCache(cacheName);
if (cache == null) {
cache = new net.sf.ehcache.Cache(cacheName, 9999999, false, false, 5 * Datetime.ONEHOURINSECONDS, 2 * Datetime.ONEHOURINSECONDS);
cacheManager.addCache(cache);
cache = cacheManager.getCache(cacheName);
}
}
public Serializable get(Serializable key) throws Exception {
Element element = cache.get(key);
return (Serializable) (element != null ? element.getObjectValue() : null);
}
public Serializable put(Serializable key, Serializable value) throws Exception {
Element element = new Element(key, value);
cache.put(element);
return value;
}
public void remove(Serializable theKey) throws Exception {
String rootKey = theKey.toString().substring(0, theKey.toString().lastIndexOf('-'));
for (Object aKey : (List) cache.getKeys()) {
if (aKey.toString().startsWith(rootKey)) {
cache.remove(aKey);
}
}
}
public void removeAll() throws Exception {
cache.removeAll();
}
public boolean contains(Serializable key) throws Exception {
return cache.isKeyInCache(key);
}
}