What is a CacheLoader?
A CacheLoader is the connection of JBossCache to a (persistent) data store. The CacheLoader is called by JBossCache to fetch data from a store when that data is not in the cache, and when modifications are made to data in the cache the CacheLoader is called to store those modifications back to the store.
In conjunction with eviction policies, JBossCache with a CacheLoader allows a user to maintain a bounded cache for a large backend datastore. Frequently used data is fetched from the datastore into the cache, and the least used data is evicted, in order to provide fast access to frequently accessed data. This is all configured through XML, and the programmer doesn't have to take care of loading and eviction.
JBossCache currently ships with several CacheLoader implementations, including:
Can writing to CacheLoaders be asynchronous?
As of JBossCache 1.2.4, yes. Set the CacheLoaderAsynchronous property to true. See the JBossCache documentation for a more detailed discussion. By default though, all cache loader writes are synchronous and will block.
Can I write my own CacheLoader ?
Yes. A CacheLoader is a class implementing org.jboss.cache.loader.CacheLoader . It is configured via the XML file (see JBossCache and Tutorial documentation).
Does a CacheLoader have to use a persistent store ?
No, a CacheLoader could for example fetch (and possibly store) its data from a webdav-capable webserver. Another example is a caching proxy server, which fetches contents from the web. Note that an implementation of CacheLoader may not implement the 'store' functionality in this case, but just the 'load' functionality.
What can I use a CacheLoader for?
How do I configure JBossCache with a CacheLoader?
Through XML: both the fully-qualified classname of the CacheLoader and its configuration string have to be given. JBossCache will then instantiate a CacheLoader. See JBossCache documentation for details.
Do I have to pay to use Sleepycat's CacheLoader?
Not if you use it only for personal use. As soon as you distribute your product with BdbjeCacheLoader, you have to purchase a commercial license from Sleepycat. See details at http://www.sleepycat.com/jeforjbosscache .
Can I use more than one cache loader?
As of JBossCache 1.3.0, yes. With the new CacheLoaderConfiguration XML element (see user manual section on cache loaders) you can now describe several cache loaders. The impact is that the cache will look at all of the cache loaders in the order they've been configured, until it finds a valid, non-null element of data. When performing writes, all cache loaders are written to (except if the ignoreModifications element has been set to true for a specific cache loader.
Why do cache loaders go into an inconsistent state when I use transactions, pessimistic locking, and I attempt to read a node after removing it from within the same transaction scope?
One of the consequences of this bug is that, for example, if you use PojoCache with pojos that have private references to a List and you update and remove someelements of that List within a transaction (when using pessimistic locking and a cache loader), you may see IllegalStateExceptions thrown.