Cache Eviction Policy
If a cache reaches its maximum storage limit and eviction is turned on, the specified ratio of items will be evicted to make space for new objects. However, if eviction is disabled and the cache is full, no items are evicted or added to the cache, and an exception is thrown.
Note
This feature is also available in the NCache Community Edition, except for the Least Frequently Used and Least Recently Used policies.
The cache eviction scheme is used to select the objects that need to be removed from the cache. A particular Eviction Policy can be selected at the creation time but once it is set, it cannot be altered in the running cache. Users can change the Eviction Policy after creating a cache only if it is not running. Three types of Eviction Policies are provided by NCache:
Least Frequently Used: In this scheme, a counter tracks the number of times each object is accessed. During eviction, objects with the lowest counter values are removed first. If multiple objects have the same counter value, one is chosen for eviction at random.
Least Recently Used: This is the default Eviction Policy. In this scheme, a timestamp is associated with all objects. This timestamp is updated each time the object is accessed. When evicting, objects with the oldest timestamp are picked first. In the case of two objects with the same timestamp, a random selection is made.
Priority Based Eviction: This scheme has the same mechanism as LFU with the addition of a relative cost that is associated with an object at the time of its addition to the cache. Objects with lower cost are removed before objects with higher cost. The client application can choose from a predefined list of priorities (Low, Below Normal, Normal, Above Normal, High, and Not Removable). In case a priority is not chosen, it is set by default as Normal.
These Eviction Policies for a cache can be specified using one of the following two ways.
Using the NCache Management Center
Launch the NCache Management Center by browsing to http://localhost:8251 or
<server-ip>:8251
on Windows and Linux.In the left navigation bar, click on Clustered Caches or Local Caches, based on the cache server to start. Against the cache name, click on View Details.
Important
Ensure the cache is stopped before making any configuration changes.
This opens up the detailed configuration page for the cache.
In the Overview tab, click on the
button.
Check the Enable Eviction box.
Select the Policy from the drop-down list.
If you have selected Priority based eviction, then Default Priority can also be changed in the drop-down list.
Change the Eviction Percentage, if required.
- Click on
to save the cache configuration.
Manually Editing NCache Configuration
You can manually edit the NCache configuration file as follows:
- Specify the Eviction Policy for a cache through the config.ncconf by adding the
<eviction-policy>
tag under<cache-settings>
:
<cache-settings ...>
<eviction-policy enabled-eviction="True" default-priority="normal" policy="lru" eviction-ratio="5%"/>
</cache-settings>
- Once changes are made, start the cache, and restart the NCache service.
Warning
The eviction-ratio=5%
is equal to 5% of the total cache size. Eviction is not recommended in case of sessions.