MemoryCache Class

Definition

Represents the type that implements an in-memory cache.

public class MemoryCache : System.Runtime.Caching.ObjectCache, IDisposable
Inheritance
MemoryCache
Implements

Inherited Members

System.Object

System.Runtime.Caching.ObjectCache

Examples

The following example declares a reference to the default memory cache instance. The cache entry uses a CacheItemPolicy object to provide eviction and expiration details for the cache entry. It also uses a ChangeMonitor object to monitor the state of the source data (which is a file) on the file system.

Private Sub btnGet_Click(ByVal sender As Object, ByVal e As EventArgs)   
    Dim cache As ObjectCache = MemoryCache.[Default]   
    Dim fileContents As String = TryCast(cache("filecontents"), String)   

    If fileContents Is Nothing Then   
        Dim policy As New CacheItemPolicy()   
        Dim filePaths As New List(Of String)()   
        filePaths.Add("c:\cache\example.txt")   

        policy.ChangeMonitors.Add(New HostFileChangeMonitor(filePaths))   

        ' Fetch the file contents.  
        fileContents = File.ReadAllText("c:\cache\example.txt")   

        cache.[Set]("filecontents", fileContents, policy)   
    End If   

    Label1.Text = fileContents  
End Sub  
private void btnGet_Click(object sender, EventArgs e)  
{  
    ObjectCache cache = MemoryCache.Default;  
    string fileContents = cache["filecontents"] as string;  

    if (fileContents == null)  
    {  
        CacheItemPolicy policy = new CacheItemPolicy();  

        List<string> filePaths = new List<string>();  
        filePaths.Add("c:\\cache\\example.txt");  

        policy.ChangeMonitors.Add(new   
        HostFileChangeMonitor(filePaths));  

        // Fetch the file contents.  
        fileContents =   
            File.ReadAllText("c:\\cache\\example.txt");  

        cache.Set("filecontents", fileContents, policy);  
    }  

    Label1.Text = fileContents;  
}  

Remarks

The MemoryCache class is a concrete implementation of the abstract ObjectCache class.

Note

The MemoryCache class is similar to the ASP.NET Cache class. The MemoryCache class has many properties and methods for accessing the cache that will be familiar to you if you have used the ASP.NET Cache class. The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed to make it usable by .NET Framework applications that are not ASP.NET applications. For example, the MemoryCache class has no dependencies on the System.Web assembly. Another difference is that you can create multiple instances of the MemoryCache class for use in the same application and in the same AppDomain instance.

The MemoryCache class does not allow null as a value in the cache. Any attempt to add or change a cache entry with a value of null will fail.

The MemoryCache type does not implement cache regions. Therefore, when you call MemoryCache methods that implement base methods that contain a parameter for regions, do not pass a value for the parameter. The methods that use the region parameter all supply a default null value. For example, the MemoryCache.AddOrGetExisting method overload has a regionName parameter whose default value is null.

Constructors

MemoryCache(String, NameValueCollection)

Initializes a new instance of the MemoryCache class.

MemoryCache(String, NameValueCollection, Boolean)

Initializes a new instance of the MemoryCache class.

Properties

CacheMemoryLimit

Gets the amount of memory on the computer, in bytes, that can be used by the cache.

Default

Gets a reference to the default MemoryCache instance.

DefaultCacheCapabilities

Gets a description of the features that the cache provides.

Item[String]

Gets or sets a value in the cache by using the default indexer property for an instance of the MemoryCache class.

Name

Gets the name of the cache.

PhysicalMemoryLimit

Gets the percentage of physical memory that the cache can use.

PollingInterval

Gets the maximum time after which the cache updates its memory statistics.

Methods

Add(CacheItem, CacheItemPolicy)

Inserts a cache entry into the cache as a CacheItem instance, and adds details about how the entry should be evicted.

AddOrGetExisting(CacheItem, CacheItemPolicy)

Adds a cache entry into the cache using the specified CacheItem instance and details about how to evict the entry.

AddOrGetExisting(String, Object, DateTimeOffset, String)

Adds a cache entry into the cache using the specified key and a value and an absolute expiration value.

AddOrGetExisting(String, Object, CacheItemPolicy, String)

Inserts a cache entry into the cache using the specified key and value and the specified details for how it is to be evicted.

Contains(String, String)

Determines whether a cache entry exists in the cache.

CreateCacheEntryChangeMonitor(IEnumerable<String>, String)

Creates a CacheEntryChangeMonitor object that can trigger events in response to changes to specified cache entries.

Dispose()

Releases all resources that are used by the current instance of the MemoryCache class.

Get(String, String)

Returns an entry from the cache.

GetCacheItem(String, String)

Returns the specified entry from the cache as a CacheItem instance.

GetCount(String)

Returns the total number of cache entries in the cache.

GetEnumerator()

Creates an enumerator that can be used to iterate through a collection of cache entries.

GetLastSize(String)

Gets the size of the named region.

GetValues(IEnumerable<String>, String)

Returns a set of cache entries that correspond to the specified keys.

Remove(String, String)

Removes a cache entry from the cache.

Remove(String, CacheEntryRemovedReason, String)

Removes a cache entry from the cache using the reason.

Set(CacheItem, CacheItemPolicy)

Inserts a cache entry into the cache by using a CacheItem instance to supply the key and value for the cache entry.

Set(String, Object, DateTimeOffset, String)

Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.

