本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9693633
建立时间:2004年12月20日




[java语言]spring对cache的支持
原创空间,  文章收藏,  软件技术

邢红瑞 发表于 2006/6/3 10:59:21

spring对encache的支持要好于其他的cache,内在的实现,不需要modules的支持,使用encache 1.2以上的版本。 Ehcache的类层次模型主要为三层,最上层的是CacheManager,是操作Ehcache的入口。可以通过CacheManager.getInstance()获得一个单子的CacheManger,或者通过CacheManger的构造函数创建一个新的CacheManger。每个CacheManager都管理着多个Cache。而每个Cache都以一种类Hash的方式,关联着多个Element。而Element则用于存放要缓存内容的地方。在ehcache的配置文件里面必须配置defaultCache。每个<cache>标签定义一个新的cache,属性的含义基本上可以从名字上得到. EhCacheManagerFactoryBean的类,管理ehcache.xml,只有configLocation属性比较重要。配置<bean id="cacheManager"     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  <property name="configLocation" value="classpath:ehcache.xml" /></bean>ehcache.xml<ehcache>  <defaultCache      maxElementsInMemory="500"      eternal="true"      overflowToDisk="false"      memoryStoreEvictionPolicy="LFU" />  <cache name="userCache"      maxElementsInMemory="500"      eternal="true"      overflowToDisk="false"      memoryStoreEvictionPolicy="LFU" /></ehcache>创建EhCache的实例 <bean id="ehcache"     class="org.springframework.cache.ehcache.EhCacheFactoryBean">  <property name="cacheManager" ref="cacheManager" />  <property name="cacheName" value="userCache" /></bean> cache region是userCache。举个acegi的使用例子 <property name="userCache">    <bean class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">      <property name="cache" ref="ehcache" />  </bean>  </property></bean>EhCacheBasedUserCache的实现很简单public class EhCacheBasedUserCache implements UserCache, InitializingBean {    //~ Static fields/initializers =====================================================================================     private static final Log logger = LogFactory.getLog(EhCacheBasedUserCache.class);     //~ Instance fields ================================================================================================     private Ehcache cache;     //~ Methods ========================================================================================================     public void afterPropertiesSet() throws Exception {        Assert.notNull(cache, "cache mandatory");    }     public Ehcache getCache() {        return cache;    }     public UserDetails getUserFromCache(String username) {        Element element = null;         try {            element = cache.get(username);        } catch (CacheException cacheException) {            throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());        }         if (logger.isDebugEnabled()) {            logger.debug("Cache hit: " + (element != null) + "; username: " + username);        }         if (element == null) {            return null;        } else {            return (UserDetails) element.getValue();        }    }     public void putUserInCache(UserDetails user) {        Element element = new Element(user.getUsername(), user);         if (logger.isDebugEnabled()) {            logger.debug("Cache put: " + element.getKey());        }         cache.put(element);    }     public void removeUserFromCache(UserDetails user) {        if (logger.isDebugEnabled()) {            logger.debug("Cache remove: " + user.getUsername());        }         this.removeUserFromCache(user.getUsername());    }     public void removeUserFromCache(String username) {        cache.remove(username);    }     public void setCache(Ehcache cache) {        this.cache = cache;    }}


阅读全文(3602) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.046 second(s), page refreshed 144769800 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号