Ehcache入门基础
1.ehcache的简介
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
2.ehcache入门实例
1.首先先导入ehcache相关的jar依赖,我这里有的是maven项目做得演示,所以要在pom.xml里面添加依赖。
1 2 3 4 5 | < 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。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? 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.测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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.输出结果
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步