How to: Add Items to the Cache

You can access items in the application cache using the Cache object. You can add an item to the application cache using the Cache object's Insert method. The method adds an item to the cache and has several overloads that enable you to add the item with different options for setting dependencies, expiration, and removal notification. If you use the Insert method to add an item to the cache and an item with the same name already exists, the existing item in the cache is replaced.

You can also add items to the cache using the Add method. This method enables you to set all the same options as the Insert method; however, Add method returns the object you added to the cache. Additionally, if you use the Add method and an item with the same name already exists in the cache, the method will not replace the item and will not raise an exception.

The procedures in this topic illustrate the following ways to add items to the application cache:

  • Adding an item to the cache by directly setting the item via key and value.

  • Adding items to the cache using the Insert method.

  • Adding an item to the cache and adding a dependency so that the item is removed from the cache when the dependency changes. You can set dependencies based on other cache items, on files, and on multiple objects.

  • Adding an item to the cache with expiration policies. In addition to being able to set an item's dependency, you can set the item to expire after a period of time (a sliding expiration) or at a specific time (an absolute expiration). You can define either an absolute expiration or a sliding expiration, but not both.

  • Adding an item to the cache and defining the relative priority of the cached item. Relative priorities help the .NET Framework determine what cache items to remove; lower priority items are removed from the cache before higher priority items.

  • Adding an item by calling the Add method.

In addition to the dependencies shown here, you can create a dependency on a SQL Server table or based on a custom dependency. For more information, see ASP.NET Caching Overview and Caching in ASP.NET with the SqlCacheDependency Class.

You can also have the application cache notify your application when the item is removed from the cache, using the CacheItemRemovedCallback delegate. For a full example, see How to: Notify an Application When an Item Is Removed from the Cache.

To add an item to the cache by directly setting the item via key and value

  • Add items to the cache as you would add items to a dictionary by specifying the item's key and value.

    The following code example adds an item named CacheItem1 to the Cache object:

    Cache["CacheItem1"] = "Cached Item 1";
    
    Cache("CacheItem1") = "Cached Item 1"
    

To add items to the cache by using the Insert method

  • Call the Insert method, passing the key and value of the item to add.

    The following code example adds a string under the name CacheItem2:

    Cache.Insert("CacheItem2", "Cached Item 2");
    
    Cache.Insert("CacheItem2", "Cached Item 2")
    

To add an item to the cache by specifying a dependency

  • Call the Insert method, passing it an instance of the CacheDependency object

    The following code example adds an item named CacheItem3 that is dependent on another item in the cache named CacheItem2:

    string[] dependencies = { "CacheItem2" };
    Cache.Insert("CacheItem3", "Cached Item 3",
        new System.Web.Caching.CacheDependency(null, dependencies));
    
    Dim dependencies As String() = {"CacheItem2"}
    Cache.Insert("CacheItem3", "Cached Item 3", _
        New System.Web.Caching.CacheDependency( _
        Nothing, dependencies))
    

    The following code example shows an item named CacheItem4 added to the cache and having a file dependency set on the file named XMLFile.xml:

    Cache.Insert("CacheItem4", "Cached Item 4",
        new System.Web.Caching.CacheDependency(
        Server.MapPath("XMLFile.xml")));
    
    Cache.Insert("CacheItem4", "Cached Item 4", _
        New System.Web.Caching.CacheDependency( _
        Server.MapPath("XMLFile.xml")))
    

    The following code example shows how to create multiple dependencies. It adds a key dependency on another item in the cache named CacheItem1 and a file dependency on the file named XMLFile.xml.

    System.Web.Caching.CacheDependency dep1 = 
        new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
    string[] keyDependencies2 = { "CacheItem1" };
    System.Web.Caching.CacheDependency dep2 = 
        new System.Web.Caching.CacheDependency(null, keyDependencies2);
    System.Web.Caching.AggregateCacheDependency aggDep = 
        new System.Web.Caching.AggregateCacheDependency();
    aggDep.Add(dep1);
    aggDep.Add(dep2);
    Cache.Insert("CacheItem5", "Cached Item 5", aggDep);
    
    Dim dep1 As CacheDependency = _
        New CacheDependency(Server.MapPath("XMLFile.xml"))
    Dim keyDependencies2 As String() = {"CacheItem1"}
    Dim dep2 As CacheDependency = _
        New System.Web.Caching.CacheDependency(Nothing, _
        keyDependencies2)
    Dim aggDep As AggregateCacheDependency = _
        New System.Web.Caching.AggregateCacheDependency()
    aggDep.Add(dep1)
    aggDep.Add(dep2)
    Cache.Insert("CacheItem5", "Cached Item 5", aggDep)
    

The add an item to the cache with expiration policies

  • Call the Insert method, passing it an absolute or sliding expiration time.

    The following code example adds an item to the cache with an absolute expiration of one minute:

    Cache.Insert("CacheItem6", "Cached Item 6",
        null, DateTime.Now.AddMinutes(1d), 
        System.Web.Caching.Cache.NoSlidingExpiration);
    
    Cache.Insert("CacheItem6", "Cached Item 6", _
        Nothing, DateTime.Now.AddMinutes(1.0), _
        TimeSpan.Zero)
    

    The following code example adds an item to the cache with a sliding expiration time of 10 minutes:

    Cache.Insert("CacheItem7", "Cached Item 7",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        new TimeSpan(0, 10, 0));
    
    Cache.Insert("CacheItem7", "Cached Item 7", _
        Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, _
        New TimeSpan(0, 10, 0))
    

To add an item to the Cache with priority settings

  • Call the Insert method, specifying a value from the CacheItemPriority enumeration.

    The following code example adds an item to the cache with a priority value of High:

    Cache.Insert("CacheItem8", "Cached Item 8",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration,
        System.Web.Caching.CacheItemPriority.High, null);
    
    Cache.Insert("CacheItem8", "Cached Item 8", _
        Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, _
        System.Web.Caching.Cache.NoSlidingExpiration, _
        System.Web.Caching.CacheItemPriority.High, _
        Nothing)  
    

To add an item to the cache using the Add method

  • Call the Add method, which returns an object representing the item.

    The following code example adds an item to the cache named CacheItem9 and sets the value of the variable CachedItem9 to be the item that was added.

    string CachedItem9 = (string)Cache.Add("CacheItem9",
        "Cached Item 9", null,
        System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration, 
        System.Web.Caching.CacheItemPriority.Default,
        null);
    
    Dim CachedItem9 As String = CStr(Cache.Add("CacheItem9", _
        "Cached Item 9", Nothing, _
        System.Web.Caching.Cache.NoAbsoluteExpiration, _
        System.Web.Caching.Cache.NoSlidingExpiration, _
        System.Web.Caching.CacheItemPriority.Default, _
        Nothing))
    

See Also

Tasks

How to: Delete Items from the Cache in ASP.NET
How to: Notify an Application When an Item Is Removed from the Cache
How to: Retrieve Values of Cached Items

Concepts

ASP.NET Caching Overview
Caching Application Data
Caching in ASP.NET with the SqlCacheDependency Class