Content Caching
An introduction to the Cachius library
A common characteristic of page-centric web-applications is the fact that large parts of the displayed data are unpersonalized and therefore are the same for all users. To improve performance and scalability it is desirable that time-consuming operations are performed only once and the results are reused for subsequent requests.
The caching library neteye-actioncache was developed in the early days of Riot and released as open source in 2002. Since then it has been ported to Spring and was renamed to Cachius. Though it has been continuously improved, the basic concept is still the same: Unlike other caching libraries, it is tightly coupled with a web-framework (first Struts, now Spring) and is aware of the MVC pattern. The idea is that the controller, that provides a view with data, should also decide whether a previously rendered version is still up-to-date.
In order to be cached, a controller must implement the CacheableController interface that defines the following three methods:
The first method, getCacheKey() returns a key that uniquely identifies the requested content. Therefore it must include all attributes that govern the output, such as request parameters, the requested URL or the controller's name. The key itself is not interpreted in any way and thus can have an arbitrary format.
The second method, getLastModified() returns the last-modified date for a given request. The controller will not be executed unless this date is newer than the timestamp of the cached version.
The third method, getTimeToLive() returns the number of milliseconds that must have elapsed since the last up-to-date check, before getLastModified() is invoked again.
Cachius supports applications that use URL-based session tracking by keeping an anonymized version of the output (without the actual session id) in cache. When the cached content is sent to the client, the current session id is inserted.
Cachius does not cache Java objects, it caches content. It has a very small memory footprint as all data is directly written to the disk. Since no object serialization is involved, the cached content can be directly streamed from the filesystem.
Cachius can also be used to deliver gzipped content. All you have to do is to implement the Compressible interface in your controller and Cachius will do the rest for you.