9.7.2 Cache reference forms and cache types
There are three types of cache reference forms:
-
This reference is not collected by GC.
-
Weak reference (java.lang.ref.WeakReference)
If the reference is weakly reachable, the reference is collected by GC. Note that whether the cache reference is weakly reachable depends on the specifications in java.lang.ref.WeakReference.
The examples of weak reference cache that are not collected with Cosminexus JPA Provider are as follows:
-
Cache of an entity object registered in the persistence context
-
Cache that references a non-weakly reachable cache using a relationship
-
Cache wherein the cache of another entity object with an inheritance relationship is not weakly reachable, when the entity inheritance strategy is used
-
-
Soft reference (java.lang.ref.SoftReference)
This is a reference form that caches out when the remaining amount of memory decreases.
A soft reference is collected by GC depending on the consumption rate and survival time of a resource. The specifications such as the collection timing and the selection of objects for collection depend on JavaVM.
The cache types differ depending on which form is used to reference the cache. The following table describes the mapping of the cache reference forms and cache types.
|
Cache reference forms |
Cache types |
|---|---|
|
Hard reference |
Full |
|
Hard reference + Weak reference#1 |
HardWeak |
|
Weak reference + Soft reference#1 |
SoftWeak |
|
Weak reference |
Weak |
|
None#2 |
None |
- #1
-
The reference forms are combined.
- #2
-
The entity object is not cached.
With Cosminexus JPA Provider, you can choose the cache type. Choose the type based on the application design and environment. Specify the cache type in persistence.xml. For details on persistence.xml, see 13.2 persistence.xml.
The following points describe the cache types.
- Organization of this subsection
(1) Full
All the entities are cached with a hard reference.
If you specify Full in the cache type, the access to the database decreases, so the processing load decreases. However, the memory continues to be occupied, therefore, the load on the memory increases.
Specify Full when the duration of the entity object is long and when the reference is created for a few entity objects that require frequent access. Furthermore, when several entity objects are read, the memory load increases, so we do not recommend using Full to update multiple records in a batch.
When Full is specified, the hard reference area is allocated with the specified cache size. If the hard reference area exceeds the defined size, the area is increased based on the Hashtable specifications. The following figure shows the image in the cache when Full is specified.
|
|
(2) HardWeak
The entities are cached with a hard reference and weak reference.
When you want to store the entity object in a list, use a hard reference. Create the hard reference area with a fixed length only up to the value specified in the cache size. If the cache size reaches the specified value, the old entity objects are moved to the weak reference. At this time, the entity objects for which cache registration has not been used for the longest time are moved sequentially to the weak reference. If the entity objects moved to the weak reference are used, the entity objects are once again stored in the hard reference area.
If you specify HardWeak, you can use the entity objects with a long duration to efficiently control the memory used in the cache.
If the state of insufficient memory occurs frequently in a system where SoftWeak is used, you cannot take advantage of the soft reference; therefore, use HardWeak. The following figure shows the image in the cache for HardWeak.
|
|
In the case of HardWeak, the cache is stored with a hard reference in the hard reference cache area. Also, the cache is stored with a weak reference in the weak reference cache area.
(3) SoftWeak
The entities are cached with a soft reference and weak reference.
You use the soft reference to store the entity objects in a list and create a soft reference area of a fixed length only up to the value specified in the cache size. If the cache size reaches the specified value, the old entity objects are moved to the weak reference area. At this time, the entity objects for which cache registration has not been used for the longest time are moved sequentially to the weak reference. If the entity objects moved to the weak reference are used, the entity objects are once again stored in the hard reference area.
If you specify SoftWeak, you can use the entity objects with a long duration to efficiently control the memory used in the cache. Therefore, when you use the cache functionality, we recommend that you specify SoftWeak. The following figure shows the image in the cache for SoftWeak.
|
|
In the case of SoftWeak, the cache is stored with a soft reference in the soft reference cache area. Also, the cache is stored with a weak reference in the weak reference cache area.
(4) Weak
All the entities are cached with a weak reference.
Therefore, all the entity objects are subject to GC. If you specify weak, the memory load decreases, but the cache might be deleted due to GC. Use the Weak cache type in systems that do not place significance on the cache functionality of the entity objects. The following figure shows the image in the cache for weak.
|
|
In the weak cache type, the entities are cached with a weak reference.