Skip to content

Commit

Permalink
Publish
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyBP committed Mar 30, 2022
2 parents d3dcd4c + 90401f1 commit b118392
Show file tree
Hide file tree
Showing 1,263 changed files with 970,077 additions and 7,841 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>Apizr.Integrations.Shiny.xml</DocumentationFile>
<DocumentationFile>Apizr.Extensions.Microsoft.Caching.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shiny.Core" Version="1.2.0.1755" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Apizr.Extensions.Microsoft.DependencyInjection\Apizr.Extensions.Microsoft.DependencyInjection.csproj" />
<ProjectReference Include="..\Apizr\Apizr.csproj" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 Apizr/Apizr.Extensions.Microsoft.Caching/InMemoryCacheHandler.cs
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();
}
}
}
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;
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.1" />
<PackageReference Include="Refit.HttpClientFactory" Version="6.0.24" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.2" />
<PackageReference Include="Refit.HttpClientFactory" Version="6.3.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit b118392

Please sign in to comment.