Store objects in application cache
You can benefit from the Sitefinity CMS application cache in your custom implementation to store items that are either expensive to process. This way you increase your application performance. Items are stored in application cache via the respective cache managers. You can store, retrieve and remove items from application cache.
Get a cache manager instance
To work with application cache must first get an instance of the
CacheManager you want to work with. Different cache managers define different sections of the application cache and have their own polling and scavenging behavior. For more information see Administration: Configure Application cache.
To get an instance of a cache manager you must use the
SystemManager.GetCacheManager method. When working with
SystemManager.GetCacheManager method you can instantiate one of the default cache managers by using the
CacheManagerInstance enum or get an instance of a custom cache manager by passing its Name as a string.
Add items to cache
You add items in cache via using the to use the
Add method of the CacheManager. When adding items to application cache you must to specify the following additional parameters:
- a cache item key - this is the key that your item will be identified by in application cache. You specify the key when adding an item, and then use the key to retrieve or remove the item from cache. You need to take care of the cache key uniqueness to avoid overriding an existing cache item. We recommend using a GUID or some other unique identifier mechanism.
- the cache item priority - the priority determines whether the item will be removed first when cache scavenging operation executes, or the scavenging operation will go through items with lower priority first. The supported options for cache item priority are available in the
- the cache item expiration - you must specify the period of time an item stays in cache. This can be specified as either sliding expiration or absolute time.
NOTE: If you do not explicitly set the expiration period and the priority properties, they are set to the default settings. The respective default settings are NeverExpired and Normal.
Get items from cache
You get items from application cache by calling the
GetData method of the cache manager instance and passing the cache item key.
GetData returns the items as generic objects so you need to cast them to their proper type.
Remove items from cache
To remove an item from application cache you must use the
Remove method of the cache manager instance and pass the cache item key.
The following sample demonstrates working with application cache: