When a Cache is more than a Cache

Recently I have come across a number of instances when I have needed to perform searches across data cached in memory. Standard cache implementations, however do not provide anything more than basic key-value lookups, which is a bit of a pain. One of my colleagues came up with a clever solution to this – keeping a seperate data structure in memory that acts as an index specific to the search at hand. Ehcache provides callback hooks that can be triggered when an object is about to be evicted. This way you can delete the object from the index when the eviction policy says that it’s time to go.

It’s a neat idea to get around the immediate requirement, but I am not convinced that it is the way to be doing things. The need to create supporting data structures and manage them on a per-search basis smells in a big way. Ideally what you want is a proper, searchable, in-memory database, like JavaDB/Derby/Cloudscape but without the hassle of needing to do O-R mapping on arbitrary pojos (updated: Berkely DB comes pretty close). So this has got me thinking that perhaps OO databases are the way forward here.

The last I heard of people I knew doing anything with them was nearly a decade ago, and the problems around their management were highlighted in my databases subject at university. Basically “don’t go there” was the general advice. I don’t know that the arguments around them apply any more. Integration databases (for people poking around in your data *ugh*)  aren’t as common these days thanks to a better awareness of integration options (Martin Fowler wrote about this recently). I suspect management tools have gotten better over a decade. If not, it won’t be long until scaffolding via Grails takes care of the mundane stuff for DB4O at least. Steve Zara writes about this on his blog. Steve also discusses using DB4O as a cache. It’s definitely worth taking a look at, especially as to how it might hook in with eviction policies. Perhaps used hand in hand with the callback hook technique above? DB4O might just be the peanut butter to Ehcache’s chocolate.

Comments are closed.