博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ehcache 的使用
阅读量:7223 次
发布时间:2019-06-29

本文共 1809 字,大约阅读时间需要 6 分钟。

hot3.png

一直以来懒得配置缓存,基本的缓存也就是orm层,基本上都交给hibernate去配置了。这段时间,感觉页面速度太慢了,还是需要使用缓存。现在的缓存工具也挺多的,较不错的属ehcache和oscache了。决定分别研究一下。
先来说说ehcache,目前的版本为1.2,已经支持集群了。对于ehcache的使用,感觉很容易上手,基本上都是配置。以前在hibernate的时候配置过,所以也不是很陌生。API也挺简单,如下的api:
CacheManager主要的缓存管理类,一般一个应用为一个实例,如下
CacheManager.create();也可以使用new CacheManager的方式创建
默认的配置文件为ehcache.xml文件,也可以使用不同的配置:
CacheManager manager = new CacheManager("src/config/other.xml");
缓存的创建,采用自动的方式
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
或者直接创建Cache
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
删除cache
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");
在使用ehcache后,需要关闭
CacheManager.getInstance().shutdown()
caches 的使用
Cache cache = manager.getCache("sampleCache1");
执行crud操作
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);
update
Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1");
//This updates the entry for "key1"
cache.put(new Element("key1", "value2");
get Serializable
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Serializable value = element.getValue();
get non serializable
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Object value = element.getObjectValue();
remove
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1"
cache.remove("key1");
不过缓存还是基本上以配置方式为主,下一篇文章将会说明ehcache如何配置

转载于:https://my.oschina.net/meSpace/blog/95009

你可能感兴趣的文章
Eclipse打包插件Fat Jar 解压打包
查看>>
Apache Shiro 使用手册
查看>>
CentOS mini 6.5 安装DB2 Express-C 问题处理记录
查看>>
DirectByteBuffer
查看>>
Docker Compose文件详解 V2
查看>>
Memcached的原理与应用(未完)
查看>>
基于 Confluence 6 数据中心的 SAML 单点登录设置你的身份提供者
查看>>
mysql总结
查看>>
Navicat for MySQL版本更新至v11.2.12,修复多项问题|附下载
查看>>
整理 JAVA中的IO流 (字符流和字节流两个大类)
查看>>
uefi与win8 (根据网络资料整理)
查看>>
Eclipse优化
查看>>
Log4j tutorial with Tomcat examples
查看>>
Kong 网关
查看>>
三层结构视频中的DBHelper.cs
查看>>
[转载] 信息系统项目管理师视频教程——18 项目沟通管理
查看>>
在Windows下建立QT开发环境
查看>>
Jedis、JedisPool、ShardedJedis和ShardedJedisPool,java对redis的基本操作
查看>>
[转载] 致命伴侣
查看>>
HTML5 localStorage本地存储实际应用举例
查看>>