#8828: Expose Redis API to clear multiple keys using wildcard syntax (#8829)

* #8828: Expose Redis API to clear multiple keys using wildcard syntax

* #8828: Rename IRedisCacheStorageProvider to ICacheStorageProviderWithKeyPrefix

* Moving ICacheStorageProviderWithKeyPrefix to Orchard.Caching

---------

Co-authored-by: Benedek Farkas <benedek.farkas@lombiq.com>
This commit is contained in:
Marek Dzikiewicz
2025-04-16 08:28:43 -04:00
committed by GitHub
parent 3561be6e37
commit 4c463287ca
2 changed files with 9 additions and 2 deletions

View File

@@ -8,4 +8,8 @@ namespace Orchard.Caching.Services {
void Remove(string key); void Remove(string key);
void Clear(); void Clear();
} }
public interface ICacheStorageProviderWithKeyPrefix : ICacheStorageProvider {
void Clear(string key);
}
} }

View File

@@ -9,10 +9,9 @@ using StackExchange.Redis;
using System; using System;
namespace Orchard.Redis.Caching { namespace Orchard.Redis.Caching {
[OrchardFeature("Orchard.Redis.Caching")] [OrchardFeature("Orchard.Redis.Caching")]
[OrchardSuppressDependency("Orchard.Caching.Services.DefaultCacheStorageProvider")] [OrchardSuppressDependency("Orchard.Caching.Services.DefaultCacheStorageProvider")]
public class RedisCacheStorageProvider : Component, ICacheStorageProvider { public class RedisCacheStorageProvider : Component, ICacheStorageProviderWithKeyPrefix {
public const string ConnectionStringKey = "Orchard.Redis.Cache"; public const string ConnectionStringKey = "Orchard.Redis.Cache";
private readonly ShellSettings _shellSettings; private readonly ShellSettings _shellSettings;
@@ -61,6 +60,10 @@ namespace Orchard.Redis.Caching {
_connectionMultiplexer.KeyDeleteWithPrefix(GetLocalizedKey("*")); _connectionMultiplexer.KeyDeleteWithPrefix(GetLocalizedKey("*"));
} }
public void Clear(string key) {
_connectionMultiplexer.KeyDeleteWithPrefix(GetLocalizedKey($"{_shellSettings.Name}:{key}"));
}
private string GetLocalizedKey(string key) { private string GetLocalizedKey(string key) {
return _shellSettings.Name + ":Cache:" + key; return _shellSettings.Name + ":Cache:" + key;
} }