The main purpose of a caching solution is to cache the commonly used data which consequently boosts up the application performance. But caching a large amount of data without constantly updating it may lead to stale data. Thus it is very important that cached data must be up to date. For this purpose, an effective caching solution must have data invalidation strategies which can be very demanding in a distributed environment. Fortunately, NCache supports different data invalidation strategies which cover almost all major needs of most of the applications. Depending upon the sensitivity, importance and the rate at which data can change, an appropriate invalidation strategy can be chosen. For instance, the user can decide to keep data in cache on the basis of its access or he/she can invalidate certain data according to its change in master data source. A combination of these strategies can be chosen to keep data refreshed in cache.
Time Based Invalidation
NCache supports time based data invalidation where time or interval can be specified to invalidate cache data. This type of data invalidation is used if changes to the data occur in a deterministic time fashion or where the frequency of data change can be determined. For example, customer information or region information may not be frequently updated, so time based expiration can be used to invalidate such data.
You will specify this time/interval with items of cache data. NCache maintains an index for items with time based expiration. There is a dedicated thread which checks cache data for these expired items. This thread periodically runs after a configurable interval of time called Clean Interval. By default its value is 15 seconds. It means that the maximum time an item can take to be removed from cache is expiration time + clean interval. So this interval shoud be selected carefully according to the expiration time of your data.
NCache provides two types in time based expiration.
-
Absolute Expiration
-
Sliding Expiration
1. Absolute Expiration
In Absolute Expiration, an item can be added to the cache by specifying the exact date and time at which the item should be invalidated. When this time is elapsed, item will be removed from cache. Here NCache maintains UTC time for absolute expiration on caching servers which facilitates in case clients and cache servers are in different time zones. This means that expiration time specified by clients (in any time zones) will be converted to UTC on cache server and item will be expired on exact date and time as specified by clients. Expiration time can range from seconds to days and months.
However, note that the item will be removed when expired on next cache clean up interval. For example, if an expiration of 10 seconds is specified with an item and cache clean up interval is configured to 15 seconds, then the item will be removed within the time frame of 15-25 seconds.
2. Sliding Expiration
In liding expiration, the data is retained in cache as long as it's being used by the application. If the data is not used for a specific period of time, it is removed from cache. For example, ASP.NET session data can be stored in the cache as long as the session is active. For this type of data, sliding expiration ranging from seconds to days and months can be specified.
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 cache if not accessed for at least 30 seconds or it can be said that with each Get, its expiration interval will be reset to 30 seconds. Similar to absolute expiration, cache items with sliding expiration are removed on cache cleanup interval.
See Also