1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Runtime.Caching.Resources; 7 using System.Collections; 8 using System.Collections.Generic; 9 using System.Collections.ObjectModel; 10 using System.Diagnostics.CodeAnalysis; 11 using System.Security; 12 using System.Security.Permissions; 13 using System.Threading; 14 15 namespace System.Runtime.Caching 16 { 17 [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "The class represents a type of cache")] 18 public abstract class ObjectCache : IEnumerable<KeyValuePair<string, object>> 19 { 20 private static IServiceProvider s_host; 21 22 public static readonly DateTimeOffset InfiniteAbsoluteExpiration = DateTimeOffset.MaxValue; 23 public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero; 24 25 public static IServiceProvider Host 26 { 27 get 28 { 29 return s_host; 30 } 31 32 set 33 { 34 if (value == null) 35 { 36 throw new ArgumentNullException("value"); 37 } 38 if (Interlocked.CompareExchange(ref s_host, value, null) != null) 39 { 40 throw new InvalidOperationException(SR.Property_already_set); 41 } 42 } 43 } 44 45 public abstract DefaultCacheCapabilities DefaultCacheCapabilities { get; } 46 47 public abstract string Name { get; } 48 49 //Default indexer property 50 public abstract object this[string key] { get; set; } 51 IEnumerable.GetEnumerator()52 IEnumerator IEnumerable.GetEnumerator() 53 { 54 return ((IEnumerable<KeyValuePair<string, object>>)this).GetEnumerator(); 55 } 56 CreateCacheEntryChangeMonitor(IEnumerable<String> keys, String regionName = null)57 public abstract CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<String> keys, String regionName = null); 58 GetEnumerator()59 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() 60 { 61 return GetEnumerator(); 62 } 63 GetEnumerator()64 protected abstract IEnumerator<KeyValuePair<string, object>> GetEnumerator(); 65 66 //Existence check for a single item Contains(string key, string regionName = null)67 public abstract bool Contains(string key, string regionName = null); 68 69 //The Add overloads are for adding an item without requiring the existing item to be returned. This was requested for Velocity. Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)70 public virtual bool Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null) 71 { 72 return (AddOrGetExisting(key, value, absoluteExpiration, regionName) == null); 73 } 74 Add(CacheItem item, CacheItemPolicy policy)75 public virtual bool Add(CacheItem item, CacheItemPolicy policy) 76 { 77 return (AddOrGetExisting(item, policy) == null); 78 } 79 Add(string key, object value, CacheItemPolicy policy, string regionName = null)80 public virtual bool Add(string key, object value, CacheItemPolicy policy, string regionName = null) 81 { 82 return (AddOrGetExisting(key, value, policy, regionName) == null); 83 } 84 AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)85 public abstract object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null); AddOrGetExisting(CacheItem value, CacheItemPolicy policy)86 public abstract CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy); 87 AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)88 public abstract object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null); 89 90 [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] Get(string key, string regionName = null)91 public abstract object Get(string key, string regionName = null); 92 GetCacheItem(string key, string regionName = null)93 public abstract CacheItem GetCacheItem(string key, string regionName = null); 94 95 [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)96 public abstract void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null); 97 98 [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] Set(CacheItem item, CacheItemPolicy policy)99 public abstract void Set(CacheItem item, CacheItemPolicy policy); 100 101 [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", Justification = "The name best represents an operation on a cache")] Set(string key, object value, CacheItemPolicy policy, string regionName = null)102 public abstract void Set(string key, object value, CacheItemPolicy policy, string regionName = null); 103 104 //Get multiple items by keys GetValues(IEnumerable<String> keys, string regionName = null)105 public abstract IDictionary<string, object> GetValues(IEnumerable<String> keys, string regionName = null); 106 GetValues(string regionName, params string[] keys)107 public virtual IDictionary<string, object> GetValues(string regionName, params string[] keys) 108 { 109 return GetValues(keys, regionName); 110 } 111 Remove(string key, string regionName = null)112 public abstract object Remove(string key, string regionName = null); 113 GetCount(string regionName = null)114 public abstract long GetCount(string regionName = null); 115 } 116 } 117