Hosting transition.

by admin on May 2, 2010

Codeskine.com is moving to webfaction hosting today, therefore this page will be “under construction” for a couple of hours.

{ 0 comments }

Got Cache?

by Billy T. on April 2, 2010

Once upon a time, I had a Servlet making expensive queries, about 3 seconds per request, using plain JDBC. Something to worry about if you already know that the servlet is placed in a high traffic website, and high means more than 10 million hits daily. Obviously without any cache mechanism in the middle the solution would be far from being acceptable to move to production.

To help to prevent this issue, Servlets can use a filter cache that would process all the incoming requests and evaluate if a cache entry is available to return or a db call should be made.

So, here is an implementation example using eh-cache web on a fictional “myServlet“:

1. How to set up the cache:

If you project is using maven, be sure to add the ehcache-web dependency:

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
<version>2.0.0</version>
</dependency>

Probably you will need to add the slf4j dependency as well, if you are interested in log the cache manager messages.

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
<version>2.0.0</version>
</dependency>

If you are not using maven, be sure to add the corresponding jar files to your classpath (including .jar dependencies).

then create or edit your ehcache.xml file, adding a new cache object. i.e.

<cache name="CachePageCachingFilter"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"  //5 minutes
overflowToDisk="true">
</cache>

the ehcache.xml, should be placed inside your classpath, i.e. /src/main/resources folder.

2. Create your cache servlet filter:

Basically, you need to create a filter that extends net.sf.ehcache.constructs.web.filter.CachingFilter class, you can see a good example of how to do this in the provided class net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter . its likely override protected String calculateKey(HttpServletRequest httpRequest) method, in order to give a specific order to your request parameters or add more information to the cache key.

3.Set up the filter:

As any other servlet filter, it must be configured in the web.xml file, first declaring it:

<filter>
<display-name>CacheFilter</display-name>
<filter-name>CacheFilter</filter-name>
<filter-class>billyto.examples.MyCacheFilter</filter-class>
<!--    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>  -->
<init-param>
<param-name>suppressStackTraces</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cacheName</param-name>
<param-value>CachePageCachingFilter</param-value>   <!-- same name as ehcache.xml -->
</init-param>
</filter>

and then, mapping it BEFORE your servlet mapping. i.e

<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/example/retrieve-data</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<servlet>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>billyto.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/example/retrieve-data</url-pattern>
</servlet-mapping>

4. Try it.

You can include a date in your servlet response, and check if the time is changing between request(no cache) or if it remains the same (cached response).

Additionally if you need to condition the cache calling, you can also override the method:  protected void doFilter(final HttpServletRequest request, final HttpServletResponse response,final FilterChain chain) And set some logic to execute the cache, invoking super.doFilter(…) or  moving the execution to the next chain level with chain.doFilter(…)

EHCache have pretty good documentation and active forums, for more information you can go here.

{ 6 comments }

More on java best practices

February 20, 2010

Continuing with the ‘Java good practices’ series, Timothy Fagan at NYC Java meet up, brought us an interesting discussion about code complexity. For many people it could sound as a ‘Capitan obvious’ advise, although all of us at some point made one of these mistakes bad decisions, when writing software. So, this is what I [...]

Read the full article →

Spring Framework 3.0 (someone else already wrote the review!!!)

February 6, 2010

I’m promise last week to write about the Spring framework the last version 3.0, base on the NYC’s java meet up talk by Mark Pollack who is a Springsource insider. But looking my notes and the website I found a  blog entry from Juergen Hollen, another SpringSource fellow that can give us an overview about [...]

Read the full article →

Java best practices Thoughts about last Java meet-up @ NYC

February 1, 2010

Last week’s Java meetup in NYC brought to great speakers, first Timothy Fagan, talking about Java best practices and later Mark Pollack from Spring source gave us an overview about what’s new on Spring 3.0 The talk about Java best practices wasn’t something new, but it was a nice reminder about the basics things you [...]

Read the full article →

2010 CFQ Goals Challenge.

January 16, 2010

It’s going to be 9 months since I drop out the regular gym and joined the Queens Crossfit guys, and today we started a 12 weeks challenge, the idea is pretty simple, chose your three goals, write a plan to acomplish them and put 30 dollars in the buck, those three participants that make the [...]

Read the full article →

Amazon recommends… Arabic.

October 8, 2009
Thumbnail image for Amazon recommends…  Arabic.

Here is the last amazon recommendation I’ve got in my email: “As someone who has purchased or rated Regular Expression Pocket Reference Regular Expressions for Perl, Ruby, PHP, Python, C, Java and .NET (Pocket Reference (O’Reilly)) : by Tony Stubblebine or other books in the APIs & Operating Environments > Unicode category, you might like [...]

Read the full article →

Tonight is fight gone bad 4

September 23, 2009

The Rules for Fight Gone Bad In this workout you spend one minute at each of five stations, resulting in a a five-minute round after which a one-minute break is allowed before repeating. This event calls for three rounds. The clock does not reset or stop between exercises. On call of ‘rotate,’ the athletes must [...]

Read the full article →

Book Review: The Passionate Programmer.

August 29, 2009
Thumbnail image for Book Review: The Passionate Programmer.

I have to confess I enjoy more reading “books about programming” than “programing books, I love to nod every time the author talks about problems I had before in projects or about people attitudes I found everyday in the development world, hence this book title was eye candy to me. This book is the second [...]

Read the full article →

Wolfram|Alpha and the answer of the universe.

June 8, 2009

A couple of weeks ago, I gave a try to Wolpram|Alpha (by Wolfram Research, the same guys from ‘Mathematica’ software),  thinking that it would be another ‘Google killer’ with low possibilities to survive, but after my first round of questions… wait… there is something good here. WA is not about indexing pages and bring links, [...]

Read the full article →