Ehcache入门基础

Simple-Object 2018-02-28 原文

   EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

  

  1.首先先导入ehcache相关的jar依赖,我这里有的是maven项目做得演示,所以要在pom.xml里面添加依赖。

  

  1.   <dependency>
  2.   <groupId>net.sf.ehcache</groupId>
  3.   <artifactId>ehcache</artifactId>
  4.   <version>2.10.2</version>
  5.   </dependency>

   2.创建配置文件 ehcache.xml,ehcache在启动的时候会默认去classpath根目录下找名为ehcache.xml的文件,也可以放在其他位置,使用时指明即可。为了方便,这次就在src/main/resources/创建一个配置文件 ehcache.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. <!-- 磁盘缓存位置 -->
  5. <diskStore path="java.io.tmpdir/ehcache"/>
  6. <!-- 默认缓存 -->
  7. <defaultCache
  8. maxEntriesLocalHeap="10000"
  9. eternal="false"
  10. timeToIdleSeconds="120"
  11. timeToLiveSeconds="120"
  12. maxEntriesLocalDisk="10000000"
  13. diskExpiryThreadIntervalSeconds="120"
  14. memoryStoreEvictionPolicy="LRU">
  15. <persistence strategy="localTempSwap"/>
  16. </defaultCache>
  17. <!-- helloworld缓存 -->
  18. <cache name="HelloWorldCache"
  19. maxElementsInMemory="1000"
  20. eternal="false"
  21. timeToIdleSeconds="100"
  22. timeToLiveSeconds="100"
  23. overflowToDisk="false"
  24. memoryStoreEvictionPolicy="LRU"/>
  25. </ehcache>

   3.测试类

  

  1. package com.hz.demo;
  2. import net.sf.ehcache.Cache;
  3. import net.sf.ehcache.CacheManager;
  4. import net.sf.ehcache.Element;
  5. public class EhcacheTest1 {
  6. public static void main(String[] args) {
  7. // 1. 创建缓存管理器
  8. CacheManager cacheManager = CacheManager.create();
  9. // 2. 获取缓存对象
  10. Cache cache = cacheManager.getCache("HelloWorldCache");
  11. // 3. 创建元素
  12. Element element = new Element("key1", "HelleWorld");
  13. // 4. 将元素添加到缓存
  14. cache.put(element);
  15. // 5. 获取缓存
  16. Element value = cache.get("key1");
  17. // 6.输出缓存中的值
  18. System.out.println("key1="+value.getObjectValue());
  19. // 7.删除缓存中的值
  20. cache.remove("key1");
  21. // 8. 刷新缓存
  22. cache.flush();
  23. // 9. 关闭缓存管理器
  24. cacheManager.shutdown();
  25. }
  26. }

  4.输出结果

  

 

发表于 2018-02-28 13:56 Simple_Object 阅读() 评论() 编辑 收藏

 

版权声明:本文为Simple-Object原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Simple-Object/p/8483276.html

Ehcache入门基础的更多相关文章

随机推荐

  1. 建立统一高效的客户关系管理体系的重要环节

          文章转载自:CRM软件资讯网   一、建立统一高效的客户关系管理理念   这种统一高效的理念应源于 […]...

  2. 12、如何在Windows 2000下将Oracle完全卸载? – Sanle

    12、如何在Windows 2000下将Oracle完全卸载? 12、如何在Windows 2000下将Ora […]...

  3. 关于ECShop4.0安装时数据库报错问题解决

    是ECShop版本的问题,重点在$db_host不能带端口号,把这个文件 ecshop4\ecshop\ins […]...

  4. 云计算技术原理

    由于云计算分为IaaS、PaaS和SaaS三种类型,不同的厂家又提供了不同的解决方案,目前还没有一个统一的技术 […]...

  5. 语音识别(语音转文字)&& 语音合成(文字转语音)

    【语音合成API】SpeechSynthesisUtterance是HTML5中新增的API,用于将指定文字合 […]...

  6. 76、django之内置Admin

    本篇导航: 配置路由 定制Admin   Django内置的Admin是对于model中对应的数据表进行增删改 […]...

  7. 智能表单设计器Web Free Form Designer:FreeForm模板及数据外部存取接口 – 智能在线表单设计器 Web Form Builder

    FreeForm模板及数据外部存取接口及方式   引言 因为FreeForm的设计方向是支援企业业务的表单引擎 […]...

  8. 高级词向量表示 – royhoo

    高级词向量表示 本文是在上文自然语言处理——词的表示基础上,引入一个更先进的词向量模型GloVe。然后介绍如何 […]...