com.lone.common.core.cache.LoginEhcache Maven / Gradle / Ivy
The newest version!
package com.lone.common.core.cache;
import java.util.List;
import org.springframework.util.Assert;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
public class LoginEhcache implements Cache {
private Ehcache cache;// ehcache
public LoginEhcache(Ehcache cache) {
Assert.notNull(cache, "cache is not null");
this.cache = CacheManager.getInstance().getCache("loginCache");// 永久缓存块
}
/**
* {@inheritDoc}
*/
public Object getObject(Object key) {
try {
Element cachedElement = cache.get(key);
if (cachedElement == null) {
return null;
}
return cachedElement.getObjectValue();
} catch (Throwable t) {
throw new CacheException(t);
}
}
@Override
public Object getItem(String key) {
try {
Element cachedElement = cache.get(key);
if (cachedElement == null) {
return null;
}
return cachedElement.getObjectValue();
} catch (Throwable t) {
throw new CacheException(t);
}
}
@Override
public void putItem(String key, Object value) {
try {
cache.put(new Element(key, value));
} catch (Throwable t) {
throw new CacheException(t);
}
}
public List