How to: Update an Object in Cache (Velocity)
To update an object in cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing 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) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Put method or Item property may be used to update an object in cache.
Example
The following example uses the Put method to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
Note
There are many other parameters available for this method. See the Put class library for more information.
C# Copy Code
//add or replace object in cache using key "Key0"
myCache.Put("Key0", "object replaced or added using Key0");
The following example uses the Item property that has array notation to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
C# Copy Code
//add or replace object in cache using array notation
myCache["Key0"] = "object replaced or added using Key0";