How to: Add and Remove Items from an Azure In-Role Cache
Updated: August 25, 2015
Important |
|---|
| Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me? |
The following examples show the ways you can add and remove objects for the cache.
Note |
|---|
| These procedures assume that you have already set up your cache cluster and have prepared your development environment |
-
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.ApplicationServer.Caching namespace.
-
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. If possible, store and reuse the same DataCacheFactory object to conserve memory and optimize performance.
-
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client), or use the default DataCache constructor to create a cache client directly.
-
After you have the DataCache object, use the Add method, the Put method, or the Item property to add an object to the cache. In the following examples, the DataCache instance is named
myCache.
The following example demonstrates how you can use the Add method to add an object to cache. You will get an exception if an object has already been cached by the same key (the first parameter).
The following example uses the Put method to add an object to cache. If an object has already been cached by the same key, it will be replaced.
The following example uses the Item property that has array notation to add an item to the cache. If an object has already been cached by the same key, it will be replaced.
Note |
|---|
| There are many other parameters available for the Add and Put methods. For more information, see the DataCache class. |
-
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.ApplicationServer.Caching namespace.
-
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. If possible, store and reuse the same DataCacheFactory object to conserve memory and optimize performance.
-
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client).
-
After you have the DataCache object, use the Remove method or the Item property to remove an object from cache. In the following examples, the DataCache instance is named
myCache.
The following example uses the Remove method to remove an object from cache.
The following example uses the Item property that has array notation to remove an object from cache.
Note |
|---|
| There are many other parameters available for the Remove method. For more information, see the DataCache class. |
Important
Note