New tests.
[mono.git] / mcs / class / System.Runtime.Caching / System.Runtime.Caching / MemoryCacheEntryChangeMonitor.cs
1 //
2 // MemoryCacheEntryChangeMonitor.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.Collections.ObjectModel;
32 using System.Text;
33
34 namespace System.Runtime.Caching
35 {
36         sealed class MemoryCacheEntryChangeMonitor : CacheEntryChangeMonitor
37         {
38                 ReadOnlyCollection <string> cacheKeys;
39                 DateTimeOffset lastModified;
40                 MemoryCache owner;
41                 string uniqueId;
42                 
43                 public override ReadOnlyCollection <string> CacheKeys {
44                         get { return cacheKeys; }
45                 }
46
47                 public override DateTimeOffset LastModified {
48                         get { return lastModified; }
49                 }
50
51                 public override string RegionName {
52                         get { return null; }
53                 }
54
55                 public override string UniqueId {
56                         get { return uniqueId; }
57                 }
58                 
59                 public MemoryCacheEntryChangeMonitor (MemoryCache owner, IEnumerable <string> keys)
60                 {
61                         this.owner = owner;
62                         this.lastModified = DateTimeOffset.MinValue;
63
64                         MemoryCacheEntry mce;
65                         var sb = new StringBuilder ();
66                         var list = new List <string> ();
67                         foreach (string key in keys) {
68                                 mce = owner.GetEntry (key);
69                                 list.Add (key);
70 #if true
71                                 // LAMESPEC: this is what's happening
72                                 DateTimeOffset modtime;
73                                 modtime = new DateTimeOffset (mce != null ? mce.LastModified : DateTime.MinValue);
74                                 if (this.lastModified < modtime)
75                                         this.lastModified = modtime;
76 #else
77                                 // LAMESPEC: this is what is supposed to be happening
78                                 if (mce == null) {
79                                         OnChanged (null);
80                                         sb.Append ("{0}{1:x}", key, DateTime.MinValue.Ticks);
81                                         continue;
82                                 }
83                                 
84                                 DateTime modtime = mce.LastModified;
85                                 if (this.lastModtime < modtime)
86                                         this.lastModtime = new DateTimeOffset (modtime);
87 #endif
88                                 sb.AppendFormat ("{0}{1:X}", key, modtime.Ticks);
89                         }
90                         this.uniqueId = sb.ToString ();
91                         this.cacheKeys = new ReadOnlyCollection <string> (list);
92
93                         // TODO: hook up to change events on MemoryCache
94                 }
95
96                 protected override void Dispose (bool disposing)
97                 {
98                 }
99         }
100 }