DataNucleus Cache

datanucleus-cache provides Level2 caching capabilities. It supports use of EHCache, Oracle Coherence, OSCache, SwarmCache, JCache, Spymemcached, Xmemcached, and Cacheonix.

Source Code

trunk can be checked out as follows

svn co https://datanucleus.svn.sourceforge.net/svnroot/datanucleus/platform/cache/trunk cache

Download

datanucleus-cache is downloadable as following


Dependencies

datanucleus-cache is dependent on the following packages of software. Click on the name to go to the home page for that software to download it.

Package Version Description Required?
datanucleus-core 3.0+ Provides logging, localisation, and other framework facilities for DataNucleus. Yes
ehcache 1.1+ EHCache caching Yes if using EHCache
Oracle Coherence Oracle Coherence caching Yes, if using Coherence
oscache 2.1 OSCache caching Yes, is using OSCache
swarmcache 1.0RC2 SwarmCache caching Yes, if using SwarmCache
net.sf.jsr107cache 1.1 JCache caching Yes, if using JCache
spymemcached 2.3+ Spymemcached caching Yes, if using Spymemcached
xmemcached 1.3+ Xmemcached caching Yes, if using Xmemcached


L2 Cache

JDO and JPA allow the use of a level 2 (L2) cache, with the cache shared between PersistenceManagers/EntityManagers. The objects in the level 2 cache don't pertain to any one manager. The L2 cache in DataNucleus is as follows :-

The L2 cache stores an object of type org.datanucleus.cache.CachedPC and is keyed by the identity of the object. The CachedPC contains a persistable object (not connected to a StateManager), together with the indicators for which fields are loaded, as well as values for any relation fields. The persistable object will have values as null for any relation fields, and it is these relation field values stored in the CachedPC that provide linkage to other objects.

The relation field values also do not store actual objects; they store the identities of the related objects. For example if an object X has a 1-1 relation with another persistable object Y then in the relation field values for X for that field we store the identity of Y. Similarly if the field is a Collection, then the relation field values will be a Collection of identities of the related objects. This provides isolation of each object in the L2 cache (so objects aren't storing references to other objects and so allowing garbage collection etc).

Objects are stored in the L2 cache in the following situations

  • An object is retrieved (from the datastore) within a transaction, and it is stored in the L2 cache if no object with that identity already exists there.
  • At commit() of the transaction any object that has been modified during that transaction will be stored/updated in the L2 cache if its persistable object is still in memory in the PM/EM (could have been garbage collected since flushing)

Each class can be configured to be cacheable or not. The default for a persistable class is to be cacheable. Configuration is performed via annotations or XML metadata. If a class is not cacheable then objects of that type aren't stored in the L2 cache.

See also :-

  • JDOStateManagerImpl : initialiseForCachedPC() - bringing object back from L2 cache
  • JDOStateManagerImpl : cache() - creating an object for putting in the L2 cache
  • datanucleus-cache plugin