mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
Implement value caches
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
<RootNamespace>System</RootNamespace>
|
<RootNamespace>System</RootNamespace>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<Version>1.6.8</Version>
|
<Version>1.6.9</Version>
|
||||||
<Authors>Alexandru Macocian</Authors>
|
<Authors>Alexandru Macocian</Authors>
|
||||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||||
<Description>Extensions for the System namespace</Description>
|
<Description>Extensions for the System namespace</Description>
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace System.Cache;
|
||||||
|
public sealed class AsyncValueCache<T>(Func<Task<T>> cacheRefreshOperation, TimeSpan cacheDuration)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
private readonly Func<Task<T>> cacheRefreshOperation = cacheRefreshOperation.ThrowIfNull(nameof(cacheRefreshOperation));
|
||||||
|
private readonly TimeSpan cacheDuration = cacheDuration;
|
||||||
|
|
||||||
|
private T? value;
|
||||||
|
private DateTime lastCacheRefresh = DateTime.MinValue;
|
||||||
|
|
||||||
|
public async Task<T> GetValue()
|
||||||
|
{
|
||||||
|
if (DateTime.Now - this.lastCacheRefresh < this.cacheDuration &&
|
||||||
|
this.value is not null)
|
||||||
|
{
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.PerformCacheRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<T> ForceCacheRefresh()
|
||||||
|
{
|
||||||
|
return await this.PerformCacheRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<T> PerformCacheRefresh()
|
||||||
|
{
|
||||||
|
this.value = await this.cacheRefreshOperation();
|
||||||
|
this.lastCacheRefresh = DateTime.Now;
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System.Extensions;
|
||||||
|
|
||||||
|
namespace System.Cache;
|
||||||
|
public sealed class ValueCache<T>(Func<T> cacheRefreshOperation, TimeSpan cacheDuration)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
private readonly Func<T> cacheRefreshOperation = cacheRefreshOperation.ThrowIfNull(nameof(cacheRefreshOperation));
|
||||||
|
private readonly TimeSpan cacheDuration = cacheDuration;
|
||||||
|
|
||||||
|
private T? value;
|
||||||
|
private DateTime lastCacheRefresh = DateTime.MinValue;
|
||||||
|
|
||||||
|
public T GetValue()
|
||||||
|
{
|
||||||
|
if (DateTime.Now - this.lastCacheRefresh < this.cacheDuration &&
|
||||||
|
this.value is not null)
|
||||||
|
{
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.PerformCacheRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T ForceCacheRefresh()
|
||||||
|
{
|
||||||
|
return this.PerformCacheRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private T PerformCacheRefresh()
|
||||||
|
{
|
||||||
|
this.value = this.cacheRefreshOperation();
|
||||||
|
this.lastCacheRefresh = DateTime.Now;
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<RootNamespace>System</RootNamespace>
|
<RootNamespace>System</RootNamespace>
|
||||||
<Version>1.6.8</Version>
|
<Version>1.6.9</Version>
|
||||||
<Authors>Alexandru Macocian</Authors>
|
<Authors>Alexandru Macocian</Authors>
|
||||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||||
<Description>Extensions for the System namespace</Description>
|
<Description>Extensions for the System namespace</Description>
|
||||||
|
|||||||
Reference in New Issue
Block a user