diff --git a/build/releasenotes.props b/build/releasenotes.props index a271a2aa..4f57e2a4 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,19 +1,20 @@ - 1. Support distributed lock. + 1. Expose raw cache database. - 1. Support distributed lock. + 1. Expose raw cache database. - 1. Support distributed lock. + 1. Expose raw cache database. + 2. Support KeyPrefix. - 1. Upgrading dependencies. + 1. Expose raw cache database. - 1. Upgrading dependencies. + 1. Expose raw cache database. 1. Upgrading dependencies. @@ -52,7 +53,7 @@ 1. Upgrading dependencies. - 1. Upgrading dependencies. + 1. Expose raw cache database. 1. Upgrading dependencies. diff --git a/build/version.props b/build/version.props index 6086c650..bb654ad0 100644 --- a/build/version.props +++ b/build/version.props @@ -1,24 +1,24 @@ - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 - 1.4.0 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 + 1.4.1 diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs index 7ffa02e1..b090616b 100644 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs @@ -3,7 +3,7 @@ using EasyCaching.Core; using EasyCaching.Core.DistributedLock; using EasyCaching.Core.Serialization; -using EasyCaching.CSRedis.DistributedLock; + using EasyCaching.CSRedis.DistributedLock; using global::CSRedis; using Microsoft.Extensions.Logging; using System; @@ -482,9 +482,8 @@ public override TimeSpan BaseGetExpiration(string cacheKey) /// Get te information of this provider. /// /// - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => _cache; } } diff --git a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs index f6e0f0b3..8dac2e5a 100644 --- a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs +++ b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs @@ -31,6 +31,8 @@ public abstract class EasyCachingAbstractProvider : IEasyCachingProvider public CachingProviderType CachingProviderType => this.ProviderType; public CacheStats CacheStats => this.ProviderStats; + public object Database => BaseGetDatabse(); + protected EasyCachingAbstractProvider() { } protected EasyCachingAbstractProvider(IDistributedLockFactory lockFactory, BaseProviderOptions options) @@ -39,6 +41,7 @@ protected EasyCachingAbstractProvider(IDistributedLockFactory lockFactory, BaseP _options = options; } + public abstract object BaseGetDatabse(); public abstract bool BaseExists(string cacheKey); public abstract Task BaseExistsAsync(string cacheKey); public abstract void BaseFlush(); diff --git a/src/EasyCaching.Core/IEasyCachingProvider.cs b/src/EasyCaching.Core/IEasyCachingProvider.cs index 9bd512bd..0aca8d21 100644 --- a/src/EasyCaching.Core/IEasyCachingProvider.cs +++ b/src/EasyCaching.Core/IEasyCachingProvider.cs @@ -115,5 +115,10 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// /// ProviderInfo GetProviderInfo(); + + /// + /// Get or sets the provider's database + /// + object Database { get; } } } diff --git a/src/EasyCaching.Disk/DefaultDiskCachingProvider.cs b/src/EasyCaching.Disk/DefaultDiskCachingProvider.cs index 9e0a3ffc..b85cfd30 100644 --- a/src/EasyCaching.Disk/DefaultDiskCachingProvider.cs +++ b/src/EasyCaching.Disk/DefaultDiskCachingProvider.cs @@ -673,9 +673,8 @@ private void DeleteDirectory(string path) } } - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => throw new Exception("Disk provider don't support this "); } } diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs index 300b53c3..420b193d 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs @@ -375,9 +375,8 @@ public override TimeSpan BaseGetExpiration(string cacheKey) /// Get te information of this provider. /// /// - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => _cache; } } \ No newline at end of file diff --git a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs index c25fc02a..f8f19409 100644 --- a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs +++ b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs @@ -402,9 +402,8 @@ public override TimeSpan BaseGetExpiration(string cacheKey) /// Get te information of this provider. /// /// - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => _cache; } } \ No newline at end of file diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs index b1bede62..633f612c 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs @@ -412,6 +412,8 @@ public override ProviderInfo BaseGetProviderInfo() return _info; } + public override object BaseGetDatabse() => _memcachedClient; + private void OnCacheHit(string cacheKey) { CacheStats.OnHit(); 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/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 242708e7..50a9fb22 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -512,9 +512,8 @@ public override TimeSpan BaseGetExpiration(string cacheKey) /// Get te information of this provider. /// /// - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => _cache; } } diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs index 20a12b8a..7610a3f2 100644 --- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs +++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs @@ -413,9 +413,8 @@ public override TimeSpan BaseGetExpiration(string cacheKey) /// Get te information of this provider. /// /// - public override ProviderInfo BaseGetProviderInfo() - { - return _info; - } + public override ProviderInfo BaseGetProviderInfo() => _info; + + public override object BaseGetDatabse() => _cache; } } diff --git a/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs index 50c22dd9..ee327dec 100644 --- a/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs @@ -38,6 +38,23 @@ protected override IEasyCachingProvider CreateCachingProvider(Action(); } + + [Fact] + public void GetDatabase_Should_Succeed() + { + var db = _provider.Database; + + Assert.NotNull(db); + Assert.IsType(db); + } + + [Fact] + public void GetDatabase_And_Use_Raw_Method_Should_Succeed() + { + var db = (EasyCachingCSRedisClient)_provider.Database; + var flag = db.Ping(); + Assert.True(flag); + } } public class CSRedisCachingProviderWithNamedSerTest diff --git a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs index 9f54a23e..f63936fa 100644 --- a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs @@ -221,6 +221,22 @@ public async Task Distributed_Set_Async_Throw_Exception_Should_Not_Break() Assert.Equal(default(string), res.Value); } + [Fact] + public void Iss_311_Test() + { + var cacheKey = $"{_namespace}_iss311_{Guid.NewGuid()}"; + + hybridCaching_1.Set(cacheKey, "val", TimeSpan.FromSeconds(30)); + + var afterSet = hybridCaching_1.Exists(cacheKey); + Assert.True(afterSet); + + hybridCaching_1.Remove(cacheKey); + + var afterRemove = hybridCaching_1.Exists(cacheKey); + Assert.False(afterRemove); + } + private void FakeCreatProvider() { A.CallTo(() => fakeFactory.GetCachingProvider("m1")).Returns(new FakeLocalCachingProvider()); diff --git a/test/EasyCaching.UnitTests/CachingTests/LiteDBCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/LiteDBCachingTest.cs index 5623df1a..fced96e0 100644 --- a/test/EasyCaching.UnitTests/CachingTests/LiteDBCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/LiteDBCachingTest.cs @@ -9,6 +9,7 @@ using System.IO; using System.Threading.Tasks; using Xunit; + using global::LiteDB; public class LiteDBCachingTest : BaseCachingProviderTest { @@ -45,6 +46,15 @@ protected override Task GetAsync_Parallel_Should_Succeed() protected override void Get_Parallel_Should_Succeed() { } + + [Fact] + public void GetDatabase_Should_Succeed() + { + var db = _provider.Database; + + Assert.NotNull(db); + Assert.IsAssignableFrom>(db); + } } diff --git a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs index 9964aa78..02a9fdc1 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs @@ -186,8 +186,24 @@ private void GetCacheItem(string cacheKey, string prefix) var val = _provider.Get(cacheKey); Assert.False(val.HasValue); } - } + [Fact] + public void GetDatabase_Should_Succeed() + { + var db = _provider.Database; + + Assert.NotNull(db); + Assert.IsType(db); + } + + [Fact] + public void GetDatabase_And_Use_Raw_Method_Should_Succeed() + { + var db = (EasyCachingMemcachedClient)_provider.Database; + var stats = db.Stats(); + Assert.NotNull(stats); + } + } public class MemcachedProviderWithFactoryTest : BaseCachingProviderWithFactoryTest { diff --git a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs index c6de8bc1..41f54a01 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs @@ -109,7 +109,17 @@ public void Issues150_DeepClone_Object_Test() Assert.Equal("catcherwong", res2.Value.Name); } + + [Fact] + public void GetDatabase_Should_Succeed() + { + var db = _provider.Database; + + Assert.NotNull(db); + Assert.IsType(db); + } } + public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest { public MemoryCachingProviderWithFactoryTest() diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 195ae855..4c963a57 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -95,6 +95,23 @@ public void Use_Configuration_String_Should_Succeed() Assert.Equal(8, dbProvider.GetDatabase().Database); } + + [Fact] + public void GetDatabase_Should_Succeed() + { + var db = _provider.Database; + + Assert.NotNull(db); + Assert.IsAssignableFrom(db); + } + + [Fact] + public void GetDatabase_And_Use_Raw_Method_Should_Succeed() + { + var db = (StackExchange.Redis.IDatabase)_provider.Database; + var ts = db.Ping(); + Assert.True(ts.Ticks > 0); + } } @@ -199,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 diff --git a/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs b/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs index 753fe188..59c3165d 100644 --- a/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs @@ -165,5 +165,10 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim { return Task.FromResult(false); } + + public override object BaseGetDatabse() + { + return null; + } } } diff --git a/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs b/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs index 2c43f991..a5927505 100644 --- a/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs @@ -21,6 +21,8 @@ public class FakeDistributedCachingProvider : IEasyCachingProvider public CacheStats CacheStats => new CacheStats(); + public object Database => null; + public virtual bool Exists(string cacheKey) { return true; diff --git a/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs b/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs index b5a15566..f1fa863a 100644 --- a/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs @@ -21,6 +21,8 @@ public class FakeLocalCachingProvider : IEasyCachingProvider public CacheStats CacheStats => new CacheStats(); + public object Database => null; + public bool Exists(string cacheKey) { return true;