forked from masastack/MASA.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af78655
commit 4516689
Showing
48 changed files
with
2,926 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Caching/MASA.Utils.Caching.Core/DependencyInjection/CachingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace MASA.Utils.Caching.Core.DependencyInjection; | ||
|
||
public class CachingBuilder : ICachingBuilder | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CachingBuilder"/> class. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> | ||
public CachingBuilder(IServiceCollection services, string name) | ||
{ | ||
Services = services; | ||
Name = name; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IServiceCollection Services { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public string Name { get; private set; } | ||
} |
30 changes: 30 additions & 0 deletions
30
...ng/MASA.Utils.Caching.Core/DependencyInjection/DistributedCacheClientBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace MASA.Utils.Caching.Core.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods for configuring an <see cref="ICachingBuilder"/> | ||
/// </summary> | ||
public static class DistributedCacheClientBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a delegate that will be used to configure a named <see cref="IDistributedCacheClient"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ICachingBuilder"/>.</param> | ||
/// <param name="configureOptions">A delegate that is used to configure an <see cref="IDistributedCacheClient"/>.</param> | ||
/// <returns>An <see cref="ICachingBuilder"/> that can be used to configure the client.</returns> | ||
public static ICachingBuilder ConfigureDistributedCacheClient<TOptions>(this ICachingBuilder builder, Action<TOptions> configureOptions) where TOptions : class | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
if (configureOptions == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configureOptions)); | ||
} | ||
|
||
builder.Services.Configure(builder.Name, configureOptions); | ||
|
||
return builder; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Caching/MASA.Utils.Caching.Core/DependencyInjection/ICachingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace MASA.Utils.Caching.Core.DependencyInjection; | ||
|
||
/// <summary> | ||
/// A builder for configuring named <see cref="ICachingBuilder"/> instances. | ||
/// </summary> | ||
public interface ICachingBuilder | ||
{ | ||
/// <summary> | ||
/// Gets the application service collection. | ||
/// </summary> | ||
IServiceCollection Services { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the client configured by this builder. | ||
/// </summary> | ||
string Name { get; } | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Caching/MASA.Utils.Caching.Core/DistributedCacheClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace MASA.Utils.Caching.Core; | ||
|
||
public abstract class DistributedCacheClientFactory<TOptions> : IDistributedCacheClientFactory | ||
{ | ||
private readonly IOptionsMonitor<TOptions> _optionsMonitor; | ||
|
||
private readonly ConcurrentDictionary<string, Lazy<IDistributedCacheClient>> _clients; | ||
private readonly Func<string, Lazy<IDistributedCacheClient>> _clientFactory; | ||
|
||
public DistributedCacheClientFactory(IOptionsMonitor<TOptions> optionsMonitor) | ||
{ | ||
if (optionsMonitor == null) | ||
{ | ||
throw new ArgumentNullException(nameof(optionsMonitor)); | ||
} | ||
|
||
_optionsMonitor = optionsMonitor; | ||
|
||
_clients = new ConcurrentDictionary<string, Lazy<IDistributedCacheClient>>(); | ||
|
||
_clientFactory = (name) => | ||
{ | ||
return new Lazy<IDistributedCacheClient>(() => | ||
{ | ||
return CreateClientHandler(name); | ||
}); | ||
}; | ||
} | ||
|
||
// <inherit /> | ||
public IDistributedCacheClient CreateClient(string name) | ||
{ | ||
name ??= string.Empty; | ||
|
||
var client = _clients.GetOrAdd(name, _clientFactory); | ||
|
||
return client.Value; | ||
} | ||
|
||
internal protected abstract IDistributedCacheClient CreateClientHandler(string name); | ||
|
||
protected TOptions GetOptions(string name) | ||
{ | ||
return _optionsMonitor.Get(name); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Caching/MASA.Utils.Caching.Core/DistributedCacheClientFactoryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace MASA.Utils.Caching.Core; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IDistributedCacheClientFactory"/>. | ||
/// </summary> | ||
public static class DistributedCacheClientFactoryExtensions | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="IDistributedCacheClient"/> using the default configuration. | ||
/// </summary> | ||
/// <param name="factory">The <see cref="IDistributedCacheClientFactory"/>.</param> | ||
/// <returns>An <see cref="IDistributedCacheClient"/> configured using the default configuration.</returns> | ||
public static IDistributedCacheClient CreateClient(this IDistributedCacheClientFactory factory) | ||
{ | ||
if (factory == null) | ||
{ | ||
throw new ArgumentNullException(nameof(factory)); | ||
} | ||
|
||
return factory.CreateClient(string.Empty); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Caching/MASA.Utils.Caching.Core/Helpers/SubscribeHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace MASA.Utils.Caching.Core.Helpers; | ||
|
||
/// <summary> | ||
/// The subscribe helper. | ||
/// </summary> | ||
public static class SubscribeHelper | ||
{ | ||
/// <summary> | ||
/// Formats the memory cache key. | ||
/// </summary> | ||
/// <param name="key">The key.</param> | ||
/// <returns>A string.</returns> | ||
public static string FormatMemoryCacheKey<T>(string key) | ||
{ | ||
var type = typeof(T); | ||
if (type.IsGenericType) | ||
{ | ||
var dictType = typeof(Dictionary<,>); | ||
if (type.GetGenericTypeDefinition() == dictType) | ||
key += type.Name + "[" + type.GetGenericArguments()[1].Name + "]"; | ||
else | ||
key += type.Name + "[" + type.GetGenericArguments()[0].Name + "]"; | ||
} | ||
else | ||
{ | ||
key += typeof(T).Name; | ||
} | ||
|
||
return key; | ||
} | ||
|
||
/// <summary> | ||
/// Formats the subscribe channel. | ||
/// </summary> | ||
/// <param name="key">The key.</param> | ||
/// <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 = "") | ||
{ | ||
var valueTypeFullName = typeof(T).FullName; | ||
switch (type) | ||
{ | ||
case SubscribeKeyTypes.ValueTypeFullName: | ||
return valueTypeFullName; | ||
case SubscribeKeyTypes.ValueTypeFullNameAndKey: | ||
return $"[{valueTypeFullName}]{key}"; | ||
case SubscribeKeyTypes.SpecificPrefix: | ||
return $"{prefix}{key}"; | ||
default: | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/Caching/MASA.Utils.Caching.Core/Interfaces/ICacheClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
namespace MASA.Utils.Caching.Core.Interfaces; | ||
|
||
/// <summary> | ||
/// The interface for cache client. | ||
/// </summary> | ||
public interface ICacheClient : IDisposable | ||
{ | ||
/// <summary> | ||
/// Gets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the request value.</param> | ||
/// <returns>The located value or null.</returns> | ||
T Get<T>(string key); | ||
|
||
/// <summary> | ||
/// Gets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the request value.</param> | ||
/// <returns>The <see cref="Task"/> that represents the asynchronous opertion, containing the located value or null.</returns> | ||
Task<T> GetAsync<T>(string key); | ||
|
||
/// <summary> | ||
/// Gets or sets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the request value.</param> | ||
/// <param name="setter">The setter.</param> | ||
/// <param name="options">The <see cref="CombinedCacheEntryOptions"/>.</param> | ||
/// <returns>The located value.</returns> | ||
T GetOrSet<T>(string key, Func<T> setter, CombinedCacheEntryOptions<T> options = null); | ||
|
||
/// <summary> | ||
/// Gets or sets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the request value.</param> | ||
/// <param name="setter">The setter.</param> | ||
/// <param name="options">The <see cref="CombinedCacheEntryOptions"/>.</param> | ||
/// <returns>The <see cref="Task"/> that represents the asynchronous opertion, containing the located value.</returns> | ||
Task<T> GetOrSetAsync<T>(string key, Func<T> setter, CombinedCacheEntryOptions<T> options = null); | ||
|
||
/// <summary> | ||
/// Gets a list of values with given keys. | ||
/// </summary> | ||
/// <param name="keys">A list of string identifying the request value.</param> | ||
/// <returns>The located values without <see langword="null"/>.</returns> | ||
IEnumerable<T> GetList<T>(string[] keys); | ||
|
||
/// <summary> | ||
/// Gets a list of values with given keys. | ||
/// </summary> | ||
/// <param name="keys">A list of string identifying the request value.</param> | ||
/// <returns>The <see cref="Task"/> that represents the asynchronous opertion, containing the located values with <see langword="null"/>.</returns> | ||
Task<IEnumerable<T>> GetListAsync<T>(string[] keys); | ||
|
||
/// <summary> | ||
/// Removes a list of values with given keys. | ||
/// </summary> | ||
/// <param name="keys">A list of string identifying the requested value.</param> | ||
void Remove<T>(params string[] keys); | ||
|
||
/// <summary> | ||
/// Removes a list of values with given keys. | ||
/// </summary> | ||
/// <param name="keys">A list of string identifying the requested value.</param> | ||
Task RemoveAsync<T>(params string[] keys); | ||
|
||
/// <summary> | ||
/// Sets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the requested value.</param> | ||
/// <param name="value">The value to set int the cache.</param> | ||
/// <param name="options">The cache options for the value.</param> | ||
void Set<T>(string key, T value, CombinedCacheEntryOptions<T> options = null); | ||
|
||
/// <summary> | ||
/// Sets a value with given key. | ||
/// </summary> | ||
/// <param name="key">A string identifying the requested value.</param> | ||
/// <param name="value">The value to set int the cache.</param> | ||
/// <param name="options">The cache options for the value.</param> | ||
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns> | ||
Task SetAsync<T>(string key, T value, CombinedCacheEntryOptions<T> options = null); | ||
|
||
/// <summary> | ||
/// Sets a list of cahce items contains key and value. | ||
/// </summary> | ||
/// <param name="keyValues">The <see cref="Dictionary{TKey, TValue}"/> contains the cache key and cache value.</param> | ||
/// <param name="options">The cache options for the value.</param> | ||
void SetList<T>(Dictionary<string, T> keyValues, CombinedCacheEntryOptions<T> options = null); | ||
|
||
/// <summary> | ||
/// Sets a list of cahce items contains key and value. | ||
/// </summary> | ||
/// <param name="keyValues">The <see cref="Dictionary{TKey, TValue}"/> contains the cache key and cache value.</param> | ||
/// <param name="options">The cache options for the value.</param> | ||
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns> | ||
Task SetListAsync<T>(Dictionary<string, T> keyValues, CombinedCacheEntryOptions<T> options = null); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Caching/MASA.Utils.Caching.Core/Interfaces/ICacheClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace MASA.Utils.Caching.Core.Interfaces; | ||
|
||
/// <summary> | ||
/// A factory abstraction for a component that can create instances of <typeparamref name="TICacheClinet" /> type with custom | ||
/// configuration for a given logical name. | ||
/// </summary> | ||
public interface ICacheClientFactory<TICacheClinet> | ||
{ | ||
/// <summary> | ||
/// Creates and configures an instance of <typeparamref name="TICacheClinet" /> type using the configuration that corresponds | ||
/// to the logical name specified by <paramref name="name"/>. | ||
/// </summary> | ||
/// <param name="name">The logical name of the client to create.</param> | ||
/// <returns>A new instance of <typeparamref name="TICacheClinet" /> type.</returns> | ||
TICacheClinet CreateClient(string name); | ||
} |
Oops, something went wrong.