Update cache key to use full URL
This commit is contained in:
parent
9ed0b98cf3
commit
14f0ca1dfd
2 changed files with 9 additions and 14 deletions
|
@ -31,13 +31,13 @@ namespace Website.Tests.Data {
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var cache = Substitute.For<IMemoryCache>();
|
var cache = Substitute.For<IMemoryCache>();
|
||||||
cache.TryGetValue("TestApiClient:Get", out _).Returns(false);
|
cache.TryGetValue("TestApiClient:Get/test", out _).Returns(false);
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
var client = new TestApiClient(httpClient, cache);
|
var client = new TestApiClient(httpClient, cache);
|
||||||
(await client.Get<TestObject>("test")).Should().BeEquivalentTo(expected);
|
(await client.Get<TestObject>("test")).Should().BeEquivalentTo(expected);
|
||||||
cache.Received(1).CreateEntry("TestApiClient:Get");
|
cache.Received(1).CreateEntry("TestApiClient:Get/test");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -51,13 +51,13 @@ namespace Website.Tests.Data {
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var cache = Substitute.For<IMemoryCache>();
|
var cache = Substitute.For<IMemoryCache>();
|
||||||
cache.TryGetValue("TestApiClient:Get?query1=1&query2=2", out _).Returns(false);
|
cache.TryGetValue("TestApiClient:Get/test?query1=1&query2=2", out _).Returns(false);
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
var client = new TestApiClient(httpClient, cache);
|
var client = new TestApiClient(httpClient, cache);
|
||||||
(await client.Get<TestObject>("test", new {query1 = 1, query2 = 2})).Should().BeEquivalentTo(expected);
|
(await client.Get<TestObject>("test", new {query1 = 1, query2 = 2})).Should().BeEquivalentTo(expected);
|
||||||
cache.Received(1).CreateEntry("TestApiClient:Get?query1=1&query2=2");
|
cache.Received(1).CreateEntry("TestApiClient:Get/test?query1=1&query2=2");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
@ -27,13 +27,12 @@ namespace Website.Data {
|
||||||
return props.ToDictionary(info => info.Name, info => info.GetValue(query, null).ToString());
|
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)
|
if (method == HttpMethod.Post)
|
||||||
return await call();
|
return await call();
|
||||||
|
|
||||||
var baseKey = GetType().Name + ":" + callerName;
|
var baseKey = GetType().Name + ":" + callerName;
|
||||||
var queryString = query != null ? "?" + string.Join('&', query.Select(pair => pair.Key + "=" + pair.Value)) : string.Empty;
|
var key = baseKey + "/" + url;
|
||||||
var key = baseKey + queryString;
|
|
||||||
|
|
||||||
return await _cache.GetOrCreate(key, async entry => {
|
return await _cache.GetOrCreate(key, async entry => {
|
||||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_cacheDurations[baseKey]);
|
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) {
|
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) {
|
return await DoCachedCall(method, callerName, url, async () => {
|
||||||
queryParameters = ParseQueryParameters(query);
|
|
||||||
url = QueryHelpers.AddQueryString(url, queryParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await DoCachedCall(method, callerName, queryParameters, async () => {
|
|
||||||
using var httpRequest = new HttpRequestMessage(method, url) { Content = content };
|
using var httpRequest = new HttpRequestMessage(method, url) { Content = content };
|
||||||
var response = await _client.SendAsync(httpRequest);
|
var response = await _client.SendAsync(httpRequest);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue