📄 ehcachebasedfunctioncache.java
字号:
package sample.auth.cache;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Element;
import org.acegisecurity.GrantedAuthority;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.util.Assert;
import sample.auth.GrantedFunction;
public class EhCacheBasedFunctionCache implements FunctionCache,InitializingBean{
private static final Log logger = LogFactory.getLog(EhCacheBasedFunctionCache.class);
private Cache cache;
//~ Methods ================================================================
public void setCache(Cache cache) {
this.cache = cache;
}
public Cache getCache() {
return cache;
}
public GrantedFunction[] getFunctionFromCache(String roleName) {
Element element = null;
logger.info("get Function by rolename from cache ");
try {
element = cache.get(roleName);
} catch (CacheException cacheException) {
throw new DataRetrievalFailureException("Cache failure: "
+ cacheException.getMessage());
}
if (logger.isDebugEnabled()) {
logger.debug("Cache hit: " + (element != null) + "; username: "
+ roleName);
}
if (element == null) {
return null;
} else {
return (GrantedFunction[]) element.getValue();
}
}
public void putFuncitonInCache(String rolename,GrantedFunction[] grantedFunction) {
Element element = new Element(rolename,(Serializable)grantedFunction);
logger.info("put Functon by rolename from cache ");
if (logger.isDebugEnabled()) {
logger.debug("Cache put: " + element.getKey());
}
cache.put(element);
}
public void removeFunctionFromCache(String roleName) {
logger.info("remove Functon by rolename from cache ");
cache.remove(roleName);
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "cache mandatory");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -