Chapter 4. Cache Loaders

Q: What is a cache loader?
Q: Is the FileCacheLoader recommended for production use?
Q: Can writing to cache loaders be asynchronous?
Q: Can I write my own cache loader?
Q: Does a cache loader have to use a persistent store?
Q: Can I use more than one cache loader?
Q: 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?
Q: Is the TCPDelegatingCacheLoader resilient to TCPCacheServer restarts?
Q:
What is a cache loader?
A:
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 back end 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 does not 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.jdbm.JdbmCacheLoader: this implementation is based on JDBM, an open source file-based transactional persistence engine.
  • org.jboss.cache.loader.bdbje.BdbjeCacheLoader: this implementation is based on 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 JBoss Cache User Guide for more details.
Q:
Is the FileCacheLoader recommended for production use?
A:
No, it is not. The FileCacheLoader has some severe limitations which restrict its 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 file systems 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 its use is restricted to testing.
Q:
Can writing to cache loaders be asynchronous?
A:
Yes. Set the async attribute to true. See the JBoss Cache Users' Guide for a more detailed discussion. By default though, all cache loader writes are synchronous and will block.
Q:
Can I write my own cache loader?
A:
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).
Q:
Does a cache loader have to use a persistent store?
A:
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.
Q:
Can I use more than one cache loader?
A:
Yes. Within the CacheLoaderConfiguration XML element (see the JBoss Cache 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).
Q:
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?
A:
Yes. See the "Transforming Cache Loaders" section within the "Cache Loaders" section located in the JBoss Cache User Guide.
Q:
Is the TCPDelegatingCacheLoader resilient to TCPCacheServer restarts?
A:
As of JBoss Cache 2.1.0, the answer is yes. See the JBoss Cache User Guide for details on how to configure and tune your retries and wait period for reestablishing the TCP connection.
Prior to that, restarting the TCPCacheServer would also mean restarting your application that uses the cache.