Ehcache你好世界示例

ehcache-徽标

在本教程中,我们将向您展示两个示例,以帮助您开始使用Ehcache

使用的工具 :

  1. 高速缓存2.9
  2. Maven 3
  3. 摇篮2
  4. JDK 1.7

PS Ehcache需要JDK 1.5或更高版本。

1.项目目录结构

ehcache-hello-world。

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)

参考文献

  1. ehcache文档
  2. ehcache.xml示例
  3. 维基百科:Ehcache
  4. Java –从资源文件夹中读取文件

翻译自: https://mkyong.com/ehcache/ehcache-hello-world-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值