How to: Add an Object to Cache (Velocity)

05/24/2009

The following examples show the ways you can add objects to 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, follow these links to view the class library topics:

Add

Put

Item

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 add an object to 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 Add method, Put method, or Item property may be called to add an object to the cache.

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

Note 
There are many other parameters available for this method. See the Add class library for more information.
C#  Copy Code
//add object to cache with key "Key0"
myCache.Add("Key0", "object added with Key0");

Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Application client method development environment object datacachefactory item

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions