Thursday, May 04, 2006

First Steps With EhCache

If you need to cache objects in your system, Ehcache is a simple cache written in Java, widely used and well tested. I will present here a short tutorial on how to use EhCache for people who don't want to look around the documentation at first, but just want to test if it works in their project and to see how easy it is to setup.
Installation
Download Ehcache from the Download link on http://ehcache.sourceforge.net. Current release is 1.2.
Unpack Ehcache with an unpacker that knows the tgz format. For unix users, it is trivial, for windows users, 7zip is a free (and open-source) unpacker. It is probably the most popular, but there are other ones like tugzip or izarc or winrar.
In your java project you need to have ehcache-1.2.jar, commons-collections-2.1.1.jar and commons-logging-1.0.4.jar (versions numbers may vary) in your classpath, those libraries are shipped with ehcache.
Cache Configuration
Write an ehcache.xml file where you describe what cache you want to use. There can be several files per project, several cache descriptions per file. I use here a persistent cache. Configuration file is well described at http://ehcache.sourceforge.net/documentation/configuration.html
<ehcache>
<cache name="firstcache" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="true" diskExpiryThreadIntervalSeconds="120"/>

</ehcache>

Code
static  {  
//Create a CacheManager using a specific config file
cacheManager = CacheManager.create(TestClass.class.getResource( "/config/ehcache.xml"));
cache = cacheManager.getCache("firstcache");
}

/**
* retrieves value from cache if exists
* if not create it and add it to cache */
public String doit(String key, String value) {
//get an element from cache by key
Element e = cache.get(key);
if (e != null) {
value = (String)e.getValue();
LOGGER.info("retrieved "+ value+" from cache ");
}
else {
value = "new value" ;
cache.put(new Element(key, value));
}
return value;
}

/**
* refresh value for given key */
public void refresh(String key) { cache.remove(key); }

/**
* to call eventually when your application is exiting */
public void shutdown() { cacheManager.shutdown(); }
Conclusion
Using EhCache is as simple a using a Java Map with an additional configuration file.

First Steps With EhCache

If you need to cache objects in your system, Ehcache is a simple cache written in Java, widely used and well tested. I will present here a short tutorial on how to use EhCache for people who don't want to look around the documentation at first, but just want to test if it works in their project and to see how easy it is to setup.
Installation
Download Ehcache from the Download link on http://ehcache.sourceforge.net. Current release is 1.2.
Unpack Ehcache with an unpacker that knows the tgz format. For unix users, it is trivial, for windows users, 7zip is a free (and open-source) unpacker. It is probably the most popular, but there are other ones like tugzip or izarc or winrar.
In your java project you need to have ehcache-1.2.jar, commons-collections-2.1.1.jar and commons-logging-1.0.4.jar (versions numbers may vary) in your classpath, those libraries are shipped with ehcache.
Cache Configuration
Write an ehcache.xml file where you describe what cache you want to use. There can be several files per project, several cache descriptions per file. I use here a persistent cache. Configuration file is well described at http://ehcache.sourceforge.net/documentation/configuration.html
<ehcache>
<cache name="firstcache" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="true" diskExpiryThreadIntervalSeconds="120"/>

</ehcache>

Code
static  {  
//Create a CacheManager using a specific config file
cacheManager = CacheManager.create(TestClass.class.getResource( "/config/ehcache.xml"));
cache = cacheManager.getCache("firstcache");
}

/**
* retrieves value from cache if exists
* if not create it and add it to cache */
public String doit(String key, String value) {
//get an element from cache by key
Element e = cache.get(key);
if (e != null) {
value = (String)e.getValue();
LOGGER.info("retrieved "+ value+" from cache ");
}
else {
value = "new value" ;
cache.put(new Element(key, value));
}
return value;
}

/**
* refresh value for given key */
public void refresh(String key) { cache.remove(key); }

/**
* to call eventually when your application is exiting */
public void shutdown() { cacheManager.shutdown(); }
Conclusion
Using EhCache is as simple a using a Java Map with an additional configuration file.

Tuesday, May 02, 2006

Last week Javablogs.com top 10


Most read last week

  1. Tomcat killer ? Not exactly, but ... (322): ... I am impressed. Winstone is a micro servlet container with a distribution size of 160 KB ! And it's also interesting to note that it does not rely on any configuration file. [read]

  2. yet another jboss innovation :-) (299): http://www.jboss.com/products/jbossmc a bit pathetic, isn’t it? [read]

  3. Google seems like a bad career move to me (246): It is fair to say that if you are doing anything cool right now and people know about it that Google will try and recruit you. However, let me ask you this... [read]

  4. 10 Things I've Learnt About Working With Developers (243): I've been a technical writer for 10 years. In those 10 years I've learnt at least 10 things (which means one thing per year, yay) about working with developers. Here they are in no particular order. [read]

  5. Three Languages For Java Programmers (222): Dave Thomas, among others, has been saying for years that you can become a better Java programmer by getting out there and learning some other programming languages. [read]

  6. The next Azureus? (218): Cross your fingers and do the jiggly dance, missy, cuz there’s a new boy in town, and he’s cute, eye-wateringly colorful, and full of Java, Java, Java. [read]

  7. Free Books: Subversion, Jakarta Commons and more (214): I love good books. Good books make the difference between searching google for hours and having the answer right there. Unfortunately, I can't have all the books I want, [read]

  8. There's a lot to love about Eclipse Web Tools Platform...(with screenshots) (203): If you haven't played with Eclipse WTP and you do web development... then today is your day to start. I've been using it for about a month now and I've gotta say I am totally impressed with it. [read]

  9. What's the difference between Java 5 and Java 1.5 (195): We were going to ask the question in the title to people we were interviewing for a job. [read]


Most read last week-end

  1. why he hates tomcat (188): at the bileblog: Why I hate tomcat Of course, I’m sure the tomcat fanboys would quickly whip out their collective p3n15 and wave it about angrily. After all, it’s still popular, right? [read]

  2. The Best Feature Of The Upcoming NetBeans IDE 5.5 (Part 1) (181): NetBeans IDE 5.5 will totally redefine the word "productivity". I mean, forget wimpy little bits of code, pretty little samples, and obsequious hints and suggestions. Think big. How big? Well, [read]

  3. Quit your Day Job (178): It’s been a while since the last post, but a lot has happened for me since then. I’ve taken the plunge and quit my day job. I spent some time thinking down in Australia, [read]

  4. At Last! The Super-Important Bug 5034036 is Fixed! (143): I have been waiting for this one to get fixed. Luckily Sun is putting their effort where it is most needed. It only took two years, which is quite good for a bug this complicated. [read]

  5. Interfaces vs abstract classes (131): I recently read an article titled interface vs abstract class and imagined an Alien versus predator type block buster movie, [read]

  6. Eclipse 3.2RC2 Released (122): Eclipse 3.2RC2 has been released on the world. Build notes for this release are available here . 39 words. [read]

  7. Grail [was: Groovy On Rails] (113): Yesterday I made my first steps with Grails, actually I wanted to get something started with RoR, but I decided otherwise. [read]

  8. Chuck Norris News and Apology (107): Dear readers, first let me apologize for any part I had to play in the series of unfortunate events which have taken place since my posting of java.lang.ChuckNorris.java. [read]

  9. JavaMail goes Open Source (100): JavaMail is now open source as part of the GlassFish project. Can we get those JARs on ibiblio now and make Maven more usable? April 19, 2006 JavaMail is now open source! [read]