[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mcs / class / System.Runtime.Caching / ReferenceSources / CacheExpires.cs
1 // This file implements the classes ExpiresEntryRef and CacheExpires missing from .NET reference source
2
3 using System.Threading;
4
5 namespace System.Runtime.Caching
6 {
7         class ExpiresEntryRef
8         {
9                 public static ExpiresEntryRef INVALID = new ExpiresEntryRef ();
10
11                 public bool IsInvalid {
12                         get { return this == INVALID; }
13                 }
14         }
15
16         class CacheExpiresHelper : ICacheEntryHelper
17         {
18                 public int Compare(MemoryCacheEntry entry1, MemoryCacheEntry entry2)
19                 {
20                         return DateTime.Compare (entry1.UtcAbsExp , entry2.UtcAbsExp);
21                 }
22
23                 public DateTime GetDateTime (MemoryCacheEntry entry)
24                 {
25                         return entry.UtcAbsExp;
26                 }
27         }
28
29         class CacheExpires : CacheEntryCollection
30         {
31
32                 public static TimeSpan MIN_UPDATE_DELTA = new TimeSpan (0, 0, 1);
33                 public static TimeSpan EXPIRATIONS_INTERVAL = new TimeSpan (0, 0, 20);
34                 public static CacheExpiresHelper helper = new CacheExpiresHelper ();
35
36                 Timer timer;
37
38                 public CacheExpires (MemoryCacheStore store)
39                         : base (store, helper)
40                 {
41                 }
42
43                 public void Add (MemoryCacheEntry entry)
44                 {
45                         entry.ExpiresEntryRef = new ExpiresEntryRef ();
46                         base.Add (entry);
47                 }
48
49                 public void Remove (MemoryCacheEntry entry)
50                 {
51                         base.Remove (entry);
52                         entry.ExpiresEntryRef = ExpiresEntryRef.INVALID;
53                 }
54
55                 public void UtcUpdate (MemoryCacheEntry entry, DateTime utcAbsExp)
56                 {
57                         base.Remove (entry);
58                         entry.UtcAbsExp = utcAbsExp;
59                         base.Add (entry);
60                 }
61
62                 public void EnableExpirationTimer (bool enable)
63                 {
64                         if (enable) {
65                                 if (timer != null)
66                                         return;
67
68                                 var period = (int) EXPIRATIONS_INTERVAL.TotalMilliseconds;
69                                 timer = new Timer ((o) => FlushExpiredItems (true), null, period, period);
70                         } else {
71                                 timer.Dispose ();
72                                 timer = null;
73                         }
74                 }
75
76                 public int FlushExpiredItems (bool blockInsert)
77                 {
78                         return base.FlushItems (DateTime.UtcNow, CacheEntryRemovedReason.Expired, blockInsert);
79                 }
80         }
81 }