How to: Add Items to the Cache

JavaScript Editor jscript editor     Web designer 



Main Page

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:

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:

    C#В CopyCode imageCopy Code
    Cache["CacheItem1"] = "Cached Item 1";

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    Cache.Insert("CacheItem2", "Cached Item 2");

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    string[] dependencies = { "CacheItem2" };
    Cache.Insert("CacheItem3", "Cached Item 3",
        new System.Web.Caching.CacheDependency(null, dependencies));

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    Cache.Insert("CacheItem4", "Cached Item 4",
        new System.Web.Caching.CacheDependency(
        Server.MapPath("XMLFile.xml")));

    Visual BasicВ CopyCode imageCopy Code
    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.

    C#В CopyCode imageCopy Code
    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);

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    Cache.Insert("CacheItem6", "Cached Item 6",
        null, DateTime.Now.AddMinutes(1d), 
        System.Web.Caching.Cache.NoSlidingExpiration);

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    Cache.Insert("CacheItem7", "Cached Item 7",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        new TimeSpan(0, 10, 0));

    Visual BasicВ CopyCode imageCopy Code
    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:

    C#В CopyCode imageCopy Code
    Cache.Insert("CacheItem8", "Cached Item 8",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration,
        System.Web.Caching.CacheItemPriority.High, null);

    Visual BasicВ CopyCode imageCopy Code
    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.

    C#В CopyCode imageCopy Code
    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);

    Visual BasicВ CopyCode imageCopy Code
    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



JavaScript Editor jscript editor     Web designer