-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ First Edition of HybridCachingProvider.
- Loading branch information
1 parent
e4ab66d
commit 3aa72e2
Showing
12 changed files
with
446 additions
and
41 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
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,12 @@ | ||
using System; | ||
namespace EasyCaching.Core | ||
{ | ||
public class EasyCachingMessage | ||
{ | ||
public string CacheKey { get; set; } | ||
|
||
public object CacheValue { get; set; } | ||
|
||
public TimeSpan Expiration { get; set; } | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\EasyCaching.Core\EasyCaching.Core.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Autofac" Version="4.6.2" /> | ||
</ItemGroup> | ||
</Project> |
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,294 @@ | ||
namespace EasyCaching.HybridCache | ||
{ | ||
using Autofac; | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Internal; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Hybrid caching provider. | ||
/// </summary> | ||
public interface IHybridCachingProvider : IEasyCachingProvider{} | ||
|
||
/// <summary> | ||
/// Hybrid caching provider. | ||
/// </summary> | ||
public class HybridCachingProvider : IHybridCachingProvider | ||
{ | ||
/// <summary> | ||
/// The local caching provider. | ||
/// </summary> | ||
private IEasyCachingProvider _localCachingProvider; | ||
|
||
/// <summary> | ||
/// The distributed caching provider. | ||
/// </summary> | ||
private IEasyCachingProvider _distributedCachingProvider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class. | ||
/// </summary> | ||
/// <param name="context">Context.</param> | ||
public HybridCachingProvider(IComponentContext context) | ||
{ | ||
this._localCachingProvider = context.ResolveKeyed<IEasyCachingProvider>("Local"); | ||
this._distributedCachingProvider = context.ResolveKeyed<IEasyCachingProvider>("Distributed"); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> is | ||
/// distributed cache. | ||
/// </summary> | ||
/// <value><c>true</c> if is distributed cache; otherwise, <c>false</c>.</value> | ||
public bool IsDistributedCache => true; | ||
|
||
/// <summary> | ||
/// Exists the specified cacheKey. | ||
/// </summary> | ||
/// <returns>The exists.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
public bool Exists(string cacheKey) | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey,nameof(cacheKey)); | ||
|
||
var flag = false; | ||
|
||
flag = _localCachingProvider.Exists(cacheKey); | ||
|
||
if(!flag) | ||
{ | ||
flag = _distributedCachingProvider.Exists(cacheKey); | ||
} | ||
|
||
return flag; | ||
} | ||
|
||
/// <summary> | ||
/// Existses the specified cacheKey async. | ||
/// </summary> | ||
/// <returns>The async.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
public async Task<bool> ExistsAsync(string cacheKey) | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
|
||
var flag = false; | ||
|
||
flag = await _localCachingProvider.ExistsAsync(cacheKey); | ||
|
||
if (!flag) | ||
{ | ||
flag = await _distributedCachingProvider.ExistsAsync(cacheKey); | ||
} | ||
|
||
return flag; | ||
} | ||
|
||
/// <summary> | ||
/// Get the specified cacheKey, dataRetriever and expiration. | ||
/// </summary> | ||
/// <returns>The get.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <param name="dataRetriever">Data retriever.</param> | ||
/// <param name="expiration">Expiration.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public CacheValue<T> Get<T>(string cacheKey, Func<T> dataRetriever, TimeSpan expiration) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); | ||
|
||
var value = _localCachingProvider.Get<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
value = _distributedCachingProvider.Get<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
var item = dataRetriever?.Invoke(); | ||
if (item != null) | ||
{ | ||
Set(cacheKey, item, expiration); | ||
return new CacheValue<T>(item, true); | ||
} | ||
else | ||
{ | ||
//TODO : Set a null value to cache!! | ||
|
||
return CacheValue<T>.NoValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get the specified cacheKey. | ||
/// </summary> | ||
/// <returns>The get.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public CacheValue<T> Get<T>(string cacheKey) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
|
||
var value = _localCachingProvider.Get<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
value = _distributedCachingProvider.Get<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
else | ||
{ | ||
//TODO : Set a null value to cache!! | ||
|
||
return CacheValue<T>.NoValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the specified cacheKey, dataRetriever and expiration async. | ||
/// </summary> | ||
/// <returns>The async.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <param name="dataRetriever">Data retriever.</param> | ||
/// <param name="expiration">Expiration.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public async Task<CacheValue<T>> GetAsync<T>(string cacheKey, Func<Task<T>> dataRetriever, TimeSpan expiration) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); | ||
|
||
var value = await _localCachingProvider.GetAsync<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
value = await _distributedCachingProvider.GetAsync<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
var item = await dataRetriever?.Invoke(); | ||
if (item != null) | ||
{ | ||
await SetAsync(cacheKey, item, expiration); | ||
return new CacheValue<T>(item, true); | ||
} | ||
else | ||
{ | ||
//TODO : Set a null value to cache!! | ||
|
||
return CacheValue<T>.NoValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the specified cacheKey async. | ||
/// </summary> | ||
/// <returns>The async.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
|
||
var value = await _localCachingProvider.GetAsync<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
|
||
value = await _distributedCachingProvider.GetAsync<T>(cacheKey); | ||
|
||
if (value.HasValue) | ||
{ | ||
return value; | ||
} | ||
else | ||
{ | ||
//TODO : Set a null value to cache!! | ||
|
||
return CacheValue<T>.NoValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Remove the specified cacheKey. | ||
/// </summary> | ||
/// <returns>The remove.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
public void Remove(string cacheKey) | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
|
||
_localCachingProvider.Remove(cacheKey); | ||
_distributedCachingProvider.Remove(cacheKey); | ||
} | ||
|
||
/// <summary> | ||
/// Removes the specified cacheKey async. | ||
/// </summary> | ||
/// <returns>The async.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
public async Task RemoveAsync(string cacheKey) | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
|
||
await _localCachingProvider.RemoveAsync(cacheKey); | ||
await _distributedCachingProvider.RemoveAsync(cacheKey); | ||
} | ||
|
||
/// <summary> | ||
/// Set the specified cacheKey, cacheValue and expiration. | ||
/// </summary> | ||
/// <returns>The set.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <param name="cacheValue">Cache value.</param> | ||
/// <param name="expiration">Expiration.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public void Set<T>(string cacheKey, T cacheValue, TimeSpan expiration) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue)); | ||
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); | ||
|
||
_localCachingProvider.Set(cacheKey,cacheValue,expiration); | ||
_distributedCachingProvider.Set(cacheKey, cacheValue, expiration); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the specified cacheKey, cacheValue and expiration async. | ||
/// </summary> | ||
/// <returns>The async.</returns> | ||
/// <param name="cacheKey">Cache key.</param> | ||
/// <param name="cacheValue">Cache value.</param> | ||
/// <param name="expiration">Expiration.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public async Task SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration) where T : class | ||
{ | ||
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); | ||
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue)); | ||
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); | ||
|
||
await _localCachingProvider.SetAsync(cacheKey, cacheValue, expiration); | ||
await _distributedCachingProvider.SetAsync(cacheKey, cacheValue, expiration); | ||
} | ||
} | ||
} |
Oops, something went wrong.