New tests.
[mono.git] / mcs / class / System.Runtime.Caching / System.Runtime.Caching / ObjectCache.cs
1 //
2 // ObjectCache.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Linq;
32 using System.Reflection;
33 using System.Runtime;
34
35 namespace System.Runtime.Caching
36 {
37         public abstract class ObjectCache : IEnumerable<KeyValuePair<string, object>>, IEnumerable
38         {
39                 static IServiceProvider host;
40                 
41                 public static readonly DateTimeOffset InfiniteAbsoluteExpiration = DateTimeOffset.MaxValue;
42                 public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
43
44                 public static IServiceProvider Host {
45                         [TargetedPatchingOptOut ("Performance critical to inline this type of method across NGen image boundaries")]
46                         get { return host; }
47                         set {
48                                 if (value == null)
49                                         throw new ArgumentNullException ("value");
50
51                                 if (host != null)
52                                         throw new InvalidOperationException ("The property has already been set, and can only be set once.");
53
54                                 host = value;
55                         }
56                 }
57                 
58                 public abstract DefaultCacheCapabilities DefaultCacheCapabilities { get; }
59                 public abstract object this [string key] { get; set; }
60                 public abstract string Name { get; }
61                 
62                 [TargetedPatchingOptOut ("Performance critical to inline this type of method across NGen image boundaries")]
63                 protected ObjectCache ()
64                 {
65                 }
66                 
67                 public virtual bool Add (CacheItem item, CacheItemPolicy policy)
68                 {
69                         return AddOrGetExisting (item, policy) == null;
70                 }
71                 
72                 public virtual bool Add (string key, object value, CacheItemPolicy policy, string regionName = null)
73                 {
74                         return AddOrGetExisting (key, value, policy, regionName) == null;
75                 }
76                 
77                 public virtual bool Add (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
78                 {
79                         return AddOrGetExisting (key, value, absoluteExpiration, regionName) == null;
80                 }
81                 
82                 public abstract CacheItem AddOrGetExisting (CacheItem value, CacheItemPolicy policy);
83                 public abstract object AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null);
84                 public abstract object AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null);
85                 public abstract bool Contains (string key, string regionName = null);
86                 public abstract CacheEntryChangeMonitor CreateCacheEntryChangeMonitor (IEnumerable <string> keys, string regionName = null);
87                 public abstract object Get (string key, string regionName = null);
88                 public abstract CacheItem GetCacheItem (string key, string regionName = null);
89                 public abstract long GetCount (string regionName = null);
90                 protected abstract IEnumerator <KeyValuePair <string, object>> GetEnumerator ();
91                 public abstract IDictionary <string, object> GetValues (IEnumerable <string> keys, string regionName = null);
92                 
93                 [TargetedPatchingOptOut ("Performance critical to inline this type of method across NGen image boundaries")]
94                 public virtual IDictionary <string, object> GetValues (string regionName = null, params string[] keys)
95                 {
96                         return GetValues (keys.AsEnumerable <string> (), regionName);
97                 }
98                 
99                 public abstract object Remove (string key, string regionName = null);
100                 public abstract void Set (CacheItem item, CacheItemPolicy policy);
101                 public abstract void Set (string key, object value, CacheItemPolicy policy, string regionName = null);
102                 public abstract void Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null);
103                 
104                 [TargetedPatchingOptOut ("Performance critical to inline this type of method across NGen image boundaries")]
105                 IEnumerator <KeyValuePair <string,object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
106                 {
107                         return GetEnumerator ();
108                 }
109                 
110                 IEnumerator IEnumerable.GetEnumerator ()
111                 {
112                         return GetEnumerator ();
113                 }
114         }
115 }