深刻理解Ehcache系列(三)

        在系列(一)中,提到Ehcache提供了三种清空策略.那么如何设置相应的参数呢? java

        Ehcache提供了配置文件的方式,也提供了参数传递的方式. 算法

   配置文件src/config/cache.xml     缓存

<cache name="testCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />

初始化时参数传递: this


public Cache(String name,
                 int maxElementsInMemory,
                 MemoryStoreEvictionPolicy memoryStoreEvictionPolicy,
                 boolean overflowToDisk,
                 String diskStorePath,
                 boolean eternal,
                 long timeToLiveSeconds,
                 long timeToIdleSeconds,
                 boolean diskPersistent,
                 long diskExpiryThreadIntervalSeconds,
                 RegisteredEventListeners registeredEventListeners) {

        this(new CacheConfiguration(name, maxElementsInMemory)
                    .memoryStoreEvictionPolicy(memoryStoreEvictionPolicy)
                    .overflowToDisk(overflowToDisk)
                    .eternal(eternal)
                    .timeToLiveSeconds(timeToLiveSeconds)
                    .timeToIdleSeconds(timeToIdleSeconds)
                    .diskPersistent(diskPersistent)
                    .diskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds),
                registeredEventListeners,
                null);

    }


各配置参数的含义: spa

maxElementsInMemory:缓存中容许建立的最大对象数
eternal:缓存中对象是否为永久的,若是是,超时设置将被忽略,对象从不过时。 code

overflowToDisk:内存不足时,是否启用磁盘缓存。
timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡以前,两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,若是该值是 0 就意味着元素能够停顿无穷长的时间。
timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,若是该值是0就意味着元素能够停顿无穷长的时间。
memoryStoreEvictionPolicy:缓存满了以后的淘汰算法。 xml

public CacheConfiguration(String name, int maxEntriesLocalHeap) {
        this.name = name;
        verifyGreaterThanOrEqualToZero((long) maxEntriesLocalHeap, "maxEntriesLocalHeap");
        this.maxEntriesLocalHeap = maxEntriesLocalHeap;
    }

      最后附上Cache的UML类图.不过这只是整个Ehcache实现的冰山一角.让咱们在后面的Blog中慢慢揭开她那神秘的面纱! 对象

相关文章
相关标签/搜索