<ehcache>
<!-- 硬盘的缓存位置 -->
<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>
<cache name="HelloWorldCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
<ehcache>
<!-- 硬盘的缓存位置 -->
<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>
<cache name="HelloWorldCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
package com.alatus.ehcache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.io.InputStream;
public class TestEh {
public static void main(String[] args) {
// 读取配置信息,获取流对象
InputStream input = TestEh.class.getClassLoader().getResourceAsStream("ehcache.xml");
// 获取缓存管理对象
CacheManager cacheManager = new CacheManager(input);
// 获取缓存对象
Cache cache = cacheManager.getCache("HelloWorldCache");
// 创建缓存数据
Element element = new Element("username","Jack");
// 存入缓存
cache.put(element);
// 取出值
Element cacheInfo = cache.get("username");
System.out.println(cacheInfo);
System.out.println(cacheInfo.getObjectValue());
}
}
package com.alatus.ehcache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.io.InputStream;
public class TestEh {
public static void main(String[] args) {
// 读取配置信息,获取流对象
InputStream input = TestEh.class.getClassLoader().getResourceAsStream("ehcache.xml");
// 获取缓存管理对象
CacheManager cacheManager = new CacheManager(input);
// 获取缓存对象
Cache cache = cacheManager.getCache("HelloWorldCache");
// 创建缓存数据
Element element = new Element("username","Jack");
// 存入缓存
cache.put(element);
// 取出值
Element cacheInfo = cache.get("username");
System.out.println(cacheInfo);
System.out.println(cacheInfo.getObjectValue());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.alatus</groupId>
<artifactId>ehcache</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.6.11</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.alatus</groupId>
<artifactId>ehcache</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.6.11</version>
<type>pom</type>
</dependency>
</dependencies>
</project>