Set(String, Object, CacheItemPolicy, String)

Inserts a cache entry into the cache by using a key and a value and eviction.

Trim(Int32)

Removes a specified percentage of cache entries from the cache object.

Explicit Interface Implementations

IEnumerable.GetEnumerator()

Provides support for iteration over a generic collection.

Extension Methods

CopyToDataTable<T>(IEnumerable<T>)
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Any<TSource>(IEnumerable<TSource>)
Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Append<TSource>(IEnumerable<TSource>, TSource)
AsEnumerable<TSource>(IEnumerable<TSource>)
Average(IEnumerable<Decimal>)
Average(IEnumerable<Double>)
Average(IEnumerable<Int32>)
Average(IEnumerable<Int64>)
Average(IEnumerable<Nullable<Decimal>>)
Average(IEnumerable<Nullable<Double>>)
Average(IEnumerable<Nullable<Int32>>)
Average(IEnumerable<Nullable<Int64>>)
Average(IEnumerable<Nullable<Single>>)
Average(IEnumerable<Single>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Cast<TResult>(IEnumerable)
Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Contains<TSource>(IEnumerable<TSource>, TSource)
Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>)
Count<TSource>(IEnumerable<TSource>)
Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
DefaultIfEmpty<TSource>(IEnumerable<TSource>)
DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
Distinct<TSource>(IEnumerable<TSource>)
Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ElementAt<TSource>(IEnumerable<TSource>, Int32)
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
First<TSource>(IEnumerable<TSource>)
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
FirstOrDefault<TSource>(IEnumerable<TSource>)
FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>, IEqualityComparer<TKey>)
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>)
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, IEqualityComparer<TKey>)
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>)
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
Last<TSource>(IEnumerable<TSource>)
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
LastOrDefault<TSource>(IEnumerable<TSource>)
LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
LongCount<TSource>(IEnumerable<TSource>)
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Max(IEnumerable<Decimal>)
Max(IEnumerable<Double>)
Max(IEnumerable<Int32>)
Max(IEnumerable<Int64>)
Max(IEnumerable<Nullable<Decimal>>)
Max(IEnumerable<Nullable<Double>>)
Max(IEnumerable<Nullable<Int32>>)
Max(IEnumerable<Nullable<Int64>>)
Max(IEnumerable<Nullable<Single>>)
Max(IEnumerable<Single>)
Max<TSource>(IEnumerable<TSource>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Min(IEnumerable<Decimal>)
Min(IEnumerable<Double>)
Min(IEnumerable<Int32>)
Min(IEnumerable<Int64>)
Min(IEnumerable<Nullable<Decimal>>)
Min(IEnumerable<Nullable<Double>>)
Min(IEnumerable<Nullable<Int32>>)
Min(IEnumerable<Nullable<Int64>>)
Min(IEnumerable<Nullable<Single>>)
Min(IEnumerable<Single>)
Min<TSource>(IEnumerable<TSource>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Min<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
OfType<TResult>(IEnumerable)
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
Prepend<TSource>(IEnumerable<TSource>, TSource)
Reverse<TSource>(IEnumerable<TSource>)
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>)
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Single<TSource>(IEnumerable<TSource>)
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
SingleOrDefault<TSource>(IEnumerable<TSource>)
SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Skip<TSource>(IEnumerable<TSource>, Int32)
SkipLast<TSource>(IEnumerable<TSource>, Int32)
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Sum(IEnumerable<Decimal>)
Sum(IEnumerable<Double>)
Sum(IEnumerable<Int32>)
Sum(IEnumerable<Int64>)
Sum(IEnumerable<Nullable<Decimal>>)
Sum(IEnumerable<Nullable<Double>>)
Sum(IEnumerable<Nullable<Int32>>)
Sum(IEnumerable<Nullable<Int64>>)
Sum(IEnumerable<Nullable<Single>>)
Sum(IEnumerable<Single>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Take<TSource>(IEnumerable<TSource>, Int32)
TakeLast<TSource>(IEnumerable<TSource>, Int32)
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
ToArray<TSource>(IEnumerable<TSource>)
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
ToHashSet<TSource>(IEnumerable<TSource>)
ToHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ToList<TSource>(IEnumerable<TSource>)
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)
AsParallel(IEnumerable)
AsParallel<TSource>(IEnumerable<TSource>)
AsQueryable(IEnumerable)
AsQueryable<TElement>(IEnumerable<TElement>)
Ancestors<T>(IEnumerable<T>)
Ancestors<T>(IEnumerable<T>, XName)
AncestorsAndSelf(IEnumerable<XElement>)
AncestorsAndSelf(IEnumerable<XElement>, XName)
Attributes(IEnumerable<XElement>)
Attributes(IEnumerable<XElement>, XName)
DescendantNodes<T>(IEnumerable<T>)
DescendantNodesAndSelf(IEnumerable<XElement>)
Descendants<T>(IEnumerable<T>)
Descendants<T>(IEnumerable<T>, XName)
DescendantsAndSelf(IEnumerable<XElement>)
DescendantsAndSelf(IEnumerable<XElement>, XName)
Elements<T>(IEnumerable<T>)
Elements<T>(IEnumerable<T>, XName)
InDocumentOrder<T>(IEnumerable<T>)
Nodes<T>(IEnumerable<T>)
Remove(IEnumerable<XAttribute>)
Remove<T>(IEnumerable<T>)

Thread Safety

This type is thread safe.