17
17
18
18
namespace Neo . IO . Caching
19
19
{
20
- internal abstract class Cache < TKey , TValue > : ICollection < TValue > , IDisposable
20
+ internal abstract class Cache < TKey , TValue >
21
+ ( int max_capacity , IEqualityComparer < TKey > ? comparer = null ) : ICollection < TValue > , IDisposable
22
+ where TKey : notnull
21
23
{
22
24
protected class CacheItem
25
+ ( TKey key , TValue value )
23
26
{
24
- public readonly TKey Key ;
25
- public readonly TValue Value ;
26
- public readonly DateTime Time ;
27
-
28
- public CacheItem ( TKey key , TValue value )
29
- {
30
- Key = key ;
31
- Value = value ;
32
- Time = TimeProvider . Current . UtcNow ;
33
- }
27
+ public readonly TKey Key = key ;
28
+ public readonly TValue Value = value ;
29
+ public readonly DateTime Time = DateTime . UtcNow ;
34
30
}
35
31
36
32
protected readonly ReaderWriterLockSlim RwSyncRootLock = new ( LockRecursionPolicy . SupportsRecursion ) ;
37
- protected readonly Dictionary < TKey , CacheItem > InnerDictionary ;
38
- private readonly int max_capacity ;
33
+ protected readonly Dictionary < TKey , CacheItem > InnerDictionary = new Dictionary < TKey , CacheItem > ( comparer ) ;
34
+ private readonly int _max_capacity = max_capacity ;
39
35
40
36
public TValue this [ TKey key ]
41
37
{
@@ -44,7 +40,7 @@ public TValue this[TKey key]
44
40
RwSyncRootLock . EnterReadLock ( ) ;
45
41
try
46
42
{
47
- if ( ! InnerDictionary . TryGetValue ( key , out CacheItem item ) ) throw new KeyNotFoundException ( ) ;
43
+ if ( ! InnerDictionary . TryGetValue ( key , out CacheItem ? item ) ) throw new KeyNotFoundException ( ) ;
48
44
OnAccess ( item ) ;
49
45
return item . Value ;
50
46
}
@@ -73,15 +69,9 @@ public int Count
73
69
74
70
public bool IsReadOnly => false ;
75
71
76
- public Cache ( int max_capacity , IEqualityComparer < TKey > comparer = null )
77
- {
78
- this . max_capacity = max_capacity ;
79
- InnerDictionary = new Dictionary < TKey , CacheItem > ( comparer ) ;
80
- }
81
-
82
72
public void Add ( TValue item )
83
73
{
84
- TKey key = GetKeyForItem ( item ) ;
74
+ var key = GetKeyForItem ( item ) ;
85
75
RwSyncRootLock . EnterWriteLock ( ) ;
86
76
try
87
77
{
@@ -95,16 +85,16 @@ public void Add(TValue item)
95
85
96
86
private void AddInternal ( TKey key , TValue item )
97
87
{
98
- if ( InnerDictionary . TryGetValue ( key , out CacheItem cacheItem ) )
88
+ if ( InnerDictionary . TryGetValue ( key , out CacheItem ? cacheItem ) )
99
89
{
100
90
OnAccess ( cacheItem ) ;
101
91
}
102
92
else
103
93
{
104
- if ( InnerDictionary . Count >= max_capacity )
94
+ if ( InnerDictionary . Count >= _max_capacity )
105
95
{
106
96
//TODO: Perform a performance test on the PLINQ query to determine which algorithm is better here (parallel or not)
107
- foreach ( CacheItem item_del in InnerDictionary . Values . AsParallel ( ) . OrderBy ( p => p . Time ) . Take ( InnerDictionary . Count - max_capacity + 1 ) )
97
+ foreach ( var item_del in InnerDictionary . Values . AsParallel ( ) . OrderBy ( p => p . Time ) . Take ( InnerDictionary . Count - _max_capacity + 1 ) )
108
98
{
109
99
RemoveInternal ( item_del ) ;
110
100
}
@@ -118,9 +108,9 @@ public void AddRange(IEnumerable<TValue> items)
118
108
RwSyncRootLock . EnterWriteLock ( ) ;
119
109
try
120
110
{
121
- foreach ( TValue item in items )
111
+ foreach ( var item in items )
122
112
{
123
- TKey key = GetKeyForItem ( item ) ;
113
+ var key = GetKeyForItem ( item ) ;
124
114
AddInternal ( key , item ) ;
125
115
}
126
116
}
@@ -135,7 +125,7 @@ public void Clear()
135
125
RwSyncRootLock . EnterWriteLock ( ) ;
136
126
try
137
127
{
138
- foreach ( CacheItem item_del in InnerDictionary . Values . ToArray ( ) )
128
+ foreach ( var item_del in InnerDictionary . Values . ToArray ( ) )
139
129
{
140
130
RemoveInternal ( item_del ) ;
141
131
}
@@ -151,7 +141,7 @@ public bool Contains(TKey key)
151
141
RwSyncRootLock . EnterReadLock ( ) ;
152
142
try
153
143
{
154
- if ( ! InnerDictionary . TryGetValue ( key , out CacheItem cacheItem ) ) return false ;
144
+ if ( ! InnerDictionary . TryGetValue ( key , out CacheItem ? cacheItem ) ) return false ;
155
145
OnAccess ( cacheItem ) ;
156
146
return true ;
157
147
}
@@ -171,7 +161,7 @@ public void CopyTo(TValue[] array, int arrayIndex)
171
161
if ( array == null ) throw new ArgumentNullException ( ) ;
172
162
if ( arrayIndex < 0 ) throw new ArgumentOutOfRangeException ( ) ;
173
163
if ( arrayIndex + InnerDictionary . Count > array . Length ) throw new ArgumentException ( ) ;
174
- foreach ( TValue item in this )
164
+ foreach ( var item in this )
175
165
{
176
166
array [ arrayIndex ++ ] = item ;
177
167
}
@@ -188,7 +178,7 @@ public IEnumerator<TValue> GetEnumerator()
188
178
RwSyncRootLock . EnterReadLock ( ) ;
189
179
try
190
180
{
191
- foreach ( TValue item in InnerDictionary . Values . Select ( p => p . Value ) )
181
+ foreach ( var item in InnerDictionary . Values . Select ( p => p . Value ) )
192
182
{
193
183
yield return item ;
194
184
}
@@ -211,7 +201,7 @@ public bool Remove(TKey key)
211
201
RwSyncRootLock . EnterWriteLock ( ) ;
212
202
try
213
203
{
214
- if ( ! InnerDictionary . TryGetValue ( key , out CacheItem cacheItem ) ) return false ;
204
+ if ( ! InnerDictionary . TryGetValue ( key , out CacheItem ? cacheItem ) ) return false ;
215
205
RemoveInternal ( cacheItem ) ;
216
206
return true ;
217
207
}
@@ -242,7 +232,7 @@ public bool TryGet(TKey key, out TValue item)
242
232
RwSyncRootLock . EnterReadLock ( ) ;
243
233
try
244
234
{
245
- if ( InnerDictionary . TryGetValue ( key , out CacheItem cacheItem ) )
235
+ if ( InnerDictionary . TryGetValue ( key , out CacheItem ? cacheItem ) )
246
236
{
247
237
OnAccess ( cacheItem ) ;
248
238
item = cacheItem . Value ;
@@ -253,7 +243,7 @@ public bool TryGet(TKey key, out TValue item)
253
243
{
254
244
RwSyncRootLock . ExitReadLock ( ) ;
255
245
}
256
- item = default ;
246
+ item = default ! ;
257
247
return false ;
258
248
}
259
249
}
0 commit comments