java项目中ehcache缓存最简单用法
java项目中ehcache缓存最简单用法:
1.下载ehcache-core-2.4.3.jar复制到项目的lib目录下
2.新建ehcache.xml文件,放置在项目src目录下的resource目录下。ehcache.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false">
<diskStore path="java.io.tmpdir" />
<cache name="dictCache" maxElementsInMemory="500" overflowToDisk="true"
eternal="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
</cache>
<defaultCache maxElementsInMemory="10000" overflowToDisk="true"
eternal="false" memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="600" timeToIdleSeconds="3600"
timeToLiveSeconds="100000" diskPersistent="false" />
</ehcache>
3.在项目启动的时候加载ehcache.xml文件。新建一个工具类TableCache.lava。在项目启动的时候调用:TableCache.getInstance();即可加载ehcache.xml
public class TableCache {
private static final String path = "resource/ehcache.xml";
private static URL url;
private static CacheManager manager;
private static TableCache ehCache;
private TableCache(String path) {
url = getClass().getResource(path);
manager = CacheManager.create(url);
}
public static TableCache getInstance() {
if (ehCache== null) {
ehCache= new TableCache(path);
}
return ehCache;
}
}
4.使用方法:
4.1在ehcache.xml中声明了一个名字为dictCache的缓存,在java代码中获取它
Cache ehCache== CacheManager.getInstance().getCache("dictCache");
4.2 把对象存入到缓存中
Element element = new Element("str", "我是谁");//str是键,可以通过键获取值"我是谁"
ehCache.put(element);
4.3获取缓存中的值
Element element = ehCache.get("str");
String str= (String)element.getObjectValue();
java项目中ehcache缓存最简单用法的更多相关文章
- JAVA项目中公布WebService服务——简单实例
1.在Java项目中公布一个WebService服务: 怎样公布? --JDK1.6中JAX-WS规范定义了怎样公布一个WebService服务. (1)用jdk1.6.0_21以后的版本号公布. ( ...
- 实战派 | Java项目中玩转Redis6.0客户端缓存!
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 在前面的文章中,我们介绍了Redis6.0中的新特性客户端缓存client-side caching,通过tel ...
- redis在java项目中的使用
在上一篇文章中已经讲了redis的spring配置,这篇将会描述redis在java项目中的使用. redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字 ...
- Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍
大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...
- 在Java项目中整合Scala
Scala是一个运行在Java JVM上的面向对象的语言.它支持函数编程,在语法上比Java更加灵活,同时通过Akka库,Scala支持强大的基于Actor的多线程编程.具有这些优势,使得我最近很想在 ...
- ckeditor编辑器在java项目中配置
一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将 ...
- Redis学习笔记之二 :在Java项目中使用Redis
成功配置redis之后,便来学习使用redis.首先了解下redis的数据类型. Redis的数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set( ...
- JAVA项目中常用的异常处理情况总结
JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...
- Ant在Java项目中的使用(一眼就看会)
参考:http://www.cnblogs.com/zhengqiang/p/5557155.html Ant是跨平台的构建工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将A ...
随机推荐
- Android测试monkeyRunner
monkeyrunner的官方文档: https://developer.android.com/studio/test/monkeyrunner monkeyrunner脚本可以执行截图操作 具体执 ...
- 15 webpack中使用url-loader处理字体文件
引用字体图标,bootstrap有提供字体图标 1.安装bootstrap cnpm i bootstrap -S 2.导入bootstrap //注意:如果要通过路径的形式,去引入node_modu ...
- Mac 升级 Python2.7 到 Python3.5
1.去 Python 官网下载一个版本的包 https://www.python.org/downloads/mac-osx/ 2.安装之后,去 /Library/Frameworks/Python ...
- Greenplum 调优--查看子节点SQL运行状态
摘自<Greenplum企业应用实战> 重点: 使用gp_dist_random函数,将查询下发到每个Segement 创建查看子节点SQL运行状态视图 1)创建v_active_sql视 ...
- Greenplum 常用数据字典
一.数据库集群信息 1.gp_segment_configration 2.pg_filespace_entry 这两个表是在pg_global表空间下面的,是全局表. 用来查看集群segment信息 ...
- Linux系统出现hung_task_timeout_secs和blocked for more than 120 seconds的解决方法
Linux系统出现系统没有响应. 在/var/log/message日志中出现大量的 “echo 0 > /proc/sys/kernel/hung_task_timeout_secs" ...
- 用免费的webservice查询天气
亲测能用URL地址:https://blog.csdn.net/qq_37171353/article/details/79415960 wsimport -s . file:///D:weath.w ...
- 使用webuploader实现断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- 微信小程序分页显示
<view class="tabNav"> <view wx:for="{{navTab}}" wx:key="index" ...
- python map() 的使用方法
>>>def square(x) : # 计算平方数 ... ... >>> map(square, [,,,,]) # 计算列表各个元素的平方 [, , , , ...