Skip to content

Commit

Permalink
Merge pull request #11 from sj-distributor/cache-double-deletion-stra…
Browse files Browse the repository at this point in the history
…tegy

feature Cache double deletion strategy
  • Loading branch information
pygzfei authored Apr 13, 2024
2 parents df57b9a + da54ae7 commit 4f04b17
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
14 changes: 8 additions & 6 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FastCache.Core.Driver;
using FastCache.Core.Entity;
using FastCache.InMemory.Enum;
using FastCache.InMemory.Extension;

namespace FastCache.InMemory.Drivers
{
Expand Down Expand Up @@ -72,25 +73,26 @@ public Task Delete(string key, string prefix = "")

if (string.IsNullOrEmpty(key))
{
queryList.ToList().ForEach(k => _dist.TryRemove(k, out var _));
queryList.ToList().ForEach(k => _dist.TryRemove(k, out var _, 3));
}
else
{
queryList.Where(x => x.Contains(key)).ToList().ForEach(k => _dist.TryRemove(k, out var _));
queryList.Where(x => x.Contains(key)).ToList().ForEach(k => _dist.TryRemove(k, out var _, 3));
}
}
else
{
var removeKey = string.IsNullOrEmpty(prefix) ? key : $"{prefix}:{key}";
_dist.TryRemove(removeKey, out var _);

_dist.TryRemove(removeKey, out var _, 3);
}

return Task.CompletedTask;
}

public Task Delete(string key)
{
_dist.TryRemove(key, out _);
_dist.TryRemove(key, out var _, 3);
return Task.CompletedTask;
}

Expand All @@ -104,7 +106,7 @@ private void ReleaseCached()
{
foreach (var key in _dist.Keys.TakeLast(removeRange))
{
_dist.Remove(key, out var _);
_dist.TryRemove(key, out var _, 3);
}
}
else
Expand All @@ -125,7 +127,7 @@ private void ReleaseCached()

foreach (var keyValuePair in keyValuePairs.TakeLast(removeRange))
{
_dist.Remove(keyValuePair.Key, out var _);
_dist.TryRemove(keyValuePair.Key, out var _, 3);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions FastCache.InMemory/Extension/DictExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace FastCache.InMemory.Extension
{
public static class DictExtension
{
public static bool TryRemove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key,
out TValue value, int seconds = 3)
{
var res = dictionary.TryRemove(key, out value);
if (res && seconds > 0)
{
Task.Delay(TimeSpan.FromSeconds(seconds)).ContinueWith(_ => { dictionary.TryRemove(key, out var _); });
}
return res;
}
}
}
32 changes: 32 additions & 0 deletions UnitTests/MemoryCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FastCache.Core.Entity;
using FastCache.Core.Utils;
using FastCache.InMemory.Drivers;
Expand Down Expand Up @@ -199,4 +200,35 @@ public async void TestMemoryCacheCanDeleteByGeneralMatchAsteriskWithPrefix(strin
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
}

[Theory]
[InlineData("anson", "ansonExpire333", "*", "18", null)]
[InlineData("anson", "ansonExpire444", "*", "19", null)]
[InlineData("anson", "ansonExpire555", "*", "20", null)]
public async void TestMemoryCacheDoubleDelete(string prefix, string key,
string deleteKey,
string value,
string result)
{
var fullKey = $"{prefix}:{key}";
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);

await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
});

await Task.Delay(TimeSpan.FromSeconds(4));

s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
}
}

0 comments on commit 4f04b17

Please sign in to comment.