Created
April 16, 2022 14:07
-
-
Save nishanc/aee01e9fa889661fe355eeceaca0aa8e to your computer and use it in GitHub Desktop.
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
public static class CacheHelper | |
{ | |
public static async Task SetRecordAsync<T>(this IDistributedCache cache, | |
string recordId, | |
T data, | |
TimeSpan? absoluteExpireTime = null, | |
TimeSpan? slidingExpireTime = null) | |
{ | |
var options = new DistributedCacheEntryOptions(); | |
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromSeconds(60); | |
options.SlidingExpiration = slidingExpireTime; | |
var jsonData = JsonSerializer.Serialize(data); | |
await cache.SetStringAsync(recordId, jsonData, options); | |
} | |
public static async Task<T?> GetRecordAsync<T>(this IDistributedCache cache, | |
string recordId) | |
{ | |
var jsonData = await cache.GetStringAsync(recordId); | |
if (jsonData is null) | |
{ | |
return default(T); | |
} | |
return JsonSerializer.Deserialize<T>(jsonData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment