Update cache key to use full URL

This commit is contained in:
Robert Marshall 2020-04-18 15:19:18 +01:00
parent 9ed0b98cf3
commit 14f0ca1dfd
2 changed files with 9 additions and 14 deletions

View file

@ -27,13 +27,12 @@ namespace Website.Data {
return props.ToDictionary(info => info.Name, info => info.GetValue(query, null).ToString());
}
private async Task<TResult> DoCachedCall<TResult>(HttpMethod method, string callerName, IDictionary<string, string> query, Func<Task<TResult>> call) {
private async Task<TResult> DoCachedCall<TResult>(HttpMethod method, string callerName, string url, Func<Task<TResult>> call) {
if (method == HttpMethod.Post)
return await call();
var baseKey = GetType().Name + ":" + callerName;
var queryString = query != null ? "?" + string.Join('&', query.Select(pair => pair.Key + "=" + pair.Value)) : string.Empty;
var key = baseKey + queryString;
var key = baseKey + "/" + url;
return await _cache.GetOrCreate(key, async entry => {
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_cacheDurations[baseKey]);
@ -42,14 +41,10 @@ namespace Website.Data {
}
private async Task<T> Send<T>(HttpMethod method, string url, object query, string callerName, HttpContent content = null) {
IDictionary<string, string> queryParameters = null;
if (query != null)
url = QueryHelpers.AddQueryString(url, ParseQueryParameters(query));
if (query != null) {
queryParameters = ParseQueryParameters(query);
url = QueryHelpers.AddQueryString(url, queryParameters);
}
return await DoCachedCall(method, callerName, queryParameters, async () => {
return await DoCachedCall(method, callerName, url, async () => {
using var httpRequest = new HttpRequestMessage(method, url) { Content = content };
var response = await _client.SendAsync(httpRequest);