ehcache cache learning and use summary

ehcache cache learning and use summary

1. Cache configuration

name: cache name.

maxElementsInMemory: The maximum number of caches.

eternal: Whether the object is permanently valid, once it is set, timeout will not work.

timeToIdleSeconds: Set the allowed idle time (unit: seconds) of the object before it becomes invalid. Only used when the eternal=false object is not permanently valid, an optional attribute, the default value is 0, that is, the idle time is infinite.

timeToLiveSeconds: Set the allowed survival time (unit: seconds) of the object before it expires. The maximum time is between the creation time and the expiration time. It is used only when eternal=false object is not permanently valid, the default is 0. That is, the object lifetime is infinite.

overflowToDisk: When the number of objects in memory reaches maxElementsInMemory, Ehcache will write the objects to disk.

diskSpoolBufferSizeMB: This parameter sets the buffer size of DiskStore (disk cache). The default is 30MB. Each Cache should have its own buffer.

maxElementsOnDisk: The maximum number of hard disk caches.

diskPersistent: Whether to cache the data during the restart period of the virtual machine. Whether the disk store persists between restarts of the Virtual Machine. The default value is false.

diskExpiryThreadIntervalSeconds: The running interval of disk failure threads, the default is 120 seconds.

memoryStoreEvictionPolicy: When the maxElementsInMemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default strategy is LRU (least recently used). You can set it to FIFO (first in first out) or LFU (less used).

clearOnFlush: Whether to clear when the amount of memory is maximum.

2.ehcache.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="hibernateCache">

    <diskStore path="java.io.tmpdir/ehcache/hibernateCache" />

    <!-- DefaultCache setting. -->
    <defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
        overflowToDisk="true" maxEntriesLocalDisk="100000" />

</ehcache>

3.EhcacheUtil class

public class EhcacheUtil {  

    private static final String path = "/ehcache.xml";  

    private URL url;  

    private CacheManager manager;  

    private static EhcacheUtil ehCache;  

    private EhcacheUtil(String path) {  
        url = getClass().getResource(path);  
        manager = CacheManager.create(url);  
    }  

    public static EhcacheUtil getInstance() {  
        if (ehCache== null) {  
            ehCache= new EhcacheUtil(path);  
        }  
        return ehCache;  
    }  

    public void put(String cacheName, String key, Object value) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = new Element(key, value);  
        cache.put(element);  
    }  

    public Object get(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = cache.get(key);  
        return element == null ? null : element.getObjectValue();  
    }  

    public Cache get(String cacheName) {  
        return manager.getCache(cacheName);  
    }  

    public void remove(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        cache.remove(key);  
    }  

}

4.Spring+EhCache cache

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation">
        <value>classpath:cache/ehcache-spring.xml</value>
    </property>
    <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->
    <property name="shared" value="true"/>
</bean> 

Guess you like

Origin blog.csdn.net/ampsycho/article/details/77750084