docker files fixes from liaqat
This commit is contained in:
74
src/Managing.Infrastructure.Storage/CacheService.cs
Normal file
74
src/Managing.Infrastructure.Storage/CacheService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Managing.Application.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Managing.Infrastructure.Storage
|
||||
{
|
||||
public class CacheService : ICacheService
|
||||
{
|
||||
private readonly IDistributedCache distributedCache;
|
||||
|
||||
public CacheService(IDistributedCache distributedCache)
|
||||
{
|
||||
this.distributedCache = distributedCache;
|
||||
}
|
||||
|
||||
public string GetValue(string key)
|
||||
{
|
||||
return distributedCache.GetString(key);
|
||||
}
|
||||
|
||||
public T GetValue<T>(string key)
|
||||
{
|
||||
var cachedData = distributedCache.GetString(key);
|
||||
|
||||
if (!string.IsNullOrEmpty(cachedData))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(cachedData);
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public void RemoveValue(string key)
|
||||
{
|
||||
distributedCache.Remove(key);
|
||||
}
|
||||
|
||||
public string SaveValue(string name, string value)
|
||||
{
|
||||
distributedCache.SetString(name, value);
|
||||
return name;
|
||||
}
|
||||
|
||||
public void SaveValue<T>(string name, T value, TimeSpan slidingExpiration)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions()
|
||||
{
|
||||
SlidingExpiration = slidingExpiration
|
||||
};
|
||||
|
||||
distributedCache.SetString(name, JsonConvert.SerializeObject(value), options);
|
||||
}
|
||||
|
||||
public T GetOrSave<T>(string name, Func<T> action, TimeSpan slidingExpiration)
|
||||
{
|
||||
var cachedData = distributedCache.GetString(name);
|
||||
|
||||
if (!string.IsNullOrEmpty(cachedData))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(cachedData);
|
||||
}
|
||||
|
||||
var options = new DistributedCacheEntryOptions()
|
||||
{
|
||||
SlidingExpiration = slidingExpiration
|
||||
};
|
||||
|
||||
var result = action();
|
||||
|
||||
distributedCache.SetString(name, JsonConvert.SerializeObject(result), options);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Managing.Application\Managing.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
82
src/Managing.Infrastructure.Storage/TaskCache.cs
Normal file
82
src/Managing.Infrastructure.Storage/TaskCache.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Core;
|
||||
//using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Runtime.Caching;
|
||||
|
||||
namespace Managing.Infrastructure.Storage
|
||||
{
|
||||
public class TaskCache : ITaskCache
|
||||
{
|
||||
private MemoryCache _cache { get; } = MemoryCache.Default;
|
||||
private CacheItemPolicy _defaultPolicy { get; } = new CacheItemPolicy();
|
||||
|
||||
public async Task<T> AddOrGetExisting<T>(string key, Func<Task<T>> valueFactory)
|
||||
{
|
||||
|
||||
var asyncLazyValue = new AsyncLazy<T>(valueFactory);
|
||||
var existingValue = (AsyncLazy<T>)_cache.AddOrGetExisting(key, asyncLazyValue, _defaultPolicy);
|
||||
|
||||
if (existingValue != null)
|
||||
{
|
||||
asyncLazyValue = existingValue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await asyncLazyValue;
|
||||
|
||||
// The awaited Task has completed. Check that the task still is the same version
|
||||
// that the cache returns (i.e. the awaited task has not been invalidated during the await).
|
||||
if (asyncLazyValue != _cache.AddOrGetExisting(key, new AsyncLazy<T>(valueFactory), _defaultPolicy))
|
||||
{
|
||||
// The awaited value is no more the most recent one.
|
||||
// Get the most recent value with a recursive call.
|
||||
return await AddOrGetExisting(key, valueFactory);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Task object for the given key failed with exception. Remove the task from the cache.
|
||||
_cache.Remove(key);
|
||||
// Re throw the exception to be handled by the caller.
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void Invalidate(string key)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
}
|
||||
|
||||
public bool Contains(string key)
|
||||
{
|
||||
return _cache.Contains(key);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
// A snapshot of keys is taken to avoid enumerating collection during changes.
|
||||
var keys = _cache.Select(i => i.Key).ToList();
|
||||
keys.ForEach(k => _cache.Remove(k));
|
||||
}
|
||||
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
var existingValue = (AsyncLazy<T>)_cache.Get(key);
|
||||
return existingValue.Value.Result;
|
||||
}
|
||||
|
||||
public virtual List<T> GetCache<T>()
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
|
||||
foreach (var item in _cache)
|
||||
{
|
||||
list.Add((T)item.Value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user