Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Add an Object to a Cache (Windows Server AppFabric Caching)

The following examples show the ways you can add objects to the cache.

For more details about the methods that are used in these examples, follow these links to view the class library topics:

noteNote
These procedures assume that you have already set up your cache cluster and have prepared your development environment. For more information, see Preparing the Cache Client Development Environment (Windows Server AppFabric Caching).

To add an object to cache

  1. 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.

  2. 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.

  3. Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client).

  4. 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.

Example

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).

//add string object to cache with key "Key0"
myCache.Add("Key0", "object added with Key0");

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.

//add or replace string 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 item to the cache. If an object has already been cached by the same key, it will be replaced.

//add or replace object in cache using array notation
myCache["Key0"] = "object replaced or added using Key0";

noteNote
There are many other parameters available for the Add and Put methods. For more information, see the DataCache class.

See Also

Community Additions

ADD
Show:
© 2015 Microsoft