zoukankan      html  css  js  c++  java
  • Spring Caching集成Ehcache

    Ehcache可以对页面、对象、数据进行缓存,同时支持集群/分布式缓存。在应用中用于常常需要读取的数据交换,而不是通过DB DAO数据交换(cache不占用DB宝贵的NIO,直接交换堆内存)。

    整合Spring、Hibernate也非常的简单,Spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache。

    从Spring3.1开始添加了对缓存的支持。

    Maven的依赖:

    <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.9.0</version>
        </dependency>
    
            <!-- Optional, to log stuff -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>
    
        <!-- Spring caching framework inside this -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
    
        <!-- Support for Ehcache and others -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
    </project>

    Gradle的依赖:

    apply plugin: 'java'
    apply plugin: 'eclipse-wtp'
    
    version = '1.0'
    
    // Uses JDK 7
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    // Get dependencies from Maven central repository
    repositories {
        mavenCentral()
    }
    
    //Project dependencies
    dependencies {
        compile 'org.springframework:spring-context:4.1.4.RELEASE'
        compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
        compile 'net.sf.ehcache:ehcache:2.9.0'
        compile 'ch.qos.logback:logback-classic:1.0.13'
    }

    spring-config-cache.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehcacheManager"/>
        </bean>
    
        <!--ehcache-->
        <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache/ehcache.xml"/>
        </bean>
    
    
    </beans>

    ehcache.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache name="es">
    
        <diskStore path="java.io.tmpdir"/>
    
        <cache name="code-cache"
               maxEntriesLocalHeap="2000"
               eternal="false"
               timeToIdleSeconds="3600"
               timeToLiveSeconds="0"
               overflowToDisk="false"
               statistics="true">
        </cache>
    
    </ehcache>

    参数说明:

        name:
        cache的名称,必填。
    
        maxEntriesLocalHeap:
        在内存中的最大保存对象数量,0则不限,如果填必须小于Integer.MAX_SIZE (2147483647)。
    
        maxEntriesLocalDisk:
        磁盘中保存的数量,0则不限,一般填0。
    
        eternal:
        true:对象永不过期。false:对象有过期时限。
    
        以下为选填:
    
        maxEntriesInCache:
        只在分布式环境使用,集群节点中的最大数量,0则不限
    
        overflowToOffHeap:
        企业版Ehcache才有的功能,用于java off-heap(off-heap允许Java直接操作内存空间),这样的目的是为了节省宝贵的jvm堆,又避免磁盘存储的低速。
    
        maxBytesLocalHeap:
        定义缓存可以从VM的堆中使用多少字节,如果定义这个则不能再定义maxEntriesLocalHeap。
    
        maxBytesLocalOffHeap:
        定义缓存可以从OffHeap中使用多少字节
    
        maxBytesLocalDisk:
        定义缓存可以从磁盘中使用多少OffHeap字节
    
        timeToIdleSeconds:
        对象空闲过期时间,以秒为单位,如果是0则不过期。只对eternal为false的有效。
    
        timeToLiveSeconds:
        对象生存时间,一般为0,为永久生存。只对eternal为false的有效。
    
        上边的两个配置容易混淆,区别:
        timeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x;对象空闲时间,指对象在多长时间没有被访问就会失效。
        timeToIdleSeconds=y:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔y;
    
        diskExpiryThreadIntervalSeconds: 
        对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。 
    
        diskSpoolBufferSizeMB:
        DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
        
        memoryStoreEvictionPolicy: 
        如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
    
        clearOnFlush:
        flush()的时候清除内存。
    
        persistence sub-element.  持久化策略,这是子属性,一般不用设置。
    
        * localRestartable - 可以重用的缓存,持久化在磁盘,只有企业版才有这个功能.
    
        * localTempSwap - 当(on-heap and/or off-heap)满的时候保存到磁盘,但并不重用持久化,即进程结束则缓存全部清除。
    
        * none - 不持久化

    使用

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.Cache;
    import org.springframework.cache.CacheManager;
    import org.springframework.stereotype.Service;
    
    ......
    
    private Cache cache;
    
    ......
    
        public void addObjToCache(Object key, Object value) {
            cache.put(key, value);
        }
    
        public String getValueByCache(String key) {
            return (String)cache.get(key).get();
        }
    
    ......
  • 相关阅读:
    ant-design-vue——子组件通过$parent修改父组件的值时无效问题及解决方法
    vue——quill-editor自定义图片上传
    ES6——var、let、const三者的区别
    js——数组/对象常用方法总结
    28.最长回文子序列
    27.马拉车
    26.扫雷一次点击
    JS添加内容之方法里传AJAX参数
    JQ 实现加载其他页面的H5代码 JQ加载H5独立导航栏代码
    CentOS 7不能上网 解决方法
  • 原文地址:https://www.cnblogs.com/starcrm/p/7168765.html
Copyright © 2011-2022 走看看