Ehcache缓存配置详解编程语言
2023-06-13 09:11:51 时间
下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了。
简单的使用真的很简单。但只能做为入门级了。
1.ehcache.xml,可放classpath根目录下,
ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true" diskStore path="java.io.tmpdir" / // 缓存存放在内存中
diskStore path="G:/ehcache/temp/"/ //缓存存放在磁盘中 defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" cache name="DEFAULT_CACHE" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /ehcache
name: cache的名字,用来识别不同的cache,必须惟一。 maxElementsInMemory: 内存管理的缓存元素数量最大限值。 maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。 eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。 overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。 2.下面是一些可选属性: timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。 timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。 diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。 diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。 diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。 memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用 --
2.第二步,配置applicationContext-ehcache.xml,与spring整合文件
?xml version="1.0" encoding="UTF-8"? beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName" default-lazy-init="false" !-- 引用ehCache的配置 -- bean id="defaultCacheManager" property name="configLocation" value classpath:ehcache.xml /value /property /bean !-- 定义ehCache的工厂,并设置所使用的Cache name -- bean id="ehCache" property name="cacheManager" ref local="defaultCacheManager"/ /property property name="cacheName" value DEFAULT_CACHE /value /property /bean /beans
实际上这样就把两者结合起来了。当然集群的话还得另外配置,这里只讲最简单的。
下面使用:
3. 添加数据到缓存:
net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache"); net.sf.ehcache.Element lgEle=new net.sf.ehcache.Element("loginName", users.getLoginName()); net.sf.ehcache.Element pwdEle=new net.sf.ehcache.Element("password", users.getPassword()); ehCache.put(lgEle); ehCache.put(pwdEle);
这样使用就可。
当然,在spring管理的bean中,也可:
private Cache ehCache; @Resource(name="ehCache") public void setEhCache(Cache ehCache) { this.ehCache = ehCache; }
4.使用。
这个其实就不用说了,大家都会了,我相信,能过对应的key值去获取就是了。
如: Element lgEle= ehCache.get( loginName );
需要修改,就先取得再修改,删除就直接删除。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19491.html
cjavaxml相关文章
- hibernate二级缓存作用、配置
- nextCloud配置redis缓存
- webpack5 配置多线程和缓存加快构建速度
- react脚手架配置px自动转rem
- 振弦采集模块配置工具VMTool 扩展功能数据处理
- Linux 中关于缓存释放的详解与配置
- 【错误记录】NDK 配置错误 ( C/C++ debug|arm64-v8a : Could not get version from cmake.dir path )
- Oracle 参数 PLSQL_CODE_TYPE 官方解释,作用,如何配置最优化建议
- SAP-EWM/ECC 系统后台配置详解编程语言
- 配置Linux下NPM的路径(linuxnpm路径)
- Linux下快速配置无线网络(linux无线连接)
- Linux配置BCache缓存系统提升性能(linuxbcache)
- MySQL实例配置指南:简单快速正确!(mysql实例配置)
- 如何优化Redis缓存配置(redis缓存配置)
- 优化MySQL并发配置,提升数据库性能(mysql并发配置)
- Redis缓存调优:合理配置时间·高效加速(redis缓存时间配置)
- 如何配置Oracle透明网关? 25字(oracle透明网关配置)
- 简易指南如何配置Redis缓存(怎么配置redis缓存)
- CDH与MySQL配置之路实现完美结合(cdh mysql 配置)
- 调整c3p0连接池配置实现oracle数据库连接(c3p0oracle配置)
- Redis缓存配置实现最优性能(redis 配置缓存时间)
- 利用Redis调整缓存容量来提高性能(redis 配置缓存大小)
- 缓存Redis定时自动清除缓存设置(redis配置定时清空)
- Oracle TNS目录指南揭开TNS配置之谜(oracle tns目录)
- apache中使用.htaccess文件缓存图片的配置方法
- MySQL性能优化之Open_Table配置参数的合理配置建议
- 基于NodeJS的前后端分离的思考与实践(三)轻量级的接口配置建模框架