Jaa


Get an Object from a Cache

The following examples show the ways you can retrieve objects from the cache.

For more details about the methods that are used in these examples, see these class library topics:

Note

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 (AppFabric 1.1 Caching).

To get an object from the cache

  • 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 Get method or the Item property to retrieve an object from cache. In the following examples, the DataCache instance is named myCache.

Example

The following examples assume that a string object was previously added to the cache with a key value of "Key0". For more information about adding objects to the cache, see Add an Object to a Cache.

The following example uses the Get method to retrieve an object from cache.

'get string from cache using key "Key0"
Dim myString1 As String = myCache.Get("Key0")
//get string from cache using key "Key0"
string myString1 = (string) myCache.Get("Key0");

The following example uses the Item property that has array notation to retrieve an object from cache.

'get string from cache using array notation
Dim myString2 As String = myCache("Key0")
//get string from cache using array notation
string myString2 = (string) myCache["Key0"];

Note

There are many other parameters available for the Get method. For more information, see the DataCache class.

See Also

Concepts

Preparing the Cache Client Development Environment (AppFabric 1.1 Caching)
Add an Object to a Cache
Update an Object in a Cache
Remove an Object from a Cache
AppFabric Caching Concepts (AppFabric 1.1 Caching)
Developing a Cache Client

  2012-09-12