java web 页面缓存技术_JAVA Web缓存 - Mr_Harlan的个人页面 - OSCHINA - 中文开源技术交流社区...

JAVA缓存最简单一种实现

创建一个静态的map     ConcurrentHashMap  线程安全

本地文件缓存

Ehcache 缓存

jar包

ehcache-core-2.5.2.jar    slf4j-api-1.6.1.jar   slf4j-jdk14-1.6.1.jar

配置  ehcache.xml

可配置多个cache

通过  Cache sample = cacheManager.getCache("name");获取缓存对象

xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">

JAVA  代码

1.获取CacheManager 对象  不传参数表示默认路径  可以传入文件路径和网络地址

2.CacheManager cacheManager =  CacheManager.create();

获取ehcache.xml 文件里配置好的 cache

Cache sample = cacheManager.getCache("SimplePageCachingFilter");

3.创建 缓存元素  key 和 val 字面意思

Element element = new Element("key", "val");

4.sample.put(element);  将缓存对象 加入缓存

通过key获取cache中的 缓存元素

Element result = sample.get("key");

注意添加到cache中对象要序列化 实现Serializable接口

通过 次元素 可获取 val等 属性的值

删除缓存

sample.remove("key");

sample.removeAll();

获取所有的缓存对象

for (Object key : cache.getKeys()) {

System.out.println(key);

}

得到缓存中的对象数

cache.getSize();

得到缓存对象占用内存的大小

cache.getMemoryStoreSize();

得到缓存读取的命中次数

cache.getStatistics().getCacheHits();

得到缓存读取的错失次数

cache.getStatistics().getCacheMisses();

写入磁盘

cache.flush();

要想把cache真正持久化到磁盘,写程序时必须注意,在是用net.sf.ehcache.Cache的void put (Element element)方法后要使用void flush()方法

创建个缓存

Ehcache cache = new Cache("testCache", 5000, false, false, 5, 2);

sample.add(cache )

Ehcache整合MyBatis

导入 mybatis-ehcache-1.0.0.jar  还有其他myBatis相关包

mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * fr

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值