1 package org.jeecgframework.core.util;
 2
 3 import net.sf.ehcache.Cache;
 4 import net.sf.ehcache.CacheManager;
 5 import net.sf.ehcache.Element;
 6
 7 /**
 8  * ehcache 缓存工具类
 9  *
10  * cacheName在ehcache.xml中配置
11  */
12 public class EhcacheUtil {
13
14     public static CacheManager manager = CacheManager.create();
15
16     public static Object 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                 return element.getObjectValue();
22             }
23         }
24         return null;
25     }
26
27     public static void put(String cacheName, Object key, Object value) {
28         Cache cache = manager.getCache(cacheName);
29         if (cache != null) {
30             cache.put(new Element(key, value));
31         }
32     }
33
34     public static boolean remove(String cacheName, Object key) {
35         Cache cache = manager.getCache(cacheName);
36         if (cache != null) {
37             return cache.remove(key);
38         }
39         return false;
40     }
41
42     public static void main(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 for timeToIdleSeconds,timeToLiveSecond,diskExpiryThreadIntervalSeconds
32         Use ehcache-safe.xml default for maxElementsInMemory,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

转载于:https://www.cnblogs.com/Eeexiang/p/9146656.html

Ehcache 缓存相关推荐

  1. Spring Boot 2.x基础教程:使用EhCache缓存集群

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 上一篇我们介绍了在Spring Boot中整合EhCac ...

  2. mybatis配置ehcache缓存

    1:在spring配置文件中加载缓存配置文件 <!-- 使用ehcache缓存 --> <bean id="ehCacheManager" class=" ...

  3. Ehcache 缓存监控配置

    监控 ehcache缓存: 1,下载: http://terracotta.org/downloads/open-source/destination?name=ehcache-monitor-kit ...

  4. shiro教程:整合ehcache缓存

    这个是在ssm的基础上再去整合shiro和ehcache的,整合ehcache主要是为了减少后台shiro拦截的次数,因为如果我们不使用缓存的话,后台shiro的认证和授权的拦截器就会反复的进行拦截, ...

  5. javaweb项目搭建ehcache缓存系统

    转载自  javaweb项目搭建ehcache缓存系统 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider,同时在项目开发中 ...

  6. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  7. 一文玩转 EhCache 缓存框架!

    Ehcache 介绍 EhCache 从 Hibernate 发展而来,是一个纯Java的进程内缓存框架,具有快速.精干等特点.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存, ...

  8. SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)

    1.说明 Spring定义了CacheManager和Cache接口, 用来统一不同的缓存技术, 例如JCache,EhCache,Hazelcast,Guava,Redis等. 本文通过Spring ...

  9. Hibernate性能优化之EHCache缓存

    像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...

最新文章

  1. python文件的用法_Python文件读写常见用法总结
  2. 程序语言python循环_Python语言程序设计之一--for循环
  3. 单向链表的 js 实现
  4. 关于Java栈与堆的思考
  5. 聊一下CPU占用高的解决方案
  6. 初学者python笔记(内置函数_1)
  7. linux系统内核参数命令,Linux内核启动参数解析及添加
  8. hive-0.11.0安装方法具体解释
  9. AutoCAD块属性提取
  10. CAD 删除 _ArchTick 块定义
  11. 『免费+批量』英文论文下载神器
  12. xubuntu切换回到ubuntu登陆界面
  13. Flutter开发环境配置
  14. 获取列——Excel的COLUMN、COLUMNS
  15. c语言 派生,继承和派生
  16. Dell灵越燃7000网络驱动被误删后无法安装解决方案
  17. 普通用户使用su无法切到root用户的解决方法
  18. 百度地图html页面设置大小设置,响应适老化!百度地图全新上线地图显示大小调节功能...
  19. PHP做大转盘抽奖的思路,jQuery实现幸运大转盘(php抽奖程序)抽奖程序
  20. 计算机无法共享的原因,文件夹无法共享的原因及其解决办法

热门文章

  1. 各种PID算法的整理和总结
  2. 汇编:call指令的应用
  3. Linux单网卡多个IP(或者多个网卡多个IP)设置
  4. 在 Android Studio 2.2 中愉快地使用 C/C++
  5. 【20160924】GOCVHelper 图像处理部分(2)
  6. 如何解决开发人员的工作无法量化的问题
  7. 摇滚吧HTML5!Jsonic超声波前端交互!
  8. Config Sharepoint 2013 Workflow PowerShell Cmdlet
  9. 易语言读写配置项ini文件
  10. Movavi PDF Editor 3中文版