CacheUsage.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file implements the classes UsageEntryRef and CacheUsage missing from .NET reference source
  2. namespace System.Runtime.Caching {
  3. class UsageEntryRef {
  4. public static UsageEntryRef INVALID = new UsageEntryRef ();
  5. public bool IsInvalid {
  6. get { return this == INVALID; }
  7. }
  8. // This is used to compare MemoryCacheEntry that have the same UtcLastUpdateUsage.
  9. public int DateTimeIndex {
  10. get; set;
  11. }
  12. }
  13. class CacheUsageHelper : ICacheEntryHelper
  14. {
  15. public int Compare(MemoryCacheEntry entry1, MemoryCacheEntry entry2)
  16. {
  17. var ret = DateTime.Compare (entry1.UtcLastUpdateUsage , entry2.UtcLastUpdateUsage);
  18. if (ret == 0)
  19. return entry1.UsageEntryRef.DateTimeIndex - entry2.UsageEntryRef.DateTimeIndex;
  20. return ret;
  21. }
  22. public DateTime GetDateTime (MemoryCacheEntry entry)
  23. {
  24. return entry.UtcLastUpdateUsage;
  25. }
  26. }
  27. class CacheUsage : CacheEntryCollection {
  28. public static TimeSpan CORRELATED_REQUEST_TIMEOUT = new TimeSpan (0, 0, 10);
  29. public static TimeSpan MIN_LIFETIME_FOR_USAGE = new TimeSpan (0, 0, 10);
  30. public static CacheUsageHelper helper = new CacheUsageHelper ();
  31. public DateTime prevDateTime;
  32. public int dateTimeIndex;
  33. public CacheUsage (MemoryCacheStore store)
  34. : base (store, helper)
  35. {
  36. }
  37. public new void Add (MemoryCacheEntry entry)
  38. {
  39. var now = DateTime.UtcNow;
  40. if (now == prevDateTime)
  41. dateTimeIndex++;
  42. else
  43. dateTimeIndex = 0;
  44. prevDateTime = now;
  45. entry.UtcLastUpdateUsage = now;
  46. entry.UsageEntryRef = new UsageEntryRef ();
  47. entry.UsageEntryRef.DateTimeIndex = dateTimeIndex;
  48. base.Add (entry);
  49. }
  50. public new void Remove (MemoryCacheEntry entry)
  51. {
  52. base.Remove (entry);
  53. entry.UsageEntryRef = UsageEntryRef.INVALID;
  54. }
  55. public void Update (MemoryCacheEntry entry)
  56. {
  57. base.Remove (entry);
  58. entry.UtcLastUpdateUsage = DateTime.UtcNow;
  59. base.Add (entry);
  60. }
  61. public int FlushUnderUsedItems (int count)
  62. {
  63. return base.FlushItems (DateTime.MaxValue, CacheEntryRemovedReason.Evicted, true, count);
  64. }
  65. }
  66. }