闲人野居
好好学习,天天向上
posts - 57,  comments - 137,  trackbacks - 0

    一直以来懒得配置缓存,基本的缓存也就是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如何配置
    

    
    

posted on 2007-02-28 20:14 布衣郎 阅读(24185) 评论(15)  编辑  收藏 所属分类: jdk相关

FeedBack:
# re: 使用ehcache
2007-03-01 20:58 | 山风小子
如果有个完整的小例子就更好了:)  回复  更多评论
  
# re: 使用ehcache
2007-03-02 17:34 | itVincent
同意楼上,有个例子更好  回复  更多评论
  
# re: 使用ehcache
2008-01-01 17:42 | bizairshop
跟hibernate结合在一起的时候不需要写这些代码,只要配置一下即可。
航服机票推荐 http://www.bizairshop.com   回复  更多评论
  
# re: 使用ehcache
2008-01-19 08:52 | tt168
要是没用hibernate呢
我用的jsp+javabeans,请问怎么用ehcache

我的MSN:mugesoft@hotmail.com,欢迎指教  回复  更多评论
  
# re: 使用ehcache[未登录]
2008-05-12 09:38 | jacky
要是没用hibernate呢
我用的jsp+javabeans,请问怎么用ehcache

我的MSN:jackyxiao@live.cn,欢迎指教  回复  更多评论
  
# 我很爱钱
2008-05-20 21:22 | 爱看书
北京到卡诺机票|北京到卡诺飞机票价格查询|预定北京到卡诺机票

航程: 卡诺机票,北京到卡诺机票
机票价格: 单程:4500元 往返:6200元
航空公司: 中国国际航空公司
税费规定: 国际机票的价格由票价、税金、燃油费组成。航空公司、舱位、有效期、退改签和机票限制等规定决定了票价的高低;税金因为根据每天的汇率会不同,所以具体金额会以您出票当天为准.机场建设费则是由民航总局规定乘坐国际航班,每人90元,婴儿、儿童免收机场建设费.此价格在有效期内针对中国国内乘客有效,
说明: 1.儿童票价为成人票价的75%。婴儿票价为公布运价的10%。
2.可在机票有效期内免费修改预订.
4.以上票价以人民币结算,不含税,税后价格以电话查询为准.
退票条件:以承运航空公司退票规定退票
<a href="http://2008sjjp.com/jipiao/115617681.html">北京到卡诺机票.北京到卡诺机票价格.北京-卡诺航班诺</a>
<a href="http://2008sjjp.com/jipiao/115546152.html">北京到拉各斯机票.北京到拉各斯机票价格.北京-拉各斯航班</a>
  回复  更多评论
  
# re: 使用ehcache
2008-05-29 21:19 | BT下载
http://www.5a520.cn 小说520网  回复  更多评论
  
# re: 使用ehcache
2008-12-16 15:49 | 养藏獒
如果详细点就好了
有点不明白  回复  更多评论
  
# re: 使用ehcache
2009-03-17 16:28 | 12312
# re: 使用ehcache
2012-07-02 09:44 | 马云
# re: 使用ehcache
2012-07-02 09:45 | 马云

只有注册用户登录后才能发表评论。


网站导航:
 

<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

常用链接

留言簿(12)

随笔分类(59)

随笔档案(57)

blog

java

uml

搜索

  •  

积分与排名

  • 积分 - 355562
  • 排名 - 154

最新评论

阅读排行榜

评论排行榜