JavaScript Editor jscript editor     Web designer 



Main Page

To retrieve data from the cache, you specify the key that the cached item was stored under. However, because information stored in the cache is volatile—that is, it might be removed by ASP.NET—the recommended development pattern is to determine first whether the item is in the cache. If it is not, you add it back to the cache and then retrieve the item.

To retrieve the value of a cached item

  • Check to see if the item is not null (Nothing in Visual Basic), in the Cache object. If it exists, assign it to your variable. Otherwise, recreate the item, add it to the cache, and then access it.

    The following code example shows how to determine whether the item named CacheItem is in the cache. If it is, the code assigns the contents of the item to the variable named cachedString. If the item is not in the cache, the code adds the item to the cache and then assigns the item to cachedString.

    C#В CopyCode imageCopy Code
    string cachedString;
    if (Cache["CacheItem"] != null)
    {
        cachedString = (string)Cache["CacheItem"];
    }
    else
    {
        Cache.Insert("CacheItem", "Hello, World.");
        cachedString = (string)Cache["CacheItem"];
    }

    Visual BasicВ CopyCode imageCopy Code
    Dim cachedString As String
    If Cache("CacheItem") IsNot Nothing Then
        cachedString = CStr(Cache("CacheItem"))
    Else
        Cache.Insert("CacheItem", "Hello, World.")
        cachedString = CStr(Cache("CacheItem"))
    End If

See Also



JavaScript Editor jscript editor     Web designer