Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/MammothCache.InMemory/InMemoryCacheDecorator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

Expand Down Expand Up @@ -75,10 +79,24 @@ public Task SetAsync<T>(string key, T value, TimeSpan? expiry = null)

public void Remove(string key, bool exactMatch = true)
{
if (!exactMatch)
throw new NotSupportedException("This action is not supported yet");
if (exactMatch)
{
Cache.Remove(key);
return;
}

if (Cache is MemoryCache memoryCache)
{
List<string> entries = GetAllKeys();
if (entries != null)
{
string pattern = "^" + Regex.Escape(key).Replace("\\*", ".*").Replace("\\?", ".") + "$";
Regex regex = new(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

Cache.Remove(key);
foreach (string entry in entries.Where(entry => regex.IsMatch(entry)))
memoryCache.Remove(entry);
}
}
}

public Task RemoveAsync(string key, bool exactMatch = true)
Expand All @@ -87,6 +105,27 @@ public Task RemoveAsync(string key, bool exactMatch = true)
return Task.CompletedTask;
}

private List<string> GetAllKeys()
{
FieldInfo coherentState = typeof(MemoryCache).GetField("_coherentState", BindingFlags.NonPublic | BindingFlags.Instance);
object coherentStateValue = coherentState.GetValue(Cache);
PropertyInfo stringEntriesCollection = coherentStateValue.GetType().GetProperty("StringEntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);

List<string> keys = [];

if (stringEntriesCollection.GetValue(coherentStateValue) is ICollection stringEntriesCollectionValue)
{
foreach (object item in stringEntriesCollectionValue)
{
PropertyInfo methodInfo = item.GetType().GetProperty("Key");
object val = methodInfo.GetValue(item);
keys.Add(val.ToString());
}
}

return keys;
}

public Task RemoveAsync(IEnumerable<string> keys)
{
foreach (string key in keys)
Expand Down
2 changes: 1 addition & 1 deletion src/MammothCache.InMemory/MammothCache.InMemory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageVersion>1.1.0.0-beta.5</PackageVersion>
<PackageVersion>1.1.1.0-beta.1</PackageVersion>
<PackageIcon>logo.png</PackageIcon>
</PropertyGroup>
<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MammothCache.Redis/MammothCache.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PropertyGroup>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageVersion>1.1.0-beta.5</PackageVersion>
<PackageVersion>1.1.1.0-beta.1</PackageVersion>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
Expand Down
11 changes: 11 additions & 0 deletions src/MammothCache.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MammothCache.InMemory", "Ma
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MammothCache.Redis", "MammothCache.Redis\MammothCache.Redis.csproj", "{38B1816D-29D7-4D5E-A1FB-1772FC0B044E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MammothCache.InMemory.Tests", "..\test\MammothCache.InMemory.Tests\MammothCache.InMemory.Tests.csproj", "{C7A1EE8B-243B-4668-A884-4ACF91449B52}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,10 +31,17 @@ Global
{38B1816D-29D7-4D5E-A1FB-1772FC0B044E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38B1816D-29D7-4D5E-A1FB-1772FC0B044E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38B1816D-29D7-4D5E-A1FB-1772FC0B044E}.Release|Any CPU.Build.0 = Release|Any CPU
{C7A1EE8B-243B-4668-A884-4ACF91449B52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7A1EE8B-243B-4668-A884-4ACF91449B52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7A1EE8B-243B-4668-A884-4ACF91449B52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7A1EE8B-243B-4668-A884-4ACF91449B52}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C7A1EE8B-243B-4668-A884-4ACF91449B52} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BBFF732A-AEFB-4861-9BB6-18E1D577498B}
EndGlobalSection
Expand Down
2 changes: 1 addition & 1 deletion src/MammothCache/MammothCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageVersion>1.1.0.0</PackageVersion>
<PackageVersion>1.1.1.0-beta.1</PackageVersion>
</PropertyGroup>
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
25 changes: 25 additions & 0 deletions test/MammothCache.InMemory.Tests/InMemoryCacheDecoratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Caching.Memory;

namespace MammothCache.InMemory.Tests
{
[TestClass]
public sealed class InMemoryCacheDecoratorTests
{
[TestMethod]
public void InMemoryCacheDecorator_Remove_Wildcard_ShouldDeleteAll()
{
InMemoryCacheDecorator cache = new(new MemoryCache(new MemoryCacheOptions()));
cache.Set("key1:suffix1", true);
cache.Set("key1:suffix2", true);
cache.Set("prefix1:key1:suffix2", true);
cache.Set("key2", true);

cache.Remove("key1:*", false);

Assert.IsTrue(cache.Get<bool?>("key1:suffix1") == null);
Assert.IsTrue(cache.Get<bool?>("key1:suffix2") == null);
Assert.IsTrue(cache.Get<bool?>("key2") == true);
Assert.IsTrue(cache.Get<bool?>("prefix1:key1:suffix2") == true);
}
}
}
1 change: 1 addition & 0 deletions test/MammothCache.InMemory.Tests/MSTestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="MSTest.Sdk/3.6.4">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!--
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MammothCache.InMemory\MammothCache.InMemory.csproj" />
</ItemGroup>

</Project>