Ehcache入门基础
2018-06-18 03:06:16来源:未知 阅读 ()
1.ehcache的简介
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
2.ehcache入门实例
1.首先先导入ehcache相关的jar依赖,我这里有的是maven项目做得演示,所以要在pom.xml里面添加依赖。
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
2.创建配置文件 ehcache.xml,ehcache在启动的时候会默认去classpath根目录下找名为ehcache.xml的文件,也可以放在其他位置,使用时指明即可。为了方便,这次就在src/main/resources/创建一个配置文件 ehcache.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">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- helloworld缓存 -->
<cache name="HelloWorldCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
3.测试类
package com.hz.demo;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhcacheTest1 {
public static void main(String[] args) {
// 1. 创建缓存管理器
CacheManager cacheManager = CacheManager.create();
// 2. 获取缓存对象
Cache cache = cacheManager.getCache("HelloWorldCache");
// 3. 创建元素
Element element = new Element("key1", "HelleWorld");
// 4. 将元素添加到缓存
cache.put(element);
// 5. 获取缓存
Element value = cache.get("key1");
// 6.输出缓存中的值
System.out.println("key1="+value.getObjectValue());
// 7.删除缓存中的值
cache.remove("key1");
// 8. 刷新缓存
cache.flush();
// 9. 关闭缓存管理器
cacheManager.shutdown();
}
}
4.输出结果

标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 04.Java基础语法 2020-06-11
- 1-Java基础回顾整理_01 2020-06-10
- Java基础语法菜鸟教程笔记 2020-06-10
- logstash系列-入门整理 2020-06-10
- Java基础复习——类和对象 2020-06-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
