From c3322556c3d05b7a1e8eb482956e6910e2e0ec98 Mon Sep 17 00:00:00 2001 From: "v.la" Date: Mon, 4 Oct 2021 04:05:14 +0800 Subject: [PATCH] feat: SERedis Support KeyPrefix Feature #325 --- .../Configurations/RedisDBOptions.cs | 5 ++ .../Configurations/RedisDatabaseProvider.cs | 9 ++- .../CachingTests/RedisCachingProviderTest.cs | 57 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/EasyCaching.Redis/Configurations/RedisDBOptions.cs b/src/EasyCaching.Redis/Configurations/RedisDBOptions.cs index 3d0c85a1..bb7ca54b 100644 --- a/src/EasyCaching.Redis/Configurations/RedisDBOptions.cs +++ b/src/EasyCaching.Redis/Configurations/RedisDBOptions.cs @@ -24,5 +24,10 @@ public class RedisDBOptions : BaseRedisOptions /// Specifies the time in milliseconds that the system should allow for synchronous operations (defaults to 5 seconds) /// public int SyncTimeout { get; set; } + + /// + /// Gets or sets the Redis database KeyPrefix will use. + /// + public string KeyPrefix { get; set; } } } diff --git a/src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs b/src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs index 7bb61612..e1435bc8 100644 --- a/src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs +++ b/src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs @@ -1,6 +1,7 @@ namespace EasyCaching.Redis { using StackExchange.Redis; + using StackExchange.Redis.KeyspaceIsolation; using System; using System.Collections.Generic; using System.Linq; @@ -20,7 +21,7 @@ public class RedisDatabaseProvider : IRedisDatabaseProvider /// The connection multiplexer. /// private Lazy _connectionMultiplexer; - + public RedisDatabaseProvider(string name, RedisOptions options) { _options = options.DBConfig; @@ -39,7 +40,11 @@ public IDatabase GetDatabase() { try { - return _connectionMultiplexer.Value.GetDatabase(); + + var database = _connectionMultiplexer.Value.GetDatabase(); + if (!string.IsNullOrEmpty(_options.KeyPrefix)) + database = database.WithKeyPrefix(_options.KeyPrefix); + return database; } catch (Exception) { diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 5eeca4b8..4c963a57 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -216,4 +216,61 @@ public void NamedSerializerTest() Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name); } } + + public class RedisCachingProviderWithKeyPrefixTest + { + private readonly IEasyCachingProviderFactory _providerFactory; + + public RedisCachingProviderWithKeyPrefixTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddEasyCaching(x => + { + + x.UseRedis(options => + { + options.DBConfig = new RedisDBOptions + { + AllowAdmin = true + }; + options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); + options.DBConfig.Database = 8; + options.SerializerName = "json"; + }, "NotKeyPrefix"); + + x.UseRedis(options => + { + options.DBConfig = new RedisDBOptions + { + AllowAdmin = true, + KeyPrefix = "foo:" + }; + options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); + options.DBConfig.Database = 8; + options.SerializerName = "json"; + }, "WithKeyPrefix"); + + x.WithJson("json"); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + _providerFactory = serviceProvider.GetService(); + } + + [Fact] + public void KeyPrefixTest() + { + var NotKeyPrefix = _providerFactory.GetCachingProvider("NotKeyPrefix"); + var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix"); + + WithKeyPrefix.Set("KeyPrefix", "ok", TimeSpan.FromSeconds(10)); + + var val1 = NotKeyPrefix.Get("foo:" + "KeyPrefix"); + var val2 = WithKeyPrefix.Get("foo:" + "KeyPrefix"); + Assert.NotEqual(val1.Value, val2.Value); + + var val3 = WithKeyPrefix.Get("KeyPrefix"); + Assert.Equal(val1.Value, val3.Value); + } + } } \ No newline at end of file