zoukankan      html  css  js  c++  java
  • Ehcache缓存配置和基本使用

    前言

      在java项目广泛的使用中。它是一个开源的、设计于提高在数据从RDBMS中取出来的高花费、高延迟采取的一种缓存方案。

    正因为Ehcache具有健壮性(基于java开发)、被认证(具有apache 2.0 license)、充满特色(稍后会详细介绍),

    所以被用于大型复杂分布式web application的各个节点中。

    特点

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

    如何使用

    Maven依赖

    1 <!--加入缓存-->
    2 <dependency>
    3     <groupId>net.sf.ehcache</groupId>
    4     <artifactId>ehcache-core</artifactId>
    5     <version>2.6.6</version>
    6 </dependency>

    配置文件

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

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3        xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
     4        updateCheck="false">
     5    <!-- EhCache在每次启动的时候都要连接到 ehcache 网站上去检查新版本 使用如上的 updateCheck="false" 来禁止这个检查新版本 -->
     6    
     7    <!--  
     8         name:cache唯一标识   
     9         eternal:缓存是否永久有效   
    10         maxElementsInMemory:内存中最大缓存对象数  
    11         overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中  
    12         diskPersistent:硬盘持久化  
    13         timeToIdleSeconds:缓存清除时间   
    14         timeToLiveSeconds:缓存存活时间
    15         diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔
    16         memoryStoreEvictionPolicy:缓存清空策略
    17         1.FIFO:first in first out 先进先出  
    18         2.LFU: Less Frequently Used 一直以来最少被使用的  
    19         3.LRU:Least Recently Used  最近最少使用的   
    20     -->
    21    
    22    <diskStore path="java.io.tmpdir/ehcache" />
    23    
    24    <defaultCache 
    25       maxElementsInMemory="10000" 
    26       eternal="false" 
    27       timeToIdleSeconds="120"
    28       timeToLiveSeconds="120" 
    29       overflowToDisk="true" 
    30       maxElementsOnDisk="10000000"
    31       diskPersistent="false"
    32       diskExpiryThreadIntervalSeconds="120" 
    33       memoryStoreEvictionPolicy="FIFO" />
    34    
    35    <cache name="normal_cache"
    36       maxElementsInMemory="200"
    37       eternal="false"
    38       timeToIdleSeconds="7200"
    39       timeToLiveSeconds="7200"
    40       overflowToDisk="true"
    41       maxElementsOnDisk="1000"
    42       diskPersistent="false"
    43       diskExpiryThreadIntervalSeconds="120"
    44       memoryStoreEvictionPolicy="FIFO" />
    45 </ehcache> 

    spring整合配置

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

     1 <!--缓存配置文件接口-->
     2 <cache:annotation-driven cache-manager="cacheManager"/>
     3 <!--创建缓存管理器工厂-->
     4 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
     5     <property name="configLocation" value="classpath:ehcache-config.xml"></property>
     6 </bean>
     7 <!--创建缓存管理器-->
     8 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
     9     <property name="cacheManager" ref="cacheManagerFactory"></property>
    10 </bean>

    使用方法

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

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

    转载http://www.zhoutao123.com/?p=106

  • 相关阅读:
    mac 终端命令kill掉某个指定端口
    python web开发之flask框架学习(1) 创建flask项目
    ios json转model的简单现实
    SnapKit swift实现高度自适应的新浪微博布局
    IOS swift实现密码的显示与隐藏切换
    IOS UIWebView与js的简单交互swift3版
    android 手写万能adapter适配器
    简单几步实现 IOS UITextField输入长度的控制
    IOS Swift UITableViewcontroller实现点击空白处隐藏键盘
    xcode 版本控制推送代码到远程git仓库的步骤
  • 原文地址:https://www.cnblogs.com/PreachChen/p/8659285.html
Copyright © 2011-2022 走看看