📄 ehcachebasedfunctionauthoritycache.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;
public class EhCacheBasedFunctionAuthorityCache implements AuthorityBasedFunctionCache,InitializingBean{
private static final Log logger = LogFactory.getLog(EhCacheBasedFunctionAuthorityCache.class);
private Cache cache;
//~ Methods ================================================================
public void setCache(Cache cache) {
this.cache = cache;
}
public Cache getCache() {
return cache;
}
public GrantedAuthority[] getAuthorityFromCache(String functonname) {
Element element = null;
logger.info("get Authority by functionname from cache with ");
try {
element = cache.get(functonname);
} catch (CacheException cacheException) {
throw new DataRetrievalFailureException("Cache failure: "
+ cacheException.getMessage());
}
if (logger.isDebugEnabled()) {
logger.debug("Cache hit: " + (element != null) + "; username: "
+ functonname);
}
if (element == null) {
return null;
} else {
return (GrantedAuthority[]) element.getValue();
}
}
public void putAuthorityInCache(String functonname, GrantedAuthority[] grantedAuthority) {
logger.info("put Authority by functionname from cache ");
Element element = new Element(functonname,(Serializable)grantedAuthority);
if (logger.isDebugEnabled()) {
logger.debug("Cache put: " + element.getKey());
}
cache.put(element);
}
public void removeAuthorityFromCache(String functonname) {
logger.info("remove Authority by functionname from cache");
cache.remove(functonname);
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "cache mandatory");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -