Friday, November 30, 2007

Spring modules caching in HTTP session

In a previous post, I described how I used declarative caching of my Spring beans using Spring modules. This strategy works great for caching data that is global to the application but breaks down when trying to cache user specific data. Let me give you an example.

On the site, there's a feature that allows users to save a partially completed crossword puzzles so they can be resumed later. In the service layer, I have a bean that is responsible for encapsulating that puzzle saving logic, let's call it the puzzle saving bean. One of its method returns the number of saved puzzles for a particular user. When the user is logged in, this number is displayed in the side bar as a reminder that there are puzzles awaiting completion. Since the side bar is shown on every page, the service layer is called a lot which overloads the database. This is unacceptable.

Fortunately, Tapestry allows me to solve this problem elegantly by declaring a UI component property to hold the number of saved puzzles. By giving that property session scope, it gets preserved into the HTTP session between calls. This performs great but won't work because of staleness. Let me explain. Let's say in another part of the site, the user saves a puzzle. Unless I specifically notify the component that holds the session property to discard it, the number displayed in the side bar will be wrong. Plus that notification introduces undesired coupling between two unrelated components.

So, what if instead of caching the number in the session I were to use the (global) caching version of the puzzle saving bean? That would solve the staleness problem since cached values are discarded when data altering methods are called. However, it introduces another problem. The cache gets quickly filled with user specific data and that data competes with the global data for resources. So what is the solution?

Well, it is quite simple. I liked the fact that the caching data in the HTTP session is tied in with the user's activity. If the user is idle, the data gets passivated. If the user logs out, the data gets deleted. I also liked the simplicity and the abstractness that Spring modules caching offers. So I combined both. I implemented a org.springmodules.cache.provider.CacheProviderFacade that uses the HTTP session to store the caching data. This is really ideal. I can choose to inject the global or session cached service bean, depending on the situation, and none of the code using it has to change. However, I need to carefully pick which version to inject. For instance, the UI component responsible for saving and deleting saved puzzles must use the session cached bean if I want the cached number of saved puzzles to be discarded.

I posted the code on the springmodules mailing list so feel free to take a look.


sapnasundari said...

Bangalore Escorts
Escorts in Bangalore
Bangalore Escort
Independent Bangalore Escortss
Best Escorts Bangalore
Bangalore Escorts Service
Escort Girls Bangalore

sapnasundari said...

Check our website to know more ...

Bangalore Escorts & Escorts in Bangalore

Avani Mehta said...

If it comes to producing all ideal life which may be achieved with Queen and anal for making great times, and we're the dealer of great times in life from all range from good times to each of sensual format of earning life. We do understand the all qualification that contributes to all great times and we've brought all great times of queen to you from each corner of earning great times and carrying all of your stress aware and satisfying the joy on your lite you've been considering make on your tired and funniest life of earning work completed and we have escorts that not only understands to create all molecules to receive the very best care for making great times without creating any rush act on your lifetime with friendly and enjoyable manner you always wished to live with call girls at noida.Noida Escort