| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Threading; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | // NOTE: this class does minimum argument checking as it is all internal |
| | | 13 | | internal class TimeBoundedCache |
| | | 14 | | { |
| | | 15 | | private static Action<object> s_purgeCallback; |
| | | 16 | | |
| | | 17 | | // if there are less than lowWaterMark entries, no purging is done |
| | | 18 | | private readonly int _lowWaterMark; |
| | | 19 | | private DateTime _nextPurgeTimeUtc; |
| | | 20 | | private TimeSpan _purgeInterval; |
| | | 21 | | private readonly PurgingMode _purgingMode; |
| | | 22 | | private IOThreadTimer _purgingTimer; |
| | | 23 | | private readonly bool _doRemoveNotification; |
| | | 24 | | |
| | 24 | 25 | | protected TimeBoundedCache(int lowWaterMark, int maxCacheItems, IEqualityComparer keyComparer, PurgingMode purgi |
| | | 26 | | { |
| | 24 | 27 | | Entries = new Hashtable(keyComparer); |
| | 24 | 28 | | CacheLock = new ReaderWriterLock(); |
| | 24 | 29 | | _lowWaterMark = lowWaterMark; |
| | 24 | 30 | | Capacity = maxCacheItems; |
| | 24 | 31 | | _purgingMode = purgingMode; |
| | 24 | 32 | | _purgeInterval = purgeInterval; |
| | 24 | 33 | | _doRemoveNotification = doRemoveNotification; |
| | 24 | 34 | | _nextPurgeTimeUtc = DateTime.UtcNow.Add(_purgeInterval); |
| | 24 | 35 | | } |
| | | 36 | | |
| | 40 | 37 | | public int Count => Entries.Count; |
| | | 38 | | |
| | | 39 | | private static Action<object> PurgeCallback |
| | | 40 | | { |
| | | 41 | | get |
| | | 42 | | { |
| | 10 | 43 | | if (s_purgeCallback == null) |
| | | 44 | | { |
| | 2 | 45 | | s_purgeCallback = new Action<object>(PurgeCallbackStatic); |
| | | 46 | | } |
| | 10 | 47 | | return s_purgeCallback; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 30 | 51 | | protected int Capacity { get; } |
| | | 52 | | |
| | 110 | 53 | | protected Hashtable Entries { get; } |
| | | 54 | | |
| | 130 | 55 | | protected ReaderWriterLock CacheLock { get; } |
| | | 56 | | |
| | | 57 | | protected bool TryAddItem(object key, object item, DateTime expirationTime, bool replaceExistingEntry) |
| | | 58 | | { |
| | 0 | 59 | | return TryAddItem(key, new ExpirableItem(item, expirationTime), replaceExistingEntry); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private void CancelTimerIfNeeded() |
| | | 63 | | { |
| | 10 | 64 | | if (Count == 0 && _purgingTimer != null) |
| | | 65 | | { |
| | 10 | 66 | | _purgingTimer.Cancel(); |
| | 10 | 67 | | _purgingTimer = null; |
| | | 68 | | } |
| | 10 | 69 | | } |
| | | 70 | | |
| | | 71 | | private void StartTimerIfNeeded() |
| | | 72 | | { |
| | 10 | 73 | | if (_purgingMode != PurgingMode.TimerBasedPurge) |
| | | 74 | | { |
| | 0 | 75 | | return; |
| | | 76 | | } |
| | 10 | 77 | | if (_purgingTimer == null) |
| | | 78 | | { |
| | 10 | 79 | | _purgingTimer = new IOThreadTimer(PurgeCallback, this, false); |
| | 10 | 80 | | _purgingTimer.Set(_purgeInterval); |
| | | 81 | | } |
| | 10 | 82 | | } |
| | | 83 | | |
| | | 84 | | protected bool TryAddItem(object key, IExpirableItem item, bool replaceExistingEntry) |
| | | 85 | | { |
| | 10 | 86 | | bool lockHeld = false; |
| | | 87 | | try |
| | | 88 | | { |
| | 10 | 89 | | try { } |
| | | 90 | | finally |
| | | 91 | | { |
| | 10 | 92 | | CacheLock.AcquireWriterLock(-1); |
| | 10 | 93 | | lockHeld = true; |
| | 10 | 94 | | } |
| | 10 | 95 | | PurgeIfNeeded(); |
| | 10 | 96 | | EnforceQuota(); |
| | 10 | 97 | | IExpirableItem currentItem = Entries[key] as IExpirableItem; |
| | 10 | 98 | | if (currentItem == null || IsExpired(currentItem)) |
| | | 99 | | { |
| | 10 | 100 | | Entries[key] = item; |
| | | 101 | | } |
| | 0 | 102 | | else if (!replaceExistingEntry) |
| | | 103 | | { |
| | 0 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | Entries[key] = item; |
| | | 109 | | } |
| | 10 | 110 | | if (currentItem != null && _doRemoveNotification) |
| | | 111 | | { |
| | 0 | 112 | | OnRemove(ExtractItem(currentItem)); |
| | | 113 | | } |
| | 10 | 114 | | StartTimerIfNeeded(); |
| | 10 | 115 | | return true; |
| | | 116 | | } |
| | | 117 | | finally |
| | | 118 | | { |
| | 10 | 119 | | if (lockHeld) |
| | | 120 | | { |
| | 10 | 121 | | CacheLock.ReleaseWriterLock(); |
| | | 122 | | } |
| | 10 | 123 | | } |
| | 10 | 124 | | } |
| | | 125 | | |
| | | 126 | | protected bool TryReplaceItem(object key, object item, DateTime expirationTime) |
| | | 127 | | { |
| | 0 | 128 | | bool lockHeld = false; |
| | | 129 | | try |
| | | 130 | | { |
| | 0 | 131 | | try { } |
| | | 132 | | finally |
| | | 133 | | { |
| | 0 | 134 | | CacheLock.AcquireWriterLock(-1); |
| | 0 | 135 | | lockHeld = true; |
| | 0 | 136 | | } |
| | 0 | 137 | | PurgeIfNeeded(); |
| | 0 | 138 | | EnforceQuota(); |
| | 0 | 139 | | if (!(Entries[key] is IExpirableItem currentItem) || IsExpired(currentItem)) |
| | | 140 | | { |
| | 0 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | else |
| | | 144 | | { |
| | 0 | 145 | | Entries[key] = new ExpirableItem(item, expirationTime); |
| | 0 | 146 | | if (currentItem != null && _doRemoveNotification) |
| | | 147 | | { |
| | 0 | 148 | | OnRemove(ExtractItem(currentItem)); |
| | | 149 | | } |
| | 0 | 150 | | StartTimerIfNeeded(); |
| | 0 | 151 | | return true; |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | finally |
| | | 155 | | { |
| | 0 | 156 | | if (lockHeld) |
| | | 157 | | { |
| | 0 | 158 | | CacheLock.ReleaseWriterLock(); |
| | | 159 | | } |
| | 0 | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | protected void ClearItems() |
| | | 164 | | { |
| | 0 | 165 | | bool lockHeld = false; |
| | | 166 | | try |
| | | 167 | | { |
| | 0 | 168 | | try { } |
| | | 169 | | finally |
| | | 170 | | { |
| | 0 | 171 | | CacheLock.AcquireWriterLock(-1); |
| | 0 | 172 | | lockHeld = true; |
| | 0 | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | int count = Entries.Count; |
| | 0 | 176 | | if (_doRemoveNotification) |
| | | 177 | | { |
| | 0 | 178 | | foreach (IExpirableItem item in Entries.Values) |
| | | 179 | | { |
| | 0 | 180 | | OnRemove(ExtractItem(item)); |
| | | 181 | | } |
| | | 182 | | } |
| | 0 | 183 | | Entries.Clear(); |
| | 0 | 184 | | CancelTimerIfNeeded(); |
| | 0 | 185 | | } |
| | | 186 | | finally |
| | | 187 | | { |
| | 0 | 188 | | if (lockHeld) |
| | | 189 | | { |
| | 0 | 190 | | CacheLock.ReleaseWriterLock(); |
| | | 191 | | } |
| | 0 | 192 | | } |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | protected object GetItem(object key) |
| | | 196 | | { |
| | 20 | 197 | | bool lockHeld = false; |
| | | 198 | | try |
| | | 199 | | { |
| | 20 | 200 | | try { } |
| | | 201 | | finally |
| | | 202 | | { |
| | 20 | 203 | | CacheLock.AcquireReaderLock(-1); |
| | 20 | 204 | | lockHeld = true; |
| | 20 | 205 | | } |
| | 20 | 206 | | if (!(Entries[key] is IExpirableItem item)) |
| | | 207 | | { |
| | 0 | 208 | | return null; |
| | | 209 | | } |
| | 20 | 210 | | else if (IsExpired(item)) |
| | | 211 | | { |
| | | 212 | | // this is a stale item |
| | 0 | 213 | | return null; |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 20 | 217 | | return ExtractItem(item); |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | finally |
| | | 221 | | { |
| | 20 | 222 | | if (lockHeld) |
| | | 223 | | { |
| | 20 | 224 | | CacheLock.ReleaseReaderLock(); |
| | | 225 | | } |
| | 20 | 226 | | } |
| | 20 | 227 | | } |
| | | 228 | | |
| | | 229 | | protected virtual ArrayList OnQuotaReached(Hashtable cacheTable) |
| | | 230 | | { |
| | 0 | 231 | | ThrowQuotaReachedException(); |
| | 0 | 232 | | return null; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | protected virtual void OnRemove(object item) |
| | | 236 | | { |
| | 10 | 237 | | } |
| | | 238 | | |
| | | 239 | | protected bool TryRemoveItem(object key) |
| | | 240 | | { |
| | 10 | 241 | | bool lockHeld = false; |
| | | 242 | | try |
| | | 243 | | { |
| | 10 | 244 | | try { } |
| | | 245 | | finally |
| | | 246 | | { |
| | 10 | 247 | | CacheLock.AcquireWriterLock(-1); |
| | 10 | 248 | | lockHeld = true; |
| | 10 | 249 | | } |
| | 10 | 250 | | PurgeIfNeeded(); |
| | 10 | 251 | | IExpirableItem currentItem = Entries[key] as IExpirableItem; |
| | 10 | 252 | | bool result = (currentItem != null) && !IsExpired(currentItem); |
| | 10 | 253 | | if (currentItem != null) |
| | | 254 | | { |
| | 10 | 255 | | Entries.Remove(key); |
| | 10 | 256 | | if (_doRemoveNotification) |
| | | 257 | | { |
| | 10 | 258 | | OnRemove(ExtractItem(currentItem)); |
| | | 259 | | } |
| | 10 | 260 | | CancelTimerIfNeeded(); |
| | | 261 | | } |
| | 10 | 262 | | return result; |
| | | 263 | | } |
| | | 264 | | finally |
| | | 265 | | { |
| | 10 | 266 | | if (lockHeld) |
| | | 267 | | { |
| | 10 | 268 | | CacheLock.ReleaseWriterLock(); |
| | | 269 | | } |
| | 10 | 270 | | } |
| | 10 | 271 | | } |
| | | 272 | | |
| | | 273 | | private void EnforceQuota() |
| | | 274 | | { |
| | 10 | 275 | | if (!(CacheLock.IsWriterLockHeld == true)) |
| | | 276 | | { |
| | | 277 | | // we failfast here because if we don't have the lock we could corrupt the cache |
| | | 278 | | Fx.Assert("Cache write lock is not held."); |
| | 0 | 279 | | DiagnosticUtility.FailFast("Cache write lock is not held."); |
| | | 280 | | } |
| | 10 | 281 | | if (Count >= Capacity) |
| | | 282 | | { |
| | | 283 | | ArrayList keysToBeRemoved; |
| | 0 | 284 | | keysToBeRemoved = OnQuotaReached(Entries); |
| | 0 | 285 | | if (keysToBeRemoved != null) |
| | | 286 | | { |
| | 0 | 287 | | for (int i = 0; i < keysToBeRemoved.Count; ++i) |
| | | 288 | | { |
| | 0 | 289 | | Entries.Remove(keysToBeRemoved[i]); |
| | | 290 | | } |
| | | 291 | | } |
| | 0 | 292 | | CancelTimerIfNeeded(); |
| | 0 | 293 | | if (Count >= Capacity) |
| | | 294 | | { |
| | 0 | 295 | | ThrowQuotaReachedException(); |
| | | 296 | | } |
| | | 297 | | } |
| | 10 | 298 | | } |
| | | 299 | | |
| | | 300 | | protected object ExtractItem(IExpirableItem val) |
| | | 301 | | { |
| | 30 | 302 | | ExpirableItem wrapper = (val as ExpirableItem); |
| | 30 | 303 | | if (wrapper != null) |
| | | 304 | | { |
| | 0 | 305 | | return wrapper.Item; |
| | | 306 | | } |
| | | 307 | | else |
| | | 308 | | { |
| | 30 | 309 | | return val; |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private bool IsExpired(IExpirableItem item) |
| | | 314 | | { |
| | | 315 | | Fx.Assert(item.ExpirationTime == DateTime.MaxValue || item.ExpirationTime.Kind == DateTimeKind.Utc, ""); |
| | 30 | 316 | | return (item.ExpirationTime <= DateTime.UtcNow); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private bool ShouldPurge() |
| | | 320 | | { |
| | 20 | 321 | | if (Count >= Capacity) |
| | | 322 | | { |
| | 0 | 323 | | return true; |
| | | 324 | | } |
| | 20 | 325 | | else if (_purgingMode == PurgingMode.AccessBasedPurge && DateTime.UtcNow > _nextPurgeTimeUtc && Count > _low |
| | | 326 | | { |
| | 0 | 327 | | return true; |
| | | 328 | | } |
| | | 329 | | else |
| | | 330 | | { |
| | 20 | 331 | | return false; |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private void PurgeIfNeeded() |
| | | 336 | | { |
| | 20 | 337 | | if (!(CacheLock.IsWriterLockHeld == true)) |
| | | 338 | | { |
| | | 339 | | // we failfast here because if we don't have the lock we could corrupt the cache |
| | | 340 | | Fx.Assert("Cache write lock is not held."); |
| | 0 | 341 | | DiagnosticUtility.FailFast("Cache write lock is not held."); |
| | | 342 | | } |
| | 20 | 343 | | if (ShouldPurge()) |
| | | 344 | | { |
| | 0 | 345 | | PurgeStaleItems(); |
| | | 346 | | } |
| | 20 | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// This method must be called from within a writer lock |
| | | 351 | | /// </summary> |
| | | 352 | | private void PurgeStaleItems() |
| | | 353 | | { |
| | 0 | 354 | | if (!(CacheLock.IsWriterLockHeld == true)) |
| | | 355 | | { |
| | | 356 | | // we failfast here because if we don't have the lock we could corrupt the cache |
| | | 357 | | Fx.Assert("Cache write lock is not held."); |
| | 0 | 358 | | DiagnosticUtility.FailFast("Cache write lock is not held."); |
| | | 359 | | } |
| | 0 | 360 | | ArrayList expiredItems = new ArrayList(); |
| | 0 | 361 | | foreach (object key in Entries.Keys) |
| | | 362 | | { |
| | 0 | 363 | | IExpirableItem item = Entries[key] as IExpirableItem; |
| | 0 | 364 | | if (IsExpired(item)) |
| | | 365 | | { |
| | | 366 | | // this is a stale item. Remove! |
| | 0 | 367 | | OnRemove(ExtractItem(item)); |
| | 0 | 368 | | expiredItems.Add(key); |
| | | 369 | | } |
| | | 370 | | } |
| | 0 | 371 | | for (int i = 0; i < expiredItems.Count; ++i) |
| | | 372 | | { |
| | 0 | 373 | | Entries.Remove(expiredItems[i]); |
| | | 374 | | } |
| | 0 | 375 | | CancelTimerIfNeeded(); |
| | 0 | 376 | | _nextPurgeTimeUtc = DateTime.UtcNow.Add(_purgeInterval); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | private void ThrowQuotaReachedException() |
| | | 380 | | { |
| | 0 | 381 | | string message = SR.Format(SR.CacheQuotaReached, Capacity); |
| | 0 | 382 | | Exception inner = new QuotaExceededException(message); |
| | 0 | 383 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(message, inner)); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | private static void PurgeCallbackStatic(object state) |
| | | 387 | | { |
| | 0 | 388 | | TimeBoundedCache self = (TimeBoundedCache)state; |
| | | 389 | | |
| | 0 | 390 | | bool lockHeld = false; |
| | | 391 | | try |
| | | 392 | | { |
| | 0 | 393 | | try { } |
| | | 394 | | finally |
| | | 395 | | { |
| | 0 | 396 | | self.CacheLock.AcquireWriterLock(-1); |
| | 0 | 397 | | lockHeld = true; |
| | 0 | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | if (self._purgingTimer == null) |
| | | 401 | | { |
| | 0 | 402 | | return; |
| | | 403 | | } |
| | 0 | 404 | | self.PurgeStaleItems(); |
| | 0 | 405 | | if (self.Count > 0 && self._purgingTimer != null) |
| | | 406 | | { |
| | 0 | 407 | | self._purgingTimer.Set(self._purgeInterval); |
| | | 408 | | } |
| | 0 | 409 | | } |
| | | 410 | | finally |
| | | 411 | | { |
| | 0 | 412 | | if (lockHeld) |
| | | 413 | | { |
| | 0 | 414 | | self.CacheLock.ReleaseWriterLock(); |
| | | 415 | | } |
| | 0 | 416 | | } |
| | 0 | 417 | | } |
| | | 418 | | |
| | | 419 | | internal interface IExpirableItem |
| | | 420 | | { |
| | | 421 | | DateTime ExpirationTime { get; } |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | internal class ExpirableItemComparer : IComparer<IExpirableItem> |
| | | 425 | | { |
| | | 426 | | private static ExpirableItemComparer s_instance; |
| | | 427 | | |
| | | 428 | | public static ExpirableItemComparer Default |
| | | 429 | | { |
| | | 430 | | get |
| | | 431 | | { |
| | 0 | 432 | | if (s_instance == null) |
| | | 433 | | { |
| | 0 | 434 | | s_instance = new ExpirableItemComparer(); |
| | | 435 | | } |
| | 0 | 436 | | return s_instance; |
| | | 437 | | } |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | // positive, if item1 will expire before item2. |
| | | 441 | | public int Compare(IExpirableItem item1, IExpirableItem item2) |
| | | 442 | | { |
| | 0 | 443 | | if (ReferenceEquals(item1, item2)) |
| | | 444 | | { |
| | 0 | 445 | | return 0; |
| | | 446 | | } |
| | | 447 | | Fx.Assert(item1.ExpirationTime.Kind == item2.ExpirationTime.Kind, ""); |
| | 0 | 448 | | if (item1.ExpirationTime < item2.ExpirationTime) |
| | | 449 | | { |
| | 0 | 450 | | return 1; |
| | | 451 | | } |
| | 0 | 452 | | else if (item1.ExpirationTime > item2.ExpirationTime) |
| | | 453 | | { |
| | 0 | 454 | | return -1; |
| | | 455 | | } |
| | | 456 | | else |
| | | 457 | | { |
| | 0 | 458 | | return 0; |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | internal sealed class ExpirableItem : IExpirableItem |
| | | 464 | | { |
| | 0 | 465 | | public ExpirableItem(object item, DateTime expirationTime) |
| | | 466 | | { |
| | 0 | 467 | | Item = item; |
| | | 468 | | Fx.Assert(expirationTime == DateTime.MaxValue || expirationTime.Kind == DateTimeKind.Utc, ""); |
| | 0 | 469 | | ExpirationTime = expirationTime; |
| | 0 | 470 | | } |
| | | 471 | | |
| | 0 | 472 | | public DateTime ExpirationTime { get; } |
| | 0 | 473 | | public object Item { get; } |
| | | 474 | | } |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | internal enum PurgingMode |
| | | 478 | | { |
| | | 479 | | TimerBasedPurge, |
| | | 480 | | AccessBasedPurge |
| | | 481 | | } |
| | | 482 | | } |