« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 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; }} |
|
|