How to: Get an Object from Cache (Velocity)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The following examples show the ways you can retrieve objects from the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).

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

Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.

To get an object from the cache

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

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

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

  4. After you have the DataCache object, the Get method or Item property may be used to retrieve an object from cache.

Example

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

Note

There are many other parameters available for this method. See the Get class library for more information.

'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"];

See Also

Tasks

How to: Prepare the Development Environment (Velocity)
How to: Add an Object to Cache (Velocity)
How to: Update an Object in Cache (Velocity)
How to: Remove an Object from Cache (Velocity)

Other Resources

Installation and Deployment (Velocity)
Cache Concepts (Velocity)
Programming Guide (Velocity)