Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Develop a generic caching service to improve performance

Build a timed cache with a background thread

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Suppose a coworker asks you for a list of all the countries in the world. Because you are no geography expert, you surf over to the United Nations Website, download the list, and print it out for her. However, she only wishes to examine the list; she doesn't actually take it with her. Because the last thing you need is another piece of paper on your desk, you feed the list to the shredder.

A day later, another coworker requests the same thing: a list of every country in the world. Cursing yourself for not keeping the list, you again surf back to the United Nations Website. On this visit to the Website, you note that the UN updates its country list every six months. You download and print the list for your coworker. He looks at it, thanks you, and again, leaves the list with you. This time you file the list away with a message on an attached Post-it note that reminds you to discard it after six months.

Sure enough, over the next few weeks your coworkers continue to request the list again and again. You congratulate yourself for filing the document since you can extract the document from the filing cabinet more quickly than you can extract it from the Website. Your filing cabinet concept catches on; soon everyone starts putting items in your cabinet. To prevent the cabinet from growing disorganized, you set guidelines for using it. In your official capacity as filing cabinet manager, you instruct your coworkers to place labels and Post-it notes on all documents, which identify the documents and their discard/expiration date. The labels help your coworkers locate the document they're looking for, and the Post-it notes qualify whether the information is up to date.

The filing cabinet grows so popular that soon you can't file any new documents in it. You must decide what to throw out and what to keep. Although you throw out all expired documents, the cabinet still overflows with paper. How do you decide which nonexpired documents to discard? Do you discard the oldest document? You could discard the least frequently used or the least recently used; in both cases you would need a log that listed when each document was accessed. Or perhaps you could decide which documents to discard based on some other determinant; the decision is purely personal.

To relate the above real-world analogy to the computer world, the filing cabinet operates as a cache: a high-speed memory that occasionally needs maintenance. The documents in the cache are cached objects, all of which conform to the standards set by you, the cache manager. The process of cleaning out the cache is termed purging. Because cached items are purged after a certain amount of time has elapsed, the cache is called a timed cache.

In this article, you will learn how to create a 100 percent pure Java cache that uses an anonymous background thread to purge expired items. You will see how to architect such a cache while understanding the trade-offs involved with various designs.

Build the cache

Enough filing cabinet analogies: let's move on to Websites. Website servers need to deal with caching too. Servers repeatedly receive requests for information, which are identical to other requests. For your next task, you must build an Internet application for one of the world's largest companies. After four months of development, including many sleepless nights and far too many Jolt colas, the application goes into development testing with 1,000 users. A 5,000-user certification test and a subsequent 20,000-user production rollout follows the development testing. However, after you receive out-of-memory errors while only 200 users test the application, development testing halts.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

Great!By Anonymous on April 30, 2009, 8:40 amvery basic level approach. clean approach for deciding. actually i was chosing between different softwares/caching techniques in market. this article gave me...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources