3.11.1.1.1. openjpa.DataCache Configuration
To enable the basic single-factory cache set the openjpa.DataCache property to true:
<property name="openjpa.DataCache" value="true"/>
To configure the data cache to remain up-to-date in a distributed environment, set the openjpa.RemoteCommitProvider property appropriately, or integrate OpenJPA with a third-party caching solution.
Remote commit providers are described in Remote Event Notification Framework.
OpenJPA’s default implementation maintains a map of object ids to cache data.
By default, 1000 elements are kept in cache.
When the cache overflows, random entries are evicted.
The maximum cache size can be adjusted by setting the CacheSize property in your plugin string - see below for an example.
Objects that are pinned into the cache are not counted when determining if the cache size exceeds its maximum size.
Expired objects are moved to a soft reference map, so they may stick around for a little while longer.
You can control the number of soft references OpenJPA keeps with the SoftReferenceSize property.
Soft references are unlimited by default.
Set to 0 to disable soft references completely.
Both the QueryCache and DataCache can be configured to use a backing Lru map rather than the default concurrent HashMap.
Note that enabling the Lru cache can hurt performance as this map in not as scalable as the default map.
<property name="openjpa.DataCache" value="true(Lru=true)"/>
<property name="openjpa.QueryCache" value="true(Lru=true)"/>
<property name="openjpa.DataCache" value="true(CacheSize=5000, SoftReferenceSize=0)"/>
You can specify a cache timeout value for a class by setting the timeout metadata extension to the amount of time in milliseconds a class’s data is valid. Use a value of -1 for no expiration. This is the default value.
Timeout Employee objects after 10 seconds.
@Entity
@DataCache(timeout=10000)
public class Employee {
...
}
Entities may be explicitly excluded from the cache by providing a list of fully qualified class names in the ExcludedTypes argument.
The entities provided via ExcludedTypes will not be cached regardless of the DataCache annotation.
foo.bar.Person and foo.bar.Employee from the cache.
<property name="openjpa.DataCache" value="true(ExcludedTypes=foo.bar.Person;foo.bar.Employee)"/>
Entities may be explicitly included in the cache by providing a list of fully qualified class names in the Types argument.
Any entities which are not included in this list will not be cached.
foo.bar.FullTimeEmployee in the cache.
<property name="openjpa.DataCache" value="true(Types=foo.bar.FullTimeEmployee)"/>
See the
org.apache.openjpa.persistence.DataCache Javadoc for more information on the DataCache annotation.
A cache can specify that it should be cleared at certain times rather than using data timeouts.
The EvictionSchedule property of OpenJPA’s cache implementation can be input in two different formats.
The first is a cron style eviction schedule.
The format of this property is a whitespace-separated list of five tokens, where the \* symbol (asterisk), indicates match all.
The tokens are, in order:
-
Minute
-
Hour of Day
-
Day of Month
-
Month
-
Day of Week
For example, the following openjpa.DataCache setting schedules the default cache to evict values from the cache at 15 and 45 minutes past 3 PM on Sunday.
true(EvictionSchedule='15,45 15 * * 1')
The second format for this property is an interval style eviction schedule.
The format of this property is a \+ followed by the number of minutes between each time that the cache should be evicted.
For example, the following openjpa.DataCache setting schedules the default cache to evict values from the cache every 120 minutes.
true(EvictionSchedule='+120')
Setting EvictOnBulkUpdate to false will tell OpenJPA to not evict from the DataCache when executing an UPDATE or DELETE statement.
The default for the value is true.
<property name="openjpa.DataCache" value="true(EvictOnBulkUpdate=false)"/>