Ehcache使用实例(二)

Cache使用

l 得到一个Cache引用

获得一个sampleCache1的引用,从官方下载ehcache.xml,在ehcache.xml中已经有配置好的缓存,大家直接使用就可以,或是做测试,如果说真正使用的时候,最后自己手动配置一个比较好。

Cache cache = manager.getCache(“sampleCache1”);

l 使用Cache

Put一个Element到cache中

Cache cache = manager.getCache(“sampleCache1”);

Element element = new Element(“key1”,”value1”);

cache.put(element);

update一个element时,只要在构造element时将相同的key传入,在调用cache.put(element),这是Ehcache会根据key到缓存中找到对应的element并做更新。

Cache cache = manager.getCache(“sampleCache1”);

Cache.put(new Element(“key1”, “value1”));

//更新element

Cache.put(new Element(“key1”, “value2”));

根据key取得对应element的序列化value值

Cache cache = manager.getCache(“sampleCache1”);

Element element = cache.get(“key1”);

Serializable value = element.getValue();

根据key取得对应element的非序列化value值

Cache cache = manager.getcache(“samplecache1”);

Element element = cache.get(“key1”);

Ojbect value = element.getObjectValue();

http://blog.csdn.net/mgoann/archive/2009/04/22/4099190.aspx

相关文章:

  • 2021-12-12
  • 2021-07-24
  • 2022-12-23
  • 2021-08-21
  • 2021-11-20
猜你喜欢
  • 2021-10-27
  • 2021-06-23
  • 2021-10-24
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案