Share via


Adding Items to the Cache

There are three different techniques you can use to add an item to the Cache object. Depending on the requirements of your application, your choices can range from the simple to the complex.

If you want to take advantage of the scavenging, expiration, and dependency support offered by the Cache, you must use either the Cache.Insert method or the Cache.Add method.

Note   The Add and Insert methods have the same signature, but there are subtle differences between them. First, calling the Add method returns an object that represents the cached item, while calling Insert does not. Second, their behavior is different if you call these methods and add an item to the Cache that is already stored there. The Insert method replaces the item, while the Add method fails.

To add an item to the Cache by specifying its key and value

  • You can add items to the cache as you would add items to a dictionary by specifying the item's key and value. The following code adds the current Value property of a text box to the Cache.

    Cache("txt1") = txtName.value
    [C#]
    Cache["txt1"] = txtName.value;
    

To add items to the Cache by using the Insert method

  • The Insert method is overloaded, allowing you to define values for the parameters of the version that you are using. For example, to add only an item key and value, use the following code.

    Cache.Insert("MyData1", connectionString)
    [C#]
    Cache.Insert("MyData1", connectionString); 
    

To add items to the Cache by using the Add method

  • The Add method has the same signature as the Insert method, but it returns an object representing the item you added.

    Cache.Add("MyData1", connectionString)
    [C#]
    Cache.Add("MyData1", connectionString); 
    

Both of these methods offer you a great deal of control over the conditions under which the item remains in the Cache. Both support making the cached item dependent on external files or directories, other keys within the Cache, or arrays of either. Adding an item to the Cache with a dependency creates an instance of the CacheDependency class, which tracks changes to the dependencies you define. If any of these dependencies change, including being deleted or moved, the item they are associated with is removed from the Cache.

To add an item to the Cache that has a dependency

  • You can add an item to the Cache with a dependency by using the dependencies parameter in the Add or Insert method. The following example demonstrates using the Insert method to add an item with a dependency on an XML file to the Cache.

    Cache.Insert("MyData1", connectionString, new CacheDependency(Server.MapPath(\\myServer\myConfig.xml)));
    [Visual Basic]
    Cache.Insert("MyData1", connectionString, new CacheDependency(Server.MapPath(\\myServer\myConfig.xml)))
    

To add an item to the Cache with expiration policies

  • You can add an item to the Cache with expiration policies by using the absoluteExpiration parameter and the slidingExpiration parameter. You can define either an absolute expiration or a sliding expiration, but not both. When you define an expiration policy with one of the parameters mentioned, you must set the other parameter to zero. The Cache class defines two fields that do this automatically: NoAbsoluteExpiration and NoSlidingExpiration. Simply set the appropriate parameter to its corresponding field value when you define an absolute or sliding expiration. The following example uses the Insert method to add an item to the Cache with an absolute expiration of two minutes.

    Cache.Insert("MyData1", connectionString, null, DateTime.Now.AddMinutes(2),  NoSlidingExpiration);
    [Visual Basic]
    Cache.Insert("MyData1", connectionString, null, DateTime.Now.AddMinutes(2),  NoSlidingExpiration)
    

    The following code uses the Insert method to add an item to the Cache with a sliding expiration of 30 seconds.

    Cache.Insert("MyData1", connectionString, null, NoAbsoluteExpiration, TimeSpan.FromSeconds(30));
    [Visual Basic]
    Cache.Insert("MyData1", connectionString, null, NoAbsoluteExpiration, TimeSpan.FromSeconds(30))
    

    Note   When you set an absolute expiration, use the DateTime structure. When you set a sliding expiration, use the TimeSpan structure. Also, if you create a sliding expiration that is less than zero or more than a year, an ArgumentOutOfRangeException Class is thrown.

You can also use the Add or Insert method to define the relative importance of the cached item by specifying a value from the CacheItemPriority enumeration. These relative priorities help the Web server when it scavenges to free memory. It can remove lower priority items from the Cache before higher priority items.

To add an item to the Cache with priority settings

  • You can add an item to the Cache with priority settings by using the priority parameter on the Add or Insert method. The following example uses the Add method to add an item to the Cache with a priority of High.

    Cache.Add("MyData1", connectionString, null, NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null);
    [Visual Basic]
    Cache.Add("MyData1", connectionString, null, NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null)  
    

These methods also allow you to notify your application when the item is removed from the cache, using the CacheItemRemovedCallback delegate. For a full example, see Notifying an Application When an Item Is Deleted from the Cache.

See Also

Caching Application Data | Retrieving Values of Cached Items | Deleting Items from the Cache | Notifying Applications When an Item Is Deleted from the Cache