Skip to content

Instantly share code, notes, and snippets.

@GeradeDev
Created March 4, 2020 11:55
Show Gist options
  • Save GeradeDev/6b1f61053e29282cda9d909962b279d9 to your computer and use it in GitHub Desktop.
Save GeradeDev/6b1f61053e29282cda9d909962b279d9 to your computer and use it in GitHub Desktop.
public class ResponseCacheService : IResponseCacheService
{
private readonly IDistributedCache _distributedCache;
public ResponseCacheService(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
public async Task CacheResponseAsync(string cacheKey, object response, TimeSpan timeTimeLive)
{
if (response == null)
{
return;
}
var serializedResponse = JsonConvert.SerializeObject(response);
await _distributedCache.SetStringAsync(cacheKey, serializedResponse, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = timeTimeLive
});
}
public async Task<string> GetCachedResponseAsync(string cacheKey)
{
var cachedResponse = await _distributedCache.GetStringAsync(cacheKey);
return string.IsNullOrEmpty(cachedResponse) ? null : cachedResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment