Expiration and Eviction (Windows Server AppFabric Caching)
Cached objects do not remain in memory permanently in Windows Server AppFabric. In addition to being explicitly removed from cache by use of the Remove method, cached objects may also expire or be evicted by the cache cluster.
Cache expiration allows the cache cluster to automatically remove cached objects from the cache. When using the Put or Add methods, an optional object time-out value can be set for the particular cached object that will determine how long it will reside in cache. If the object time-out value is not provided at the time the object is cached, the values specified in the cluster configuration settings for the named cache are used to determine the lifetime of the object in cache.
When cached objects are locked for the purposes of concurrency, they will not be removed from cache even if they are past their expiration. As soon as they are unlocked, they will be immediately removed from cache if past their expiration.
To prevent instant removal when you unlock expired objects, the Unlock method also supports extending the expiration of the cached object. For more information about the supported concurrency models and methods, see Concurrency Models (Windows Server AppFabric Caching) and Concurrency Methods (Windows Server AppFabric Caching).
Local Cache Invalidation
There are two complementary types of invalidation for local cache: time-out-based invalidation and notification-based invalidation. For an example of programmatically enabling local cache, see Enable Windows Server AppFabric Local Cache. For an example of using an application configuration file to enable local cache, see Enable Windows Server AppFabric Local Cache (XML).
|After objects are stored in the local cache, your application continues to use those objects until they are invalidated, regardless of whether those objects are updated by another client on the cache cluster. For this reason, it is best to use local cache for data that changes infrequently.|
After objects are downloaded to local cache, they stay there until they reach the object time-out value specified in the cache client configuration settings. After they reach this time-out value, objects are invalidated so that the object can be refreshed from the cache cluster the next time that it is requested.
If your cache client has enabled local cache, you can also use cache notifications to automatically invalidate your locally cached objects. By shortening the lifetime of those objects on an "as needed" basis, you can reduce the possibility that your application is using stale data.
When you use cache notifications, your application checks with the cache cluster on a regular interval to see if any new notifications are available. This interval, called the polling interval, is every 300 seconds by default. The polling interval is specified in units of seconds in the application configuration settings. Note that even with notification-based invalidation, timeouts still apply to items in the local cache. This makes notification-based invalidation complementary to timeout-based invalidation.
For your application to use notifications you will need to enable them on a named cache. Use the
To maintain the memory capacity available for cache on each cache host, AppFabric supports least recently used (LRU) eviction. Thresholds, referred to as watermarks, are used to make sure that memory is evenly distributed across all cache hosts in the cluster.
When the memory consumption of the cache service on a cache server exceeds the low watermark threshold, AppFabric starts evicting objects that have already expired.
When memory consumption exceeds the high watermark threshold, objects are evicted from memory, regardless of whether they have expired or not, until memory consumption goes back down to the low watermark. Subsequently cached objects may be rerouted to other hosts to maintain an optimal distribution of memory.
Specifying Expiration and Eviction Settings
Expiration and eviction behavior are configured at the named cache level in the cluster configuration settings. These cache-level settings can be configured with the Windows PowerShell-based cache administration tool. For more information, see Using Windows PowerShell to Manage Windows Server AppFabric Caching Features.
Additionally, the following methods allow you to override the default settings that are in the cache:
The Add and Put methods provide overloads that allow you to specify an expiration time-out value only for the object you add to the cache.
The PutAndUnlock and Unlock methods provide overloads that allow you to extend an object's expiration after unlocking it.
The ResetObjectTimeout method allows you to explicitly extend an object's lifetime, overriding the expiration settings of the cache.
Regardless of the expiration or eviction settings, if a cache cluster is restarted all objects in the cache are flushed. Your application code must reload the cache from a data source if the data is not found in the cache. This is often referred to as a cache-aside programming pattern.
ConceptsWindows Server AppFabric Caching Physical Architecture Diagram
Windows Server AppFabric Caching Logical Architecture Diagram
Cache Clients and Local Cache (Windows Server AppFabric Caching)
Enable Windows Server AppFabric Local Cache
Enable Windows Server AppFabric Local Cache (XML)
Developing a Cache Client (Windows Server AppFabric Caching)