Ehcache 缓存

摘要:
=null){19Elementelement=cache.get(key);20if(element!=null){21returnelement.getObjectValue();22}23}24returnnull;25}2627publicstaticvoidput(StringcacheName,Objectkey,Objectvalue){28Cachecache=manager.getCache(cacheName);29if(cache!=null){30cache.put(newElement(key,value));31}32}3334publicstaticbooleanremove(StringcacheName,Objectkey){35Cachecache=manager.getCache(cacheName);36if(cache!=null){37returncache.remove(key);38}39returnfalse;40}4142publicstaticvoidmain(String[]args){43Stringkey="key";44Stringvalue="hello";45EhcacheUtil.put("mytest",key,value);46//System.out.println(EhcacheUtil.get("mytest",key));47}4849}123456˂!
1 packageorg.jeecgframework.core.util;
2 
3 importnet.sf.ehcache.Cache;
4 importnet.sf.ehcache.CacheManager;
5 importnet.sf.ehcache.Element;
6 
7 /**
8 * ehcache 缓存工具类
9 * 
10 * cacheName在ehcache.xml中配置
11  */
12 public classEhcacheUtil {
13 
14     public static CacheManager manager =CacheManager.create();
15 
16     public staticObject get(String cacheName, Object key) {
17         Cache cache =manager.getCache(cacheName);
18         if (cache != null) {
19             Element element =cache.get(key);
20             if (element != null) {
21                 returnelement.getObjectValue();
22 }
23 }
24         return null;
25 }
26 
27     public static voidput(String cacheName, Object key, Object value) {
28         Cache cache =manager.getCache(cacheName);
29         if (cache != null) {
30             cache.put(newElement(key, value));
31 }
32 }
33 
34     public static booleanremove(String cacheName, Object key) {
35         Cache cache =manager.getCache(cacheName);
36         if (cache != null) {
37             returncache.remove(key);
38 }
39         return false;
40 }
41 
42     public static voidmain(String[] args) {
43         String key = "key";
44         String value = "hello";
45         EhcacheUtil.put("mytest", key, value);
46         //System.out.println(EhcacheUtil.get("mytest", key));
47 }
48 
49 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ehcache updateCheck="false">
3 
4     <diskStore path="java.io.tmpdir" />
5 
6     <!-- Cluster localhost setting -->
7     <cacheManagerPeerProviderFactory
8         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
9         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
10         multicastGroupPort=4446, timeToLive=32"/>
11     
12     <cacheManagerPeerListenerFactory
13         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
14         properties="hostName=localhost, port=40001,socketTimeoutMillis=2000" />
15 
16 
17     <cache name="dictCache" maxElementsInMemory="500" overflowToDisk="true"
18         eternal="true">
19         <cacheEventListenerFactory
20             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
21             properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
22     </cache>
23 
24     <cache name="eternalCache" maxElementsInMemory="500"
25         overflowToDisk="true" eternal="false" timeToIdleSeconds="1200"
26         timeToLiveSeconds="1200">
27         <cacheEventListenerFactory
28             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
29             properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
30     </cache>
31     <!-- DefaultCache setting. Modify ehcache-safe.xml fortimeToIdleSeconds,timeToLiveSecond,diskExpiryThreadIntervalSeconds 
32         Use ehcache-safe.xml default formaxElementsInMemory,maxElementsOnDisk,overflowToDisk,eternal 
33         Use ehcache default for memoryStoreEvictionPolicy,diskPersistent,. -->
34     <defaultCache maxElementsInMemory="10000" overflowToDisk="true"
35         eternal="false" memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000000"
36         diskExpiryThreadIntervalSeconds="600" timeToIdleSeconds="3600"
37         timeToLiveSeconds="100000" diskPersistent="false" />
38 </ehcache>
View Code

免责声明:文章转载自《Ehcache 缓存》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Eclipse java文件、包、工程左下角有感叹号原因及处理方法uni-app 开发小程序 预览包过大下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ehcache 分布式集群同步数据实例

本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可。直奔主题,可运行的demo实例! 创建一个maven项目,配置pom pom.xml <dependencies> <dependency> <groupId>net.s...

mybatis 详解(九)------ 一级缓存、二级缓存

上一章节,我们讲解了通过mybatis的懒加载来提高查询效率,那么除了懒加载,还有什么方法能提高查询效率呢?这就是我们本章讲的缓存。   本篇源码下载链接:http://pan.baidu.com/s/1eRHTsIm 密码:a5wn   mybatis 为我们提供了一级缓存和二级缓存,可以通过下图来理解:      ①、一级缓存是SqlSession级别...

EhCache缓存的使用

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。   我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网页,都把Url存储到缓存中,并且每次爬某个网页之前,都去缓存中搜索下,假如存在的话,我们就不要爬取这个网页了,不存在的话,我们就爬下网页,爬取成功后,把这...

12、MyBatis教程之缓存

13、缓存 简介 1、什么是缓存 [ Cache ]? 存在内存中的临时数据。 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。 2、为什么使用缓存? 减少和数据库的交互次数,减少系统开销,提高系统效率。 3、什么样的数据能使用缓存? 经常查询...

设置缓存的大小

目录 1     CacheManager级别 2     Cache级别 3     大小衡量 4     配置大小示例        缓存大小的限制可以设置在CacheManager上,也可以设置在单个的Cache上。我们可以设置缓存使用内存的大小,也可以设置缓存使用磁盘的大小,但是使用堆内存的大小是必须设置的,其它可设可不设,默认不设就是无限制。在设...

学习Spring Boot:(二十五)使用 Redis 实现数据缓存

前言 由于 Ehcache 存在于单个 java 程序的进程中,无法满足多个程序分布式的情况,需要将多个服务器的缓存集中起来进行管理,需要一个缓存的寄存器,这里使用的是 Redis。 正文 当应用程序要去缓存中读取数据,但是缓存中没有找到该数据,则重新去数据库中获取数据,然后将数据存入缓存中。 还有当我们需要更新或者删除缓存中的数据时候,需要让缓存失效...