-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
1,263 changed files
with
970,077 additions
and
7,841 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
Apizr/Apizr.Extensions.Microsoft.Caching/Apizr.Extensions.Microsoft.Caching.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
Apizr/Apizr.Extensions.Microsoft.Caching/DistributedCacheHandler.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,77 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Apizr.Caching; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Refit; | ||
|
||
namespace Apizr | ||
{ | ||
public class DistributedCacheHandler<TCache> : ICacheHandler | ||
{ | ||
private readonly IDistributedCache _distributedCache; | ||
private readonly IHttpContentSerializer _contentSerializer; | ||
|
||
public DistributedCacheHandler(IDistributedCache distributedCache, IHttpContentSerializer contentSerializer) | ||
{ | ||
_distributedCache = distributedCache ?? throw new ArgumentNullException(nameof(distributedCache), $"You must register an implementation of {nameof(IDistributedCache)} before trying to use it"); | ||
_contentSerializer = contentSerializer; | ||
} | ||
|
||
public async Task SetAsync(string key, object value, TimeSpan? lifeSpan = null, CancellationToken cancellationToken = default) | ||
{ | ||
var options = new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = lifeSpan }; | ||
|
||
if (typeof(TCache) == typeof(byte[])) | ||
{ | ||
var data = value.ToByteArray(); | ||
await _distributedCache.SetAsync(key, data, options, cancellationToken); | ||
} | ||
else if (typeof(TCache) == typeof(string)) | ||
{ | ||
var data = await value.ToJsonStringAsync(_contentSerializer); | ||
await _distributedCache.SetStringAsync(key, data, options, cancellationToken); | ||
} | ||
else | ||
{ | ||
throw new ArgumentException($"{nameof(TCache)} must be either {nameof(String)} or {nameof(Byte)}[]", nameof(TCache)); | ||
} | ||
} | ||
|
||
public async Task<TData> GetAsync<TData>(string key, CancellationToken cancellationToken = default) | ||
{ | ||
if (typeof(TCache) == typeof(byte[])) | ||
{ | ||
var result = await _distributedCache.GetAsync(key, cancellationToken); | ||
return result.FromByteArray<TData>(); | ||
} | ||
else if (typeof(TCache) == typeof(string)) | ||
{ | ||
var result = await _distributedCache.GetStringAsync(key, cancellationToken); | ||
return await result.FromJsonStringAsync<TData>(_contentSerializer, cancellationToken); | ||
} | ||
else | ||
{ | ||
throw new ArgumentException($"{nameof(TCache)} must be either {nameof(String)} or {nameof(Byte)}[]", nameof(TCache)); | ||
} | ||
} | ||
|
||
public async Task<bool> RemoveAsync(string key, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await _distributedCache.RemoveAsync(key, cancellationToken); | ||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public Task ClearAsync(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException("Clearing feature is not available with IDistributedCache"); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Apizr/Apizr.Extensions.Microsoft.Caching/InMemoryCacheHandler.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,51 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Apizr.Caching; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Apizr | ||
{ | ||
public class InMemoryCacheHandler : ICacheHandler | ||
{ | ||
private readonly IMemoryCache _memoryCache; | ||
|
||
public InMemoryCacheHandler(IMemoryCache memoryCache) | ||
{ | ||
_memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache), $"You must register an implementation of {nameof(IMemoryCache)} before trying to use it"); | ||
} | ||
|
||
public Task SetAsync(string key, object value, TimeSpan? lifeSpan = null, CancellationToken cancellationToken = default) | ||
{ | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = lifeSpan }; | ||
_memoryCache.Set(key, value, options); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default) | ||
{ | ||
if (!_memoryCache.TryGetValue<T>(key, out var result)) | ||
result = default; | ||
|
||
return Task.FromResult(result); | ||
} | ||
|
||
public Task<bool> RemoveAsync(string key, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
_memoryCache.Remove(key); | ||
return Task.FromResult(true); | ||
} | ||
catch (Exception) | ||
{ | ||
return Task.FromResult(false); | ||
} | ||
} | ||
|
||
public Task ClearAsync(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Apizr/Apizr.Extensions.Microsoft.Caching/MicrosoftCachingOptionsBuilderExtensions.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,31 @@ | ||
using Apizr.Extending.Configuring.Common; | ||
|
||
[assembly: Apizr.Preserve] | ||
namespace Apizr | ||
{ | ||
public static class MicrosoftCachingOptionsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Use any registered IDistributedCache implementation | ||
/// </summary> | ||
/// <returns></returns> | ||
public static TBuilder WithDistributedCacheHandler<TBuilder, TCache>(this TBuilder builder) where TBuilder : IApizrExtendedCommonOptionsBuilder | ||
{ | ||
builder.WithCacheHandler<DistributedCacheHandler<TCache>>(); | ||
|
||
return builder; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Use any registered IMemoryCache implementation | ||
/// </summary> | ||
/// <returns></returns> | ||
public static TBuilder WithInMemoryCacheHandler<TBuilder>(this TBuilder builder) where TBuilder : IApizrExtendedCommonOptionsBuilder | ||
{ | ||
builder.WithCacheHandler<InMemoryCacheHandler>(); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Apizr/Apizr.Extensions.Microsoft.Caching/SerializationExtensions.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,52 @@ | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Refit; | ||
|
||
namespace Apizr | ||
{ | ||
internal static class SerializationExtensions | ||
{ | ||
internal static byte[] ToByteArray(this object obj) | ||
{ | ||
if (obj == null) | ||
{ | ||
return null; | ||
} | ||
var binaryFormatter = new BinaryFormatter(); | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
binaryFormatter.Serialize(memoryStream, obj); | ||
return memoryStream.ToArray(); | ||
} | ||
} | ||
|
||
internal static T FromByteArray<T>(this byte[] byteArray) | ||
{ | ||
if (byteArray == null) | ||
{ | ||
return default; | ||
} | ||
var binaryFormatter = new BinaryFormatter(); | ||
using (var memoryStream = new MemoryStream(byteArray)) | ||
{ | ||
return (T)binaryFormatter.Deserialize(memoryStream); | ||
} | ||
} | ||
|
||
internal static async Task<string> ToJsonStringAsync(this object obj, IHttpContentSerializer contentSerializer) | ||
{ | ||
var httpContent = contentSerializer.ToHttpContent(obj); | ||
return await httpContent.ReadAsStringAsync(); | ||
} | ||
|
||
internal static async Task<T> FromJsonStringAsync<T>(this string str, IHttpContentSerializer contentSerializer, CancellationToken cancellationToken = default) | ||
{ | ||
var content = new StringContent(str, Encoding.UTF8, "application/json"); | ||
return await contentSerializer.FromHttpContentAsync<T>(content, cancellationToken); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.