Pinning Data
Without pinning, expired cache entries can be flushed and eventually evicted, and even most non-expired elements can also be flushed and evicted as well, if resource limitations are reached. Pinning gives per-cache control over whether data can be evicted from Ehcache .
Data that should remain in memory can be pinned. You cannot pin individual entries, only an entire cache. As described in the following topics, there are two types of pinning, depending upon whether the pinning configuration should take precedence over resource constraints or the other way around.
Configuring Pinning
Entire caches can be pinned using the pinning element in the Ehcache configuration. This element has a required attribute (store) to specify how the pinning will be accomplished.
The store attribute can have either of the following values:
inCache – Data is pinned in the cache, in any tier in which cache data is stored. The tier is chosen based on performance-enhancing efficiency algorithms. Unexpired entries can never be evicted.
localMemory – Data is pinned to the memory store. Entries are evicted only in the event that the store's configured size is exceeded.
For example, the following cache is configured to pin its entries:
<cache name="Cache1" ... >
<pinning store="inCache" />
</cache>
The following cache is configured to pin its entries to heap only:
<cache name="Cache2" ... >
<pinning store="localMemory" />
</cache>
Pinning and Cache Sizing
The interaction of the pinning configuration with the cache sizing configuration depends upon which pinning option is used.
For inCache pinning, the pinning setting takes priority over the configured cache size. Elements resident in a cache with this pinning option cannot be evicted if they have not expired. This type of pinned cache is not eligible for eviction at all, and maxEntriesInCache should not be configured for this cache.
Important: | Potentially, pinned caches could grow to an unlimited size. Caches should never be pinned unless they are designed to hold a limited amount of data (such as reference data) or their usage and expiration characteristics are understood well enough to conclude that they cannot cause errors. |
For localMemory pinning, the configured cache size takes priority over the pinning setting. localMemory pinning should be used for optimization, to keep data in heap memory, unless or until the tier becomes too full. If the number of entries surpasses the configured size, entries will be evicted. For example, in the following cache the maxEntriesOnHeap and maxBytesLocalOffHeap settings override the pinning configuration. (Off-heap storage is only available in the Terracotta BigMemory products.)
<cache name="myCache"
maxEntriesOnHeap="10000"
maxBytesLocalOffHeap="8g"
... >
<pinning store="localMemory" />
</cache>
Scope of Pinning
Pinning achieved programmatically will not be persisted — after a restart the pinned entries are no longer pinned.
Explicitly Removing Data from a Pinned Cache
To unpin all of a cache's pinned entries, clear the cache. Specific entries can be removed from a cache using Cache.remove(). To empty the cache, Cache.removeAll(). If the cache itself is removed (Cache.dispose() or CacheManager.removeCache()), then any data still remaining in the cache is also removed locally. However, that remaining data is not removed from disk (if localRestartable).