Ehcache浅析

本文详细介绍了Ehcache缓存的数据结构、组件组成、使用方法,包括通过Spring XML配置获取Ehcache实例和注解驱动的缓存配置,并探讨了Ehcache对Hibernate的支持及异常处理机制。重点分析了Ehcache在分布式缓存场景中的应用,强调了Spring对Ehcache注解支持的价值,适用于方法执行结果的缓存优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ehcache提供了比较成熟的缓存机制,提供多方位的入口(spring支持),特别是各种可配置的store和策略比较强大,今天简单分析下源码分享一下

数据结构:

Element:

key,value,tti(time to idle),ttl(time to live),ElementEvictionData(对象销毁的各个时间点)

Cache:

cacheManager(管理cache内部状态,生命周期,support(peerprovider,diskpath)等),

api(外部调用入口 put get remove)

store(实际的存储实现,比如MemoryStore,LRUMemoryStore,DiskStore...)

RegisteredEventListeners(注册的各种listener,根据EventType类型

private static enum Event {
        EVICTED, PUT, EXPIRY, UPDATED, REMOVED;
}
事件通知,listener的作用可以有很多,例如远程同步操作达到分布式缓存的目的,同步或异步远程拷贝RMISynchronousCacheReplicator,RMIAsynchronousCacheReplicator

CachePeer

cache的远程操作接口,它的实现类可以是RMICachePeer,也可以是实现了事务的TransactionalRMICachePeer

。。。其他的略掉

使用方法:

通过spring xml配置获取ehcache实例,

      	<bean id="XxxCache"
      		class="xxx.cache.impl.XxxCacheImpl"
      		scope="singleton">
      		<property name="ehcache">
      			<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
      
      				<property name="cacheManager">
      					<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
      						<property name="configLocation">
      							<value>classpath:ehcache.xml</value>
      						</property>
      					</bean>
      				</property>
      				<property name="cacheName">
      					<value>UNIQUE_CACHE</value>
      				</property>
      			</bean>
      		</property>
     	</bean>
通过 注解驱动的缓存配置,网上找了一段:

@Component
public class DaoImpl implements Dao {

    /**
     * value定义了缓存区(缓存区的名字),每个缓存区可以看作是一个Map对象
     * key作为该方法结果缓存的唯一标识,
     */
    @Cacheable(value = { "dao.select" },key="#id")
    @Override
    public Object select(int id) {
        System.out.println("do in function select()");
        return new Object();
    }

    @CacheEvict(value = { "dao.select" }, key="#obj")
    @Override
    public void save(Object obj) {
        System.out.println("do in function save(obj)");
    }

}

需要定义一个resource,看上去如下:

<?xml version="1.0" encoding="GBK"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <diskStore path="java.io.tmpdir" />
           <defaultCache maxElementsInMemory="10000" eternal="false"
               timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
               maxElementsOnDisk="10000000" diskPersistent="false"
               diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
       
           <cache name="UNIQUE_CACHE" maxElementsInMemory="10000"
              eternal="false" timeToIdleSeconds="86400" timeToLiveSeconds="86400"
              overflowToDisk="false" />
</ehcache>


总结:

ehcache还提供了对hibernate的支持,有自己的异常处理机制等。

关键是spring对ehcache的注解支持很不错,可以对method的执行结果缓存,适合的场景也必将多,算是一种应用层面的优化方案。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值