在本教程中,我们将向您展示两个示例,以帮助您开始使用Ehcache 。
使用的工具 :
- 高速缓存2.9
- Maven 3
- 摇篮2
- JDK 1.7
PS Ehcache需要JDK 1.5或更高版本。
1.项目目录结构
2. Hello World
假设这是一个Maven项目:
pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency>
阅读注释以进行自我解释。
HelloEhCache.java
package com.mkyong.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class HelloEhCache{
public static void main(String[] args) {
//1. Create a cache manager
CacheManager cm = CacheManager.getInstance();
//2. Create a cache called "cache1"
cm.addCache("cache1");
//3. Get a cache called "cache1"
Cache cache = cm.getCache("cache1");
//4. Put few elements in cache
cache.put(new Element("1","Jan"));
cache.put(new Element("2","Feb"));
cache.put(new Element("3","Mar"));
//5. Get element from cache
Element ele = cache.get("1");
//6. Print out the element
String output = (ele == null ? null : ele.getObjectValue().toString());
System.out.println(output);
//7. Is key in cache?
System.out.println(cache.isKeyInCache("1"));
System.out.println(cache.isKeyInCache("5"));
//8. shut down the cache manager
cm.shutdown();
}
}
输出量
06:03:37.007 [main] WARN n.s.e.config.ConfigurationFactory - No configuration found.
Configuring ehcache from ehcache-failsafe.xml found in the classpath:
//...
Jan
true
false
//...
注意
有关更多API信息,请参阅此执行基本缓存操作
注意
可以通过ehcache.xml
文件配置Ehcache框架,如果该文件不可用,将使用默认的ehcache-failsafe.xml
。
2. Ehcache配置
在此示例中,我们将向您展示如何通过ehcache.xml
文件配置Ehcache。
假设这是一个Gradle项目:
gradle.build
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 'net.sf.ehcache:ehcache:2.9.0'
}
创建一个ehcache.xml
,并将其放入src/main/resources
文件夹。
src/main/resources/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<!-- By default, Ehcache stored the cached files in temp folder. -->
<!-- <diskStore path="java.io.tmpdir" /> -->
<!-- Ask Ehcache to store cache in this path -->
<diskStore path="c:\\cache" />
<!-- Sample cache named cache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp" -->
<cache name="cache1"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
要了解更多信息,请阅读此官方ehcache.xml示例。
HelloEhCache.java
package com.mkyong.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class HelloEhCache{
public static void main(String[] args) {
//1. Create a cache manager
CacheManager cm = CacheManager.newInstance();
//cm.addCache("cache1");
//2. Get a cache called "cache1", declared in ehcache.xml
Cache cache = cm.getCache("cache1");
//3. Put few elements in cache
cache.put(new Element("1","Jan"));
cache.put(new Element("2","Feb"));
cache.put(new Element("3","Mar"));
//4. Get element from cache
Element ele = cache.get("2");
//5. Print out the element
String output = (ele == null ? null : ele.getObjectValue().toString());
System.out.println(output);
//6. Is key in cache?
System.out.println(cache.isKeyInCache("3"));
System.out.println(cache.isKeyInCache("10"));
//7. shut down the cache manager
cm.shutdown();
}
}
输出量
06:45:56.294 [main] DEBUG n.s.e.config.ConfigurationFactory -
Configuring ehcache from ehcache.xml found in the classpath:
file:/C:/Users/mkyong/workspace2/JavaCache/target/classes/ehcache.xml
//...
Feb
true
false
//...
注意
有关加载配置的更多信息
下载源代码
下载它– Java-Ehcache-HelloWorld-Example.zip (11 KB)
参考文献
翻译自: https://mkyong.com/ehcache/ehcache-hello-world-example/