开发者

Any ehcache standalone example projects I can download?

I'm a little overwhelmed reading through the ehcache user guide http://ehcache.org/EhcacheUserGuide.html#id.s20. I'm trying to figure out how to set up a simple ehcache app that has a distributed cache. I'd like to just run the app multiple times and have it share the cache between the multiple instances.

Is there an example开发者_高级运维 app I can just download and run that will do that? I know that there are a variety of distributed caching mechanisms. It'd be nice to get sample apps for each, but I'd settle for just having a single sample app that used any of the distributed caching mechanisms.


http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/ http://www.terracotta.org/start/distributed-cache-tutorial


Wrote a simple post on Getting started with Ehcache, hope it help http://www.ashishpaliwal.com/blog/2015/01/getting-started-with-ehcache/

Need to follow few steps

  1. Create an instance of CacheManager
  2. Get/Add Cache instance from CacheManager
  3. Create Element instance passing Key-Value to added to cache
  4. Add Element to the Cache using put() API.

Sample code

CacheManager cacheManager = CacheManager.newInstance();
Ehcache cache = cacheManager.addCacheIfAbsent("testCache");

Element cacheElement1 = new Element("Key-1", "Value-1");
Element cacheElement2 = new Element("Key-2", "Value-2");
Element cacheElement3 = new Element("Key-3", "Value-3");
cache.put(cacheElement1);
cache.put(cacheElement2);
cache.put(cacheElement3);

System.out.println(cache.get("Key-1").getObjectValue());
System.out.println(cache.get("Key-3").getObjectValue());
System.out.println(cache.isKeyInCache("Key-4"));
System.out.println(cache.isKeyInCache("Key-1"));

cacheManager.shutdown();

The Ehcache documentation has also improved a lot, can find similar examples at http://www.ehcache.org/generated/2.9.0/html/ehc-all/index.html#page/Ehcache_Documentation_Set%2Fto-codebasics_basic_caching.html%23

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