Chapter 4. Cache Loaders


What is a cache loader?

A cache loader is the connection of JBoss Cache to a (persistent) data store. The cache loader is called by JBoss Cache 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 Cache Loader is called to store those modifications back to the store.

In conjunction with eviction policies, JBoss Cache with a cache loader 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.

JBoss Cache currently ships with several cache loader implementations, including:

  • org.jboss.cache.loader.FileCacheLoader : this implementation uses the file system to store and retrieve data. JBoss Cache nodes are mapped to directories, subnodes to subdirectories etc. Attributes of a node are mapped to a data file inside the directory.

  • org.jboss.cache.loader.BdbjeCacheLoader : this implementation is based on the Oracle's Berkeley DB Java Edition database, a fast and efficient transactional database. It uses a single file for the entire store. Note that if you use the Berkeley DB cache loader with JBoss Cache and wish to ship your product, you will have to acquire a commercial license from Oracle .

  • org.jboss.cache.loader.JDBCCacheLoader : this implementation uses the relational database as the persistent storage.

  • And more. See the chapter on cache loaders in the User Guide for more details.


Is the FileCacheLoader recommended for production use?

No, it is not. The FileCacheLoader has some severe limitations which restrict it's use in a production environment, or if used in such an environment, it should be used with due care and sufficient understanding of these limitations.

  • Due to the way the FileCacheLoader represents a tree structure on disk (directories and files) traversal is inefficient for deep trees.
  • Usage on shared filesystems like NFS, Windows shares, etc. should be avoided as these do not implement proper file locking and can cause data corruption.
  • Usage with an isolation level of NONE can cause corrupt writes as multiple threads attempt to write to the same file.
  • File systems are inherently not transactional, so when attempting to use your cache in a transactional context, failures when writing to the file (which happens during the commit phase) cannot be recovered.

As a rule of thumb, it is recommended that the FileCacheLoader not be used in a highly concurrent, transactional or stressful environment, and it's use is restricted to testing.


Can writing to cache loaders be asynchronous?

Yes. Set the async attrobute to true. See the JBoss Cache User Guide for a more detailed discussion. By default though, all cache loader writes are synchronous and will block.


Can I write my own cache loader ?

Yes. A cache loader is a class implementing org.jboss.cache.loader.CacheLoader or extending org.jboss.cache.loader.AbstractCacheLoader . It is configured via the XML file (see JBoss Cache User Guide).


Does a cache loader have to use a persistent store ?

No, a cache loader 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.


Do I have to pay to use Oracle's Berkeley DB 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 Oracle. See details at .


Can I use more than one cache loader?

Yes. Within the CacheLoaderConfiguration XML element (see user guide chapter on cache loaders) you can 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.


Can I migrate a JDBCacheLoader or FileCacheLoader based cache store containing data formatted with JBoss Cache 1.x.x to JBoss Cache 2.0 format?

Yes. See "Transforming Cache Loaders" section within the "Cache Loaders" section located in the JBoss Cache users guide.