缓存数据 ehcache
2016-03-09 15:42
232 查看
ehcache-core-2.6.5.jar
1.创建CacheManager
读取配置文件中的 Cache
2.1 创建1个Cache
3.放入元素
4.关闭
xml文件的样子。
或
spring文件中的配置
如果配置文件配置了的话
直接用
CacheManager manager = CacheManager.create(); 就好了
1.创建CacheManager
CacheManager manager = CacheManager.create(); CacheManager manager = CacheManager.create("xx/xx/xx/ehcache.xml"); InputStream fis = new FileInputStream(new File("xx/xx/ehcache.xml").getAbsolutePath()); try { manager = CacheManager.create(fis); } finally { fis.close(); }
读取配置文件中的 Cache
Cache ca = manager.getCache("accesstokenCache"); Element element = ca.get(weixintokenkey); if(element == null){ }else{ String token = element.getValue().toString(); }
2.1 创建1个Cache
CacheManager manager = CacheManager.create(); Cache cache = new Cache("test", 1, true, false, 5, 2); manager.addCache(cache); //这里在CacheManager中直接加入了一个的cache。如果不设置其他参数, //只给了一个名字,所以系统会把defaultCache的设置给它clone一份。 //使用方法,像字典一样使用就行: String names[] = manager.getCacheNames(); for(int i=0;i<names.length;i++){ System.out.println(names[i]); }
3.放入元素
Element element = new Element(key,value); demo.put(element);
4.关闭
manager.shutdown();
xml文件的样子。
<ehcache> <diskStore path="java.io.tempdir" /> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <cache name="ehcacheName" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000" overflowToDisk="true" /> </ehcache>
或
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir"/> <defaultCache maxEntriesLocalHeap="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="1800" timeToLiveSeconds="3600"> </defaultCache> <!--system cache name sysCache--> <cache name="sysCache" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="20000" eternal="false" overflowToDisk="false" diskSpoolBufferSizeMB="50" timeToIdleSeconds="1800" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" transactionalMode="off" /> <cache name="accesstokenCache" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="20000" eternal="false" overflowToDisk="false" diskSpoolBufferSizeMB="50" timeToIdleSeconds="7000" timeToLiveSeconds="7000" memoryStoreEvictionPolicy="LFU" transactionalMode="off" /> </ehcache> <!-- maxEntriesLocalHeap="10000" 内存最多1W条--> <!-- maxEntriesLocalDisk="20000" 磁盘最多2W条--> <!-- eternal="false" 是否自动过期--> <!-- overflowToDisk="true" 是否自动存磁盘--> <!-- diskSpoolBufferSizeMB="50" DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore--> <!-- timeToIdleSeconds="1800" 空闲过期时间--> <!-- timeToLiveSeconds="3600" 最长使用时间--> <!-- memoryStoreEvictionPolicy="LFU" 缓存策略--> <!-- transactionalMode="off" 使ehcache作为JTA事务的参与者--> <!-- FIFO (先进先出). --> <!-- LFU 最少使用) --> <!-- LRU 最近最少使用(ehcache 默认值) -->
spring文件中的配置
如果配置文件配置了的话
直接用
CacheManager manager = CacheManager.create(); 就好了
声明cacheManager <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"></bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="cacheManagerFactory" ></bean>
相关文章推荐
- xml(ibatis配置)中CDATA的用法
- 字符流中第一个不重复的字符
- Nginx支持php配置
- .net面试总结
- 作业一:(2)课程进度总结
- document.getElementsByTagName
- 从setContentView分析Android加载布局的流程
- iOS多线程开发
- vector resize reserve区别
- 使用Unity发布第一个程序时遇到的问题
- Tcp
- C#第七节课
- Oracle 截取字符串,取系统时间
- 协议(protocol)与网络协议
- Android 6.0 新特性(官方文档翻译)
- php session过期时间设置,纯新手教学
- 第一场
- MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
- 表示数值的字符串
- Makefile学习笔记(一):Windows平台(转载)