// // Copyright (c) 2009 Microsoft Corporation. All rights reserved. // using System; using System.Runtime.Caching.Resources; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Security; using System.Security.Permissions; using System.Threading; namespace System.Runtime.Caching { [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification= "The class represents a type of cache")] public abstract class ObjectCache : IEnumerable> { private static IServiceProvider _host; public static readonly DateTimeOffset InfiniteAbsoluteExpiration = DateTimeOffset.MaxValue; public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero; public static IServiceProvider Host { [PermissionSet(SecurityAction.Demand, Unrestricted = true)] [SecurityCritical] get { return _host; } [PermissionSet(SecurityAction.Demand, Unrestricted = true)] [SecurityCritical] set { if (value == null) { throw new ArgumentNullException("value"); } if (Interlocked.CompareExchange(ref _host, value, null) != null) { throw new InvalidOperationException(R.Property_already_set); } } } public abstract DefaultCacheCapabilities DefaultCacheCapabilities { get; } public abstract string Name { get; } //Default indexer property public abstract object this[string key] { get; set; } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable>)this).GetEnumerator(); } [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable keys, String regionName = null); IEnumerator> IEnumerable>.GetEnumerator() { return GetEnumerator(); } protected abstract IEnumerator> GetEnumerator(); //Existence check for a single item [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract bool Contains(string key, string regionName = null); //The Add overloads are for adding an item without requiring the existing item to be returned. This was requested for Velocity. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public virtual bool Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null) { return (AddOrGetExisting(key, value, absoluteExpiration, regionName) == null); } public virtual bool Add(CacheItem item, CacheItemPolicy policy) { return (AddOrGetExisting(item, policy) == null); } [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public virtual bool Add(string key, object value, CacheItemPolicy policy, string regionName = null) { return (AddOrGetExisting(key, value, policy, regionName) == null); } [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null); public abstract CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy); [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null); [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification="The name best represents an operation on a cache")] [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract object Get(string key, string regionName = null); [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract CacheItem GetCacheItem(string key, string regionName = null); [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null); [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] public abstract void Set(CacheItem item, CacheItemPolicy policy); [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract void Set(string key, object value, CacheItemPolicy policy, string regionName = null); //Get multiple items by keys [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract IDictionary GetValues(IEnumerable keys, string regionName = null); public virtual IDictionary GetValues(string regionName, params string[] keys) { return GetValues(keys, regionName); } [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract object Remove(string key, string regionName = null); [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification="This is assembly is a special case approved by the NetFx API review board")] public abstract long GetCount(string regionName = null); } }