Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: RefactoringCaching #235

Merged
merged 65 commits into from
Sep 27, 2022
Merged
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
a8310ed
refactor(Caching): Refactor the cache
zhenlei520 Aug 23, 2022
44810f9
refactor: Refactor DistributedCacheClient.StackExchangeRedis
zhenlei520 Aug 24, 2022
408a798
refactor(Redis): Optimize performance
zhenlei520 Aug 25, 2022
9321902
feature: Optimize Caching And Add `KeyExpireAsync`
zhenlei520 Aug 26, 2022
c3befb5
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Aug 29, 2022
64b33da
refactor(Cache): Refactor the cache
zhenlei520 Aug 29, 2022
8d228b8
feature(Cache): Perfect unit tests
zhenlei520 Aug 30, 2022
eaa0496
feature(Caching): Waiting to troubleshoot the problem of option value…
zhenlei520 Aug 30, 2022
18cd648
feature(Configuration): Modify TryAddConfiguration methods
zhenlei520 Aug 30, 2022
99abaf8
refactor(Caching): Optimize to judge the true type
zhenlei520 Aug 31, 2022
30f2f4a
test(Caching): Fix unit test errors
zhenlei520 Aug 31, 2022
84fd1a2
test: Add Redis UnitTest
zhenlei520 Sep 1, 2022
3fde920
feat: Extend GetList, Refresh, Remove methods
zhenlei520 Sep 1, 2022
30041e9
rename: DataCacheOptions -> DataCacheModel
zhenlei520 Sep 1, 2022
d230af8
refactor(caching): Refactor Distributed Cache And ICacheClient
zhenlei520 Sep 1, 2022
ceb7cb8
feat(Caching): Add MultilevelCache
zhenlei520 Sep 1, 2022
551c796
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 2, 2022
70aeb35
fix(Caching):fix get type error
zhenlei520 Sep 2, 2022
1e6c8a4
chore(Caching): Optimize empty judgment
zhenlei520 Sep 2, 2022
5e86881
fix: Optimize Redis, fix refresh failure
zhenlei520 Sep 2, 2022
e57c056
test: Add Redis UnitTest
zhenlei520 Sep 2, 2022
b5e142e
chore: Remove invalid references
zhenlei520 Sep 2, 2022
bcdbadf
chore: fix warning
zhenlei520 Sep 2, 2022
9d00a88
chore: global using sort
zhenlei520 Sep 2, 2022
c2caa99
feat(Caching): Add MultilevelCache
zhenlei520 Sep 2, 2022
7978d95
test: Perfect unit tests
zhenlei520 Sep 5, 2022
8524dee
fix(Redis): Fix when the key does not exist, the GetOrSet method for …
zhenlei520 Sep 5, 2022
ece7ae8
doc(Caching): Add Caching Doc
zhenlei520 Sep 5, 2022
3b691b5
chore: global using sort
zhenlei520 Sep 5, 2022
57e4b0c
test(Caching): Modify the redis service link address
zhenlei520 Sep 6, 2022
15fafa3
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 6, 2022
51222d4
test(Caching): Modify the redis service link address
zhenlei520 Sep 6, 2022
b680b40
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 6, 2022
740cb5e
Test(Caching): Fix unit tests
zhenlei520 Sep 6, 2022
4a0b7dc
chore: fix bad code smell
zhenlei520 Sep 6, 2022
05e7b03
chore: fix bad code smell
zhenlei520 Sep 6, 2022
23817e7
feat(Caching): Remove Utils.Caching
zhenlei520 Sep 7, 2022
469c628
chore: Solve the bad code smell, modify the unit test
zhenlei520 Sep 7, 2022
8948e9e
test: Add UnitTest
zhenlei520 Sep 7, 2022
324fc19
Test(Caching): Add UnitTest
zhenlei520 Sep 7, 2022
71b7da1
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 7, 2022
92c849e
Test: Add UnitTest
zhenlei520 Sep 8, 2022
661a180
Test(Caching): Perfect unit tests
zhenlei520 Sep 8, 2022
11058e1
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 8, 2022
f957f3c
Test: Perfect unit tests
zhenlei520 Sep 8, 2022
002b4d9
chore: fix bad code smell
zhenlei520 Sep 8, 2022
858f32f
chore: Ignore JsonSerializerExtension unit test
zhenlei520 Sep 8, 2022
4c39f42
test: Perfect unit tests
zhenlei520 Sep 8, 2022
3d09ff3
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 8, 2022
1789159
test: Fix unit test errors
zhenlei520 Sep 8, 2022
51c5f8d
Test: Add UnitTest
zhenlei520 Sep 9, 2022
38d5d08
chore: deal with bad taste
zhenlei520 Sep 9, 2022
0726c86
feat: Add coverlet.msbuild
zhenlei520 Sep 9, 2022
aea3d8b
chore: use global using
zhenlei520 Sep 9, 2022
ab28c2e
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 21, 2022
9274c6b
chore: Fix as suggested
zhenlei520 Sep 27, 2022
5f1ee1c
doc: Modify README
zhenlei520 Sep 27, 2022
081745e
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 27, 2022
c828e83
chore: Dealing with bad code smells
zhenlei520 Sep 27, 2022
5c7f1b6
chore: Modify the Identity directory structure
zhenlei520 Sep 27, 2022
2f5d8dd
rename: BaseDistributedCacheClient -> RedisClientBase
zhenlei520 Sep 27, 2022
2b9d8be
fix: fix warning
zhenlei520 Sep 27, 2022
a6fed31
chore: Remove the ToList method
zhenlei520 Sep 27, 2022
b7f67ba
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Sep 27, 2022
eb18321
chore: Add global using
zhenlei520 Sep 27, 2022
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
212 changes: 119 additions & 93 deletions Masa.Framework.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public abstract class CacheClientBase : ICacheClient
{
public abstract T? Get<T>(string key);

public abstract Task<T?> GetAsync<T>(string key);

public IEnumerable<T?> GetList<T>(params string[] keys)
=> GetList<T>(GetKeys(keys));

public abstract IEnumerable<T?> GetList<T>(IEnumerable<string> keys);

public Task<IEnumerable<T?>> GetListAsync<T>(params string[] keys)
=> GetListAsync<T>(GetKeys(keys));

public abstract Task<IEnumerable<T?>> GetListAsync<T>(IEnumerable<string> keys);

public virtual void Set<T>(string key, T value, DateTimeOffset absoluteExpiration)
=> Set(key, value, new CacheEntryOptions(absoluteExpiration));

public virtual void Set<T>(string key, T value, TimeSpan absoluteExpirationRelativeToNow)
=> Set(key, value, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public abstract void Set<T>(string key, T value, CacheEntryOptions? options = null);

public virtual Task SetAsync<T>(string key, T value, DateTimeOffset absoluteExpiration)
=> SetAsync(key, value, new CacheEntryOptions(absoluteExpiration));

public virtual Task SetAsync<T>(string key, T value, TimeSpan absoluteExpirationRelativeToNow)
=> SetAsync(key, value, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public abstract Task SetAsync<T>(string key, T value, CacheEntryOptions? options = null);

public virtual void SetList<T>(Dictionary<string, T?> keyValues, DateTimeOffset absoluteExpiration)
=> SetList(keyValues, new CacheEntryOptions(absoluteExpiration));

public virtual void SetList<T>(Dictionary<string, T?> keyValues, TimeSpan absoluteExpirationRelativeToNow)
=> SetList(keyValues, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public abstract void SetList<T>(Dictionary<string, T?> keyValues, CacheEntryOptions? options = null);

public virtual Task SetListAsync<T>(Dictionary<string, T?> keyValues, DateTimeOffset absoluteExpiration)
=> SetListAsync(keyValues, new CacheEntryOptions(absoluteExpiration));

public virtual Task SetListAsync<T>(Dictionary<string, T?> keyValues, TimeSpan absoluteExpirationRelativeToNow)
=> SetListAsync(keyValues, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public abstract Task SetListAsync<T>(Dictionary<string, T?> keyValues, CacheEntryOptions? options = null);

protected static IEnumerable<string> GetKeys(params string[] keys) => keys;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public class CacheEntry<T> : CacheEntryOptions<T>
{
public T Value { get; }

public CacheEntry(T value)
{
Value = value;
}

public CacheEntry(T value, DateTimeOffset absoluteExpiration) : this(value)
=> AbsoluteExpiration = absoluteExpiration;

public CacheEntry(T value, TimeSpan absoluteExpirationRelativeToNow) : this(value)
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public class CombinedCacheEntry<T>
{
public CacheEntryOptions? MemoryCacheEntryOptions { get; set; }

public Func<CacheEntry<T>> DistributedCacheEntryFunc { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public abstract class CacheClientFactoryBase<TService> : MasaFactoryBase<TService, CacheRelationOptions<TService>>,
ICacheClientFactory<TService> where TService : class
{
protected CacheClientFactoryBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public class DistributedCacheClientFactoryBase : CacheClientFactoryBase<IDistributedCacheClient>, IDistributedCacheClientFactory
{
protected override string DefaultServiceNotFoundMessage
=> "Default DistributedCache not found, you need to add it, like services.AddStackExchangeRedisCache()";

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] DistributedCache, it was not found";

protected override MasaFactoryOptions<CacheRelationOptions<IDistributedCacheClient>> FactoryOptions => _optionsMonitor.CurrentValue;

private readonly IOptionsMonitor<DistributedCacheFactoryOptions> _optionsMonitor;

public DistributedCacheClientFactoryBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<DistributedCacheFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public class MultilevelCacheClientFactoryBase : CacheClientFactoryBase<IMultilevelCacheClient>, IMultilevelCacheClientFactory
{
protected override string DefaultServiceNotFoundMessage
=> "Default MultilevelCache not found, you need to add it, like services.AddMultilevelCache()";

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] MultilevelCache, it was not found";

protected override MasaFactoryOptions<CacheRelationOptions<IMultilevelCacheClient>> FactoryOptions => _optionsMonitor.CurrentValue;

private readonly IOptionsMonitor<MultilevelCacheFactoryOptions> _optionsMonitor;

public MultilevelCacheClientFactoryBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<MultilevelCacheFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public abstract class DistributedCacheClientBase : CacheClientBase, IDistributedCacheClient
{
public abstract T? GetOrSet<T>(string key, Func<CacheEntry<T>> setter);

public abstract Task<T?> GetOrSetAsync<T>(string key, Func<CacheEntry<T>> setter);

public void Refresh(params string[] keys) => Refresh(GetKeys(keys));

public abstract void Refresh(IEnumerable<string> keys);

public Task RefreshAsync(params string[] keys) => RefreshAsync(GetKeys(keys));

public abstract Task RefreshAsync(IEnumerable<string> keys);

public virtual void Remove(params string[] keys) => Remove(GetKeys(keys));

public abstract void Remove(IEnumerable<string> keys);

public Task RemoveAsync(params string[] keys) => RemoveAsync(GetKeys(keys));

public abstract Task RemoveAsync(IEnumerable<string> keys);

public abstract bool Exists(string key);

public abstract Task<bool> ExistsAsync(string key);

public abstract IEnumerable<string> GetKeys(string keyPattern);

public abstract Task<IEnumerable<string>> GetKeysAsync(string keyPattern);

public abstract IEnumerable<KeyValuePair<string, T?>> GetByKeyPattern<T>(string keyPattern);

public abstract Task<IEnumerable<KeyValuePair<string, T?>>> GetByKeyPatternAsync<T>(string keyPattern);

public abstract void Publish(string channel, Action<PublishOptions> options);

public abstract Task PublishAsync(string channel, Action<PublishOptions> options);

public abstract void Subscribe<T>(string channel, Action<SubscribeOptions<T>> options);

public abstract Task SubscribeAsync<T>(string channel, Action<SubscribeOptions<T>> options);

public abstract Task<long> HashIncrementAsync(string key, long value = 1);

public abstract Task<long> HashDecrementAsync(string key, long value = 1, long defaultMinVal = 0);

public virtual bool KeyExpire(string key, TimeSpan absoluteExpirationRelativeToNow)
=> KeyExpire(key, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public virtual bool KeyExpire(string key, DateTimeOffset absoluteExpiration)
=> KeyExpire(key, new CacheEntryOptions(absoluteExpiration));

public abstract bool KeyExpire(string key, CacheEntryOptions? options = null);

public abstract long KeyExpire(IEnumerable<string> keys, CacheEntryOptions? options = null);

public virtual Task<bool> KeyExpireAsync(string key, DateTimeOffset absoluteExpiration)
=> KeyExpireAsync(key, new CacheEntryOptions(absoluteExpiration));

public virtual Task<bool> KeyExpireAsync(string key, TimeSpan absoluteExpirationRelativeToNow)
=> KeyExpireAsync(key, new CacheEntryOptions(absoluteExpirationRelativeToNow));

public abstract Task<bool> KeyExpireAsync(string key, CacheEntryOptions? options = null);

public abstract Task<long> KeyExpireAsync(IEnumerable<string> keys, CacheEntryOptions? options = null);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Caching.Core.Models;
namespace Masa.BuildingBlocks.Caching;

/// <summary>
/// The subscribe key type.
/// </summary>
public enum SubscribeKeyTypes
public enum SubscribeKeyType
{
ValueTypeFullName = 1,

Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Caching.Core.Models;
namespace Masa.BuildingBlocks.Caching;

/// <summary>
/// The subscribe operation.
/// </summary>
public enum SubscribeOperation
{
Set = 1,

Remove = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection TryAddDistributedCacheCore(this IServiceCollection services)
{
services.TryAddSingleton<IDistributedCacheClientFactory, DistributedCacheClientFactoryBase>();
services.TryAddSingleton(serviceProvider
=> serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create());
return services;
}

public static IServiceCollection TryAddMultilevelCacheCore(this IServiceCollection services)
{
services.TryAddSingleton<IMultilevelCacheClientFactory, MultilevelCacheClientFactoryBase>();
services.TryAddSingleton(serviceProvider
=> serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create());
return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Caching.Core.Helpers;
namespace Masa.BuildingBlocks.Caching;

/// <summary>
/// The subscribe helper.
/// </summary>
public static class SubscribeHelper
{
/// <summary>
@@ -39,16 +36,16 @@ public static string FormatMemoryCacheKey<T>(string key)
/// <param name="type">The type.</param>
/// <param name="prefix">The prefix.</param>
/// <returns>A string.</returns>
public static string FormatSubscribeChannel<T>(string key, SubscribeKeyTypes type, string prefix = "")
public static string FormatSubscribeChannel<T>(string key, SubscribeKeyType type, string prefix = "")
{
var valueTypeFullName = typeof(T).FullName!;
switch (type)
{
case SubscribeKeyTypes.ValueTypeFullName:
case SubscribeKeyType.ValueTypeFullName:
return valueTypeFullName;
case SubscribeKeyTypes.ValueTypeFullNameAndKey:
case SubscribeKeyType.ValueTypeFullNameAndKey:
return $"[{valueTypeFullName}]{key}";
case SubscribeKeyTypes.SpecificPrefix:
case SubscribeKeyType.SpecificPrefix:
return $"{prefix}{key}";
default:
throw new NotImplementedException();
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public interface ICacheClient
{
T? Get<T>(string key);

Task<T?> GetAsync<T>(string key);

IEnumerable<T?> GetList<T>(params string[] keys);

IEnumerable<T?> GetList<T>(IEnumerable<string> keys);

Task<IEnumerable<T?>> GetListAsync<T>(params string[] keys);

Task<IEnumerable<T?>> GetListAsync<T>(IEnumerable<string> keys);

void Set<T>(string key, T value, DateTimeOffset absoluteExpiration);

void Set<T>(string key, T value, TimeSpan absoluteExpirationRelativeToNow);

void Set<T>(string key, T value, CacheEntryOptions? options = null);

Task SetAsync<T>(string key, T value, DateTimeOffset absoluteExpiration);

Task SetAsync<T>(string key, T value, TimeSpan absoluteExpirationRelativeToNow);

Task SetAsync<T>(string key, T value, CacheEntryOptions? options = null);

void SetList<T>(Dictionary<string, T?> keyValues, DateTimeOffset absoluteExpiration);

void SetList<T>(Dictionary<string, T?> keyValues, TimeSpan absoluteExpirationRelativeToNow);

void SetList<T>(Dictionary<string, T?> keyValues, CacheEntryOptions? options = null);

Task SetListAsync<T>(Dictionary<string, T?> keyValues, DateTimeOffset absoluteExpiration);

Task SetListAsync<T>(Dictionary<string, T?> keyValues, TimeSpan absoluteExpirationRelativeToNow);

Task SetListAsync<T>(Dictionary<string, T?> keyValues, CacheEntryOptions? options = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Caching;

public interface ICacheClientFactory<out TService> : IMasaFactory<TService> where TService : class
{

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Caching.Core.DependencyInjection;
namespace Masa.BuildingBlocks.Caching;

/// <summary>
/// A builder for configuring named <see cref="ICachingBuilder"/> instances.
/// </summary>
public interface ICachingBuilder
{
/// <summary>
Loading