Helma Logo
main list history

Caching

The Object-Relational Mapping layer in Helma 1 comes with an embedded caching mechanism that prevents data from being fetched from the database for every request. The size of the cache can be defined in app.properties with the cachesize-property.

Helma uses a Least-Recently-Used cache, that means that it keeps track of which key-object pairs were requested recently, and when it's time to throw out some objects, those that weren't used recently are kicked.

Keeping track of which objects were recently used is a potentially very complex task, so we're using a very simple mechanism to implement this: Helma uses two Hashtables internally, called "newTable" and "oldTable". When we look for an object in the cache, we first look in newTable. If the object is found, it is returned and nothing else happens. Otherwise, we look in oldTable. If we find the object there, we remove it from oldTable, put it into newTable (this is how we mark that the object was recently used) before returning it. If we didn't find it in oldTable either, the object was not found in the cache. When new objects are inserted in the cache, they are put into newTable. Now as soon as newTable reaches a certain size (currently cacheSize/2), newTable becomes the new oldTable and a new, empty newTable is created, while those objects that were previously in oldTable are discarded from cache (they weren't used in a while, otherwise they would have been transfered to newTable). This is the so-called cache rotation.

With this, it's easy to understand the cache rotation messages in the event log. For example, the messages on antville.org currently look like this:

> grep Rotating /var/log/helma/antville_event.log
....
[2002/09/19 09:30] Rotating Cache tables at 15000/6961 (new/old)
[2002/09/19 09:37] Rotating Cache tables at 15000/7015 (new/old)
[2002/09/19 09:42] Rotating Cache tables at 15036/6972 (new/old)
[2002/09/19 09:47] Rotating Cache tables at 15000/6690 (new/old)
[2002/09/19 09:53] Rotating Cache tables at 15065/6295 (new/old)
[2002/09/19 09:58] Rotating Cache tables at 15004/7372 (new/old)
[2002/09/19 10:02] Rotating Cache tables at 15037/6601 (new/old)
...

One can see that we have a cache size of 30000, so tables are rotated when newTable reaches 15000 objects. Currently, we rotate every 5 minutes, and in those 5 minutes we usually reuse 7000 objects from cache (these are the objects that are used very frequently, which would result in lots of database queries if they weren't cached) and fetch 7000 objects from the database.

Links to this page: Server and Application Properties