Created
March 4, 2020 11:55
-
-
Save GeradeDev/6b1f61053e29282cda9d909962b279d9 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 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