Data Expiration Strategies
NCache supports time-based data invalidation where you can specify the time or interval to expire your cache data. This type of data invalidation is used if changes to the data occur in a deterministic time fashion or where you can determine the frequency of data change. For example, customer information or region information may not be frequently updated, so you can use a time based expiration to invalidate such data.
You will specify this time/interval with the items of cache data. NCache maintains an index for items with time based expiration. A dedicated thread checks cache data for these expired items periodically after a configurable interval of time called Clean Interval. By default, its value is 15 seconds, which means that the maximum time an item can take to be removed from the cache is (expiration time + clean interval). So you should select this interval carefully according to the expiration time of your data.
You can add an item to the cache with absolute expiration by specifying the exact date and time at which the item should be invalidated. When this time is elapsed, the item will be removed from the cache. Here, NCache maintains UTC time for absolute expiration on the caching servers which facilitates the case where clients and cache servers are in different time zones. This means that expiration time specified by clients (in any time zone) will be converted to UTC on the cache server and the item will be expired on the exact date and time as specified by clients. You can specify the expiration time ranging from seconds to days and months.
However, note that the item will be removed when expired on the next cache clean up interval. For example, if you specify an expiration of 10 seconds with an item and cache clean up interval is configured to 15 seconds, then item will be removed within the time frame of 15-25 seconds.
In Sliding expiration, you want the cache to retain the data as long as it is being used by the application and throw away any data that has not been used for a specific period of time. For example, JSP session data can be stored in the cache as long as the session is active. For this type of data, you can specify sliding expiration ranging from seconds to days and months.
Whenever a cache data with sliding expiration is accessed, its time to live in cache is extended by the expiration interval specified, e.g. an item with 30 seconds sliding expiration will be removed from the cache if not accessed for at least 30 seconds. However, with each get, the item's expiration interval will be reset to 30 seconds, extending its life in the cache by 30 seconds.
Similar to absolute expiration, cache items with sliding expiration are removed on cache cleanup interval.
The Client Cache will utilize the same expiration enabled on the clustered cache. It does not have its own independent expiration policy.
If no expiration is provided with cache item, it will reside in the cache till explicit removal. This can lead to cache storage overwhelming. Also please refer to Eviction Policies which is another feature of NCache to control cache storage usage.
NCache has introduced a new set of default expirations that can aid you in setting your data invalidation strategies with more flexibility. All of the default expirations have a minimum value of 5 seconds.
- Default Longer
It is up to you whether you want to incorporate expiration into your cache or not. By default, expiration is disabled.
To enable expiration, it is mandatory that you enable it through NCache Manager. If you pass the API for default expiration and it is not configured through Manager, it will not work.
However, there are rules you must remember while setting the expiration either through API or through configuration:
If you enable expiration, you must configure one the following default expirations:
- Default Absolute
- Default Sliding
- Default Absolute Longer
- Default Sliding Longer
There are multiple scenarios where configuration of expiration policy through the NCache Manager and API will overlap, which will trigger the following behaviors:
If Expiration is enabled, but default expiration is not passed from the API, Absolute Default will be used.
If a certain default Expiration mode is enabled, the same default must be passed through the API as well, otherwise expiration will not work.
For example, when an API call receives Sliding Longer expiration:
If Default Sliding Longer is configured, it will be used as default.
If Default Sliding Longer is not configured, no expiration will take place.
If Expiration is enabled and the time value is passed through the API call, the value passed through API will be used. If no value is passed, the value configured in NCache Manager is used.
If Expiration is enabled and both policies (Sliding and Absolute) are passed through the API, an exception will be thrown.
If Expiration is disabled, an API call with any of the defaults will be ignored and items will be inserted without expiration. However, if you pass the expiration with time value from API (non-default expiration), then it will take place regardless of the configuration.
Make sure to apply configuration after enabling expiration using NCache Manager as guided in Configuring Data Expirations in Administrators' Guide.
In This Section
Explains how to perform operations using Sliding Expiration.
Explains how to perform operations using Absolute Expiration.