@@ -17,6 +17,8 @@ public class InMemoryCaching : IInMemoryCaching
17
17
private long _cacheSize = 0L ;
18
18
private const string _UPTOLIMIT_KEY = "inter_up_to_limit_key" ;
19
19
20
+ public event EventHandler < EvictedEventArgs > Evicted ;
21
+
20
22
public InMemoryCaching ( string name , InMemoryCachingOptions optionsAccessor )
21
23
{
22
24
ArgumentCheck . NotNull ( optionsAccessor , nameof ( optionsAccessor ) ) ;
@@ -32,7 +34,7 @@ public InMemoryCaching(string name, InMemoryCachingOptions optionsAccessor)
32
34
public void Clear ( string prefix = "" )
33
35
{
34
36
if ( string . IsNullOrWhiteSpace ( prefix ) )
35
- {
37
+ {
36
38
_memory . Clear ( ) ;
37
39
38
40
if ( _options . SizeLimit . HasValue )
@@ -53,9 +55,13 @@ public int GetCount(string prefix = "")
53
55
54
56
internal void RemoveExpiredKey ( string key )
55
57
{
56
- bool flag = _memory . TryRemove ( key , out _ ) ;
57
- if ( _options . SizeLimit . HasValue && flag )
58
- Interlocked . Decrement ( ref _cacheSize ) ;
58
+ if ( _memory . TryRemove ( key , out _ ) )
59
+ {
60
+ Evicted ? . Invoke ( this , new EvictedEventArgs ( key ) ) ;
61
+
62
+ if ( _options . SizeLimit . HasValue )
63
+ Interlocked . Decrement ( ref _cacheSize ) ;
64
+ }
59
65
}
60
66
61
67
public CacheValue < T > Get < T > ( string key )
@@ -189,7 +195,7 @@ private bool SetInternal(CacheEntry entry, bool addOnly = false)
189
195
190
196
_memory . AddOrUpdate ( deep . Key , deep , ( k , cacheEntry ) => deep ) ;
191
197
192
- if ( _options . SizeLimit . HasValue )
198
+ if ( _options . SizeLimit . HasValue )
193
199
Interlocked . Increment ( ref _cacheSize ) ;
194
200
}
195
201
}
@@ -239,7 +245,7 @@ public int RemoveAll(IEnumerable<string> keys = null)
239
245
continue ;
240
246
241
247
if ( _memory . TryRemove ( key , out _ ) )
242
- {
248
+ {
243
249
removed ++ ;
244
250
if ( _options . SizeLimit . HasValue )
245
251
Interlocked . Decrement ( ref _cacheSize ) ;
@@ -254,7 +260,7 @@ public bool Remove(string key)
254
260
bool flag = _memory . TryRemove ( key , out _ ) ;
255
261
256
262
if ( _options . SizeLimit . HasValue && ! key . Equals ( _UPTOLIMIT_KEY ) && flag )
257
- {
263
+ {
258
264
Interlocked . Decrement ( ref _cacheSize ) ;
259
265
}
260
266
@@ -270,10 +276,10 @@ public int RemoveByPrefix(string prefix)
270
276
public int RemoveByPattern ( string searchKey , SearchKeyPattern searchPattern )
271
277
{
272
278
var keysToRemove = _memory . Keys . Where ( x => FilterByPattern ( x , searchKey , searchPattern ) ) . ToList ( ) ;
273
-
279
+
274
280
return RemoveAll ( keysToRemove ) ;
275
281
}
276
-
282
+
277
283
private static bool FilterByPattern ( string key , string searchKey , SearchKeyPattern searchKeyPattern )
278
284
{
279
285
switch ( searchKeyPattern )
@@ -336,7 +342,10 @@ private void ScanForExpiredItems(InMemoryCaching cache)
336
342
var now = SystemClock . UtcNow ;
337
343
foreach ( var entry in cache . _memory . Values . Where ( x => x . ExpiresAt < now ) )
338
344
{
339
- cache . Remove ( entry . Key ) ;
345
+ if ( cache . Remove ( entry . Key ) )
346
+ {
347
+ Evicted ? . Invoke ( this , new EvictedEventArgs ( entry . Key ) ) ;
348
+ }
340
349
}
341
350
}
342
351
@@ -403,7 +412,7 @@ internal object Value
403
412
public T GetValue < T > ( bool isDeepClone = true )
404
413
{
405
414
object val = Value ;
406
-
415
+
407
416
var t = typeof ( T ) ;
408
417
409
418
if ( t == TypeHelper . BoolType || t == TypeHelper . StringType || t == TypeHelper . CharType || t == TypeHelper . DateTimeType || t . IsNumeric ( ) )
@@ -412,7 +421,7 @@ public T GetValue<T>(bool isDeepClone = true)
412
421
if ( t == TypeHelper . NullableBoolType || t == TypeHelper . NullableCharType || t == TypeHelper . NullableDateTimeType || t . IsNullableNumeric ( ) )
413
422
return val == null ? default ( T ) : ( T ) Convert . ChangeType ( val , Nullable . GetUnderlyingType ( t ) ) ;
414
423
415
- return isDeepClone
424
+ return isDeepClone
416
425
? DeepClonerGenerator . CloneObject < T > ( ( T ) val )
417
426
: ( T ) val ;
418
427
}
0 commit comments