|
1 | | -using System; |
2 | | -using System.Diagnostics; |
3 | | -using System.Runtime.CompilerServices; |
4 | | - |
5 | | -namespace BitFaster.Caching.Lru |
6 | | -{ |
7 | | -#if !NETCOREAPP3_0_OR_GREATER |
8 | | - /// <summary> |
9 | | - /// Implement an expire after access policy. |
10 | | - /// </summary> |
11 | | - /// <remarks> |
12 | | - /// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us. |
13 | | - /// </remarks> |
14 | | - public readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>> |
15 | | - { |
16 | | - private readonly long timeToLive; |
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | + |
| 5 | +namespace BitFaster.Caching.Lru |
| 6 | +{ |
| 7 | +#if !NETCOREAPP3_0_OR_GREATER |
| 8 | + /// <summary> |
| 9 | + /// Implement an expire after access policy. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us. |
| 13 | + /// </remarks> |
| 14 | + internal readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>> |
| 15 | + { |
| 16 | + private readonly long timeToLive; |
17 | 17 | private readonly Time time; |
18 | 18 |
|
19 | | - ///<inheritdoc/> |
20 | | - public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive); |
21 | | - |
22 | | - /// <summary> |
23 | | - /// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live. |
24 | | - /// </summary> |
25 | | - /// <param name="timeToLive">The time to live.</param> |
26 | | - public AfterAccessLongTicksPolicy(TimeSpan timeToLive) |
27 | | - { |
28 | | - this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive); |
29 | | - this.time = new Time(); |
30 | | - } |
31 | | - |
32 | | - ///<inheritdoc/> |
33 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
34 | | - public LongTickCountLruItem<K, V> CreateItem(K key, V value) |
35 | | - { |
36 | | - return new LongTickCountLruItem<K, V>(key, value, Stopwatch.GetTimestamp()); |
37 | | - } |
38 | | - |
39 | | - ///<inheritdoc/> |
40 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
41 | | - public void Touch(LongTickCountLruItem<K, V> item) |
42 | | - { |
43 | | - item.TickCount = this.time.Last; |
44 | | - item.WasAccessed = true; |
45 | | - } |
46 | | - |
47 | | - ///<inheritdoc/> |
48 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
49 | | - public void Update(LongTickCountLruItem<K, V> item) |
50 | | - { |
51 | | - item.TickCount = Stopwatch.GetTimestamp(); |
52 | | - } |
53 | | - |
54 | | - ///<inheritdoc/> |
55 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
56 | | - public bool ShouldDiscard(LongTickCountLruItem<K, V> item) |
57 | | - { |
58 | | - this.time.Last = Stopwatch.GetTimestamp(); |
59 | | - if (this.time.Last - item.TickCount > this.timeToLive) |
60 | | - { |
61 | | - return true; |
62 | | - } |
63 | | - |
64 | | - return false; |
65 | | - } |
66 | | - |
67 | | - ///<inheritdoc/> |
68 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
69 | | - public bool CanDiscard() |
70 | | - { |
71 | | - return true; |
72 | | - } |
73 | | - |
74 | | - ///<inheritdoc/> |
75 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
76 | | - public ItemDestination RouteHot(LongTickCountLruItem<K, V> item) |
77 | | - { |
78 | | - if (this.ShouldDiscard(item)) |
79 | | - { |
80 | | - return ItemDestination.Remove; |
81 | | - } |
82 | | - |
83 | | - if (item.WasAccessed) |
84 | | - { |
85 | | - return ItemDestination.Warm; |
86 | | - } |
87 | | - |
88 | | - return ItemDestination.Cold; |
89 | | - } |
90 | | - |
91 | | - ///<inheritdoc/> |
92 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
93 | | - public ItemDestination RouteWarm(LongTickCountLruItem<K, V> item) |
94 | | - { |
95 | | - if (this.ShouldDiscard(item)) |
96 | | - { |
97 | | - return ItemDestination.Remove; |
98 | | - } |
99 | | - |
100 | | - if (item.WasAccessed) |
101 | | - { |
102 | | - return ItemDestination.Warm; |
103 | | - } |
104 | | - |
105 | | - return ItemDestination.Cold; |
106 | | - } |
107 | | - |
108 | | - ///<inheritdoc/> |
109 | | - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
110 | | - public ItemDestination RouteCold(LongTickCountLruItem<K, V> item) |
111 | | - { |
112 | | - if (this.ShouldDiscard(item)) |
113 | | - { |
114 | | - return ItemDestination.Remove; |
115 | | - } |
116 | | - |
117 | | - if (item.WasAccessed) |
118 | | - { |
119 | | - return ItemDestination.Warm; |
120 | | - } |
121 | | - |
122 | | - return ItemDestination.Remove; |
123 | | - } |
124 | | - |
125 | | - } |
126 | | -#endif |
127 | | -} |
| 19 | + ///<inheritdoc/> |
| 20 | + public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live. |
| 24 | + /// </summary> |
| 25 | + /// <param name="timeToLive">The time to live.</param> |
| 26 | + public AfterAccessLongTicksPolicy(TimeSpan timeToLive) |
| 27 | + { |
| 28 | + this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive); |
| 29 | + this.time = new Time(); |
| 30 | + } |
| 31 | + |
| 32 | + ///<inheritdoc/> |
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 34 | + public LongTickCountLruItem<K, V> CreateItem(K key, V value) |
| 35 | + { |
| 36 | + return new LongTickCountLruItem<K, V>(key, value, Stopwatch.GetTimestamp()); |
| 37 | + } |
| 38 | + |
| 39 | + ///<inheritdoc/> |
| 40 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 41 | + public void Touch(LongTickCountLruItem<K, V> item) |
| 42 | + { |
| 43 | + item.TickCount = this.time.Last; |
| 44 | + item.WasAccessed = true; |
| 45 | + } |
| 46 | + |
| 47 | + ///<inheritdoc/> |
| 48 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 49 | + public void Update(LongTickCountLruItem<K, V> item) |
| 50 | + { |
| 51 | + item.TickCount = Stopwatch.GetTimestamp(); |
| 52 | + } |
| 53 | + |
| 54 | + ///<inheritdoc/> |
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 56 | + public bool ShouldDiscard(LongTickCountLruItem<K, V> item) |
| 57 | + { |
| 58 | + this.time.Last = Stopwatch.GetTimestamp(); |
| 59 | + if (this.time.Last - item.TickCount > this.timeToLive) |
| 60 | + { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + ///<inheritdoc/> |
| 68 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 69 | + public bool CanDiscard() |
| 70 | + { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + ///<inheritdoc/> |
| 75 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 76 | + public ItemDestination RouteHot(LongTickCountLruItem<K, V> item) |
| 77 | + { |
| 78 | + if (this.ShouldDiscard(item)) |
| 79 | + { |
| 80 | + return ItemDestination.Remove; |
| 81 | + } |
| 82 | + |
| 83 | + if (item.WasAccessed) |
| 84 | + { |
| 85 | + return ItemDestination.Warm; |
| 86 | + } |
| 87 | + |
| 88 | + return ItemDestination.Cold; |
| 89 | + } |
| 90 | + |
| 91 | + ///<inheritdoc/> |
| 92 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 93 | + public ItemDestination RouteWarm(LongTickCountLruItem<K, V> item) |
| 94 | + { |
| 95 | + if (this.ShouldDiscard(item)) |
| 96 | + { |
| 97 | + return ItemDestination.Remove; |
| 98 | + } |
| 99 | + |
| 100 | + if (item.WasAccessed) |
| 101 | + { |
| 102 | + return ItemDestination.Warm; |
| 103 | + } |
| 104 | + |
| 105 | + return ItemDestination.Cold; |
| 106 | + } |
| 107 | + |
| 108 | + ///<inheritdoc/> |
| 109 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 110 | + public ItemDestination RouteCold(LongTickCountLruItem<K, V> item) |
| 111 | + { |
| 112 | + if (this.ShouldDiscard(item)) |
| 113 | + { |
| 114 | + return ItemDestination.Remove; |
| 115 | + } |
| 116 | + |
| 117 | + if (item.WasAccessed) |
| 118 | + { |
| 119 | + return ItemDestination.Warm; |
| 120 | + } |
| 121 | + |
| 122 | + return ItemDestination.Remove; |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | +#endif |
| 127 | +} |
0 commit comments