Ehcache缓存配置和基本使用

在java项目广泛的使用。它是一个开源的、设计于提高在数据从RDBMS中取出来的高花费、高延迟采取的一种缓存方案。正因为Ehcache具有健壮性(基于java开发)、被认证(具有apache 2.0 license)、充满特色(稍后会详细介绍),所以被用于大型复杂分布式web application的各个节点中。

 

  • 够快
    Ehcache的发行有一段时长了,经过几年的努力和不计其数的性能测试,Ehcache终被设计于large, high concurrency systems.
  • 够简单
    开发者提供的接口非常简单明了,从Ehcache的搭建到运用运行仅仅需要的是你宝贵的几分钟。其实很多开发者都不知道自己用在用Ehcache,Ehcache被广泛的运用于其他的开源项目
    比如:hibernate
  • 够袖珍
    关于这点的特性,官方给了一个很可爱的名字small foot print ,一般Ehcache的发布版本不会到2M,V 2.2.3 才 668KB。
  • 够轻量
    核心程序仅仅依赖slf4j这一个包,没有之一!
  • 好扩展
    Ehcache提供了对大数据的内存和硬盘的存储,最近版本允许多实例、保存对象高灵活性、提供LRU、LFU、FIFO淘汰算法,基础属性支持热配置、支持的插件多
  • 监听器
    缓存管理器监听器 (CacheManagerListener)和 缓存监听器(CacheEvenListener),做一些统计或数据一致性广播挺好用的
    如何使用?

POM文件

        <!--加入缓存-->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.6</version>
        </dependency>

 

配置文件

在resources资源目录下创建一个ehcache-config.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <!-- EhCache在每次启动的时候都要连接到 ehcache 网站上去检查新版本 使用如上的 updateCheck="false" 来禁止这个检查新版本 -->

    <!--  
        name:cache唯一标识   
        eternal:缓存是否永久有效   
        maxElementsInMemory:内存中最大缓存对象数  
        overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中  
        diskPersistent:硬盘持久化  
        timeToIdleSeconds:缓存清除时间   
        timeToLiveSeconds:缓存存活时间
        diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔
        memoryStoreEvictionPolicy:缓存清空策略
        1.FIFO:first in first out 先讲先出  
        2.LFU: Less Frequently Used 一直以来最少被使用的  
        3.LRU:Least Recently Used  最近最少使用的   
    -->

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

    <defaultCache 
        maxElementsInMemory="10000" 
        eternal="false" 
        timeToIdleSeconds="120"
        timeToLiveSeconds="120" 
        overflowToDisk="true" 
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120" 
        memoryStoreEvictionPolicy="FIFO" />

    <!-- 缓存  -->
    <cache name="cache_mall_keys"
       maxElementsInMemory="200"
       eternal="false"
       timeToIdleSeconds="7200"
       timeToLiveSeconds="7200"
       overflowToDisk="true"
       maxElementsOnDisk="1000"
       diskPersistent="false"
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="FIFO" />

    <!-- 缓存  -->
    <cache name="cache_pos_codes"
           maxElementsInMemory="200"
           eternal="false"
           timeToIdleSeconds="43200"
           timeToLiveSeconds="43200"
           overflowToDisk="true"
           maxElementsOnDisk="1000"
           diskPersistent="false"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="FIFO" />

</ehcache> 

 

Spirng整合配置

注意一下内容必须注册在Spring的主配置文件中

    <!--缓存配置文件接口-->

    <cache:annotation-driven cache-manager="cacheManager"/>
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache-config.xml"></property>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"></property>
    </bean>

 

使用方法

这里可以使用注解的方式 @Cacheable(value = “cache_pos_codes”) 其中value的是设置的配置文件ehcache-config.xml的配置名称。需要注意的是 import org.springframework.cache.annotation.Cacheable;

    @RequestMapping(value = "/date",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE + CHARSET)
    @ResponseBody
    @Cacheable(value = "cache_pos_codes")
    public String getDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

 

测试发现,不断刷新该URL,发现不返回实时的时间,而是返回缓存的字符串时间

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值