ehcache API

本文详细介绍了Ehcache的使用方法,包括CacheManager的创建、Cache的增删改查操作及事件监听器的自定义实现。此外,还深入解析了Ehcache配置文件中的各项参数及其意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 ehcache API: cache技术向来都是应用的高级话题, 但今天贴的是一个关于ehcache的低级备份.参考官方文档做的一个备份. 1: Using the CacheManager 1.1所有ehcache的使用, 都是从 CacheManager. 开始的.有多种方法创建CacheManager实例: //Create a singleton CacheManager using defaults, then list caches.CacheManager.getInstance() 或者: //Create a CacheManager instance using defaults, then list caches.CacheManager manager = new CacheManager(); String[] cacheNames = manager.getCacheNames(); 如果需要从指定配置文件创建 CacheManager manager1 = new CacheManager("src/config/ehcache1.xml"); CacheManager manager2 = new CacheManager("src/config/ehcache2.xml"); String[] cacheNamesForManager1 = manager1.getCacheNames(); String[] cacheNamesForManager2 = manager2.getCacheNames(); 1.2 Adding and Removing Caches Programmatically手动创建一个cache, 而不是通过配置文件: //creates a cache called testCache, which //will be configured using defaultCache from the configuration  CacheManager singletonManager = CacheManager.create(); singletonManager.addCache("testCache"); Cache test = singletonManager.getCache("testCache"); 或者: //Create a Cache and add it to the CacheManager, then use it. Note that Caches are not usable until they have //been added to a CacheManager.  public void testCreatCacheByProgram() {   CacheManager singletonManager = CacheManager.create();   Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);   singletonManager.addCache(memoryOnlyCache);   Cache testCache = singletonManager.getCache("testCache");   assertNotNull(testCache);    }  手动移除一个cache: //Remove cache called sampleCache1 CacheManager singletonManager = CacheManager.create(); singletonManager.removeCache("sampleCache1"); 1.3 Shutdown the CacheManagerehcache应该在使用后关闭, 最佳实践是在code中显式调用: //Shutdown the singleton CacheManagerCacheManager.getInstance().shutdown();   2 Using Caches比如我有这样一个cache:  <cache name="sampleCache1" maxElementsInMemory="10000"  maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"  diskSpoolBufferSizeMB="20" timeToIdleSeconds="300"  timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" />   2.1 Obtaining a reference to a Cache获得该cache的引用:   String cacheName = "sampleCache1";   CacheManager manager = new CacheManager("src/ehcache1.xml");   Cache cache = manager.getCache(cacheName); 2.2 Performing CRUD operations下面的代码演示了ehcache的增删改查:  public void testCRUD() {   String cacheName = "sampleCache1";   CacheManager manager = new CacheManager("src/ehcache1.xml");   Cache cache = manager.getCache(cacheName);   //Put an element into a cache   Element element = new Element("key1", "value1");   cache.put(element);   //This updates the entry for "key1"   cache.put(new Element("key1", "value2"));   //Get a Serializable value from an element in a cache with a key of "key1".   element = cache.get("key1");   Serializable value = element.getValue();   //Get a NonSerializable value from an element in a cache with a key of "key1".   element = cache.get("key1");   assertNotNull(element);   Object valueObj = element.getObjectValue();   assertNotNull(valueObj);   //Remove an element from a cache with a key of "key1".   assertNotNull(cache.get("key1"));   cache.remove("key1");   assertNull(cache.get("key1"));    }  2.3 Disk Persistence on demand //sampleCache1 has a persistent diskStore. We wish to ensure that the data and index are written immediately.   public void testDiskPersistence() {   String cacheName = "sampleCache1";   CacheManager manager = new CacheManager("src/ehcache1.xml");   Cache cache = manager.getCache(cacheName);   for (int i = 0; i < 50000; i++)  {    Element element = new Element("key" + i, "myvalue" + i);    cache.put(element);   }   cache.flush();   Log.debug("java.io.tmpdir = " + System.getProperty("java.io.tmpdir"));  } 备注: 持久化到硬盘的路径由虚拟机参数"java.io.tmpdir"决定.例如, 在windows中, 会在此路径下C:/Documents and Settings/li/Local Settings/Temp在linux中, 通常会在: /tmp 下 2.4  Obtaining Cache Sizes以下代码演示如何获得cache个数:  public void testCachesizes() {   long count = 5;   String cacheName = "sampleCache1";   CacheManager manager = new CacheManager("src/ehcache1.xml");   Cache cache = manager.getCache(cacheName);   for (int i = 0; i < count; i++)  {    Element element = new Element("key" + i, "myvalue" + i);    cache.put(element);  }   //Get the number of elements currently in the Cache.   int elementsInCache = cache.getSize();   assertTrue(elementsInCache == 5);   //Cache cache = manager.getCache("sampleCache1");   long elementsInMemory = cache.getMemoryStoreSize();   //Get the number of elements currently in the DiskStore.   long elementsInDiskStore = cache.getDiskStoreSize();   assertTrue(elementsInMemory + elementsInDiskStore == count);   }  3: Registering CacheStatistics in an MBeanServerehCache 提供jmx支持: CacheManager manager = new CacheManager(); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(manager, mBeanServer, false, false, false, true); 把该程序打包, 然后:java -Dcom.sun.management.jmxremote -jar 程序名.jar 再到javahome/bin中运行jconsole.exe, 便可监控cache. 4. 用户可以自定义处理cacheEventHandler, 处理诸如元素放入cache的各种事件(放入,移除,过期等事件)只需三步: 4.1 在cache配置中, 增加cacheEventListenerFactory节点.  <cache name="Test" maxElementsInMemory="1" eternal="false"  overflowToDisk="true" timeToIdleSeconds="1" timeToLiveSeconds="2"  diskPersistent="false" diskExpiryThreadIntervalSeconds="1"  memoryStoreEvictionPolicy="LFU">  <cacheEventListenerFactory class="co.ehcache.EventFactory" /> </cache> 4.2: 编写EventFactory, 继承CacheEventListenerFactory: public class EventFactory extends CacheEventListenerFactory  {  @Override  public CacheEventListener createCacheEventListener(Properties properties) {   // TODO Auto-generated method stub   return new CacheEvent(); } }  4.3  编写 class: CacheEvent, 实现 CacheEventListener 接口: public class CacheEvent implements CacheEventListener  {   public void dispose()  {  log("in dispose"); }   public void notifyElementEvicted(Ehcache cache, Element element)  {  // TODO Auto-generated method stub   log("in notifyElementEvicted" + element); }   public void notifyElementExpired(Ehcache cache, Element element) {   // TODO Auto-generated method stub   log("in notifyElementExpired" + element); }   public void notifyElementPut(Ehcache cache, Element element) throws CacheException {   // TODO Auto-generated method stub   log("in notifyElementPut" + element); }  public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {   // TODO Auto-generated method stub   log("in notifyElementRemoved" + element); }   public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {   // TODO Auto-generated method stub  log("in notifyElementUpdated" + element); }   public void notifyRemoveAll(Ehcache cache) {   // TODO Auto-generated method stub   log("in notifyRemoveAll"); }     public Object clone() throws CloneNotSupportedException     {        return super.clone();    }          private void log(String s)    {     Log.debug(s);    }} 现在可以编写测试代码:  public void testEventListener() {   String key = "person";   Person person = new Person("lcl", 100);   MyCacheManager.getInstance().put("Test", key, person);   Person p = (Person) MyCacheManager.getInstance().get("Test", key);   try  {    Thread.sleep(10000);  }   catch (InterruptedException e)   {   // TODO Auto-generated catch block   e.printStackTrace();  }    assertNull(MyCacheManager.getInstance().get("Test", key)); }   根据配置, 该缓存对象生命期只有2分钟, 在Thread.sleep(10000)期间, 该缓存元素将过期被销毁, 在销毁前, 触发notifyElementExpired事件. 二 Ehcache配置文件 以如下配置为例说明:  <cache name="CACHE_FUNC" maxElementsInMemory="2" eternal="false" timeToIdleSeconds="10" timeToLiveSeconds="20" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120"  /> maxElementsInMemory :cache 中最多可以存放的元素的数量。如果放入cache中的元素超过这个数值,有两种情况: 1、若overflowToDisk的属性值为true,会将cache中多出的元素放入磁盘文件中。 2、若overflowToDisk的属性值为false,会根据memoryStoreEvictionPolicy的策略替换cache中原有的元素。 eternal :是否永驻内存。如果值是true,cache中的元素将一直保存在内存中,不会因为时间超时而丢失,所以在这个值为true的时候,timeToIdleSeconds和timeToLiveSeconds两个属性的值就不起作用了。 timeToIdleSeconds :访问这个cache中元素的最大间隔时间。如果超过这个时间没有访问这个cache中的某个元素,那么这个元素将被从cache中清除。 timeToLiveSeconds : cache中元素的生存时间。意思是从cache中的某个元素从创建到消亡的时间,从创建开始计时,当超过这个时间,这个元素将被从cache中清除。 overflowToDisk :溢出是否写入磁盘。系统会根据标签<diskStore path="java.io.tmpdir"/> 中path的值查找对应的属性值,如果系统的java.io.tmpdir的值是 D:/temp,写入磁盘的文件就会放在这个文件夹下。文件的名称是cache的名称,后缀名的data。如:CACHE_FUNC.data。 diskExpiryThreadIntervalSeconds  :磁盘缓存的清理线程运行间隔. memoryStoreEvictionPolicy :内存存储与释放策略。有三个值:LRU -least recently usedLFU -least frequently usedFIFO-first in first out, the oldest element by creation time diskPersistent : 是否持久化磁盘缓存。当这个属性的值为true时,系统在初始化的时候会在磁盘中查找文件名为cache名称,后缀名为index的的文件,如CACHE_FUNC.index 。这个文件中存放了已经持久化在磁盘中的cache的index,找到后把cache加载到内存。要想把cache真正持久化到磁盘,写程序时必须注意,在是用net.sf.ehcache.Cache的void put (Element element)方法后要使用void flush()方法。 更多说明可看ehcache自带的ehcache.xml的注释说明. 感叹: csdn的blog越来越烂了, 在外面编好的格式, 粘贴过来就乱了.很是深恶痛绝!
标题基于SpringBoot+Vue的学生交流互助平台研究AI更换标题第1章引言介绍学生交流互助平台的研究背景、意义、现状、方法与创新点。1.1研究背景与意义分析学生交流互助平台在当前教育环境下的需求及其重要性。1.2国内外研究现状综述国内外在学生交流互助平台方面的研究进展与实践应用。1.3研究方法与创新点概述本研究采用的方法论、技术路线及预期的创新成果。第2章相关理论阐述SpringBoot与Vue框架的理论基础及在学生交流互助平台中的应用。2.1SpringBoot框架概述介绍SpringBoot框架的核心思想、特点及优势。2.2Vue框架概述阐述Vue框架的基本原理、组件化开发思想及与前端的交互机制。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue在学生交流互助平台中的整合方式及优势。第3章平台需求分析深入分析学生交流互助平台的功能需求、非功能需求及用户体验要求。3.1功能需求分析详细阐述平台的各项功能需求,如用户管理、信息交流、互助学习等。3.2非功能需求分析对平台的性能、安全性、可扩展性等非功能需求进行分析。3.3用户体验要求从用户角度出发,提出平台在易用性、美观性等方面的要求。第4章平台设计与实现具体描述学生交流互助平台的架构设计、功能实现及前后端交互细节。4.1平台架构设计给出平台的整体架构设计,包括前后端分离、微服务架构等思想的应用。4.2功能模块实现详细阐述各个功能模块的实现过程,如用户登录注册、信息发布与查看、在线交流等。4.3前后端交互细节介绍前后端数据交互的方式、接口设计及数据传输过程中的安全问题。第5章平台测试与优化对平台进行全面的测试,发现并解决潜在问题,同时进行优化以提高性能。5.1测试环境与方案介绍测试环境的搭建及所采用的测试方案,包括单元测试、集成测试等。5.2测试结果分析对测试结果进行详细分析,找出问题的根源并
内容概要:本文详细介绍了一个基于灰狼优化算法(GWO)优化的卷积双向长短期记忆神经网络(CNN-BiLSTM)融合注意力机制的多变量多步时间序列预测项目。该项目旨在解决传统时序预测方法难以捕捉非线性、复杂时序依赖关系的问题,通过融合CNN的空间特征提取、BiLSTM的时序建模能力及注意力机制的动态权重调节能力,实现对多变量多步时间序列的精准预测。项目不仅涵盖了数据预处理、模型构建与训练、性能评估,还包括了GUI界面的设计与实现。此外,文章还讨论了模型的部署、应用领域及其未来改进方向。 适合人群:具备一定编程基础,特别是对深度学习、时间序列预测及优化算法有一定了解的研发人员和数据科学家。 使用场景及目标:①用于智能电网负荷预测、金融市场多资产价格预测、环境气象多参数预报、智能制造设备状态监测与预测维护、交通流量预测与智慧交通管理、医疗健康多指标预测等领域;②提升多变量多步时间序列预测精度,优化资源调度和风险管控;③实现自动化超参数优化,降低人工调参成本,提高模型训练效率;④增强模型对复杂时序数据特征的学习能力,促进智能决策支持应用。 阅读建议:此资源不仅提供了详细的代码实现和模型架构解析,还深入探讨了模型优化和实际应用中的挑战与解决方案。因此,在学习过程中,建议结合理论与实践,逐步理解各个模块的功能和实现细节,并尝试在自己的项目中应用这些技术和方法。同时,注意数据预处理的重要性,合理设置模型参数与网络结构,控制多步预测误差传播,防范过拟合,规划计算资源与训练时间,关注模型的可解释性和透明度,以及持续更新与迭代模型,以适应数据分布的变化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值