Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b57230d

Browse files
bremnesRupengLiu
andcommitted
Caching response from Azure rest api (#326)
Co-authored-by: RupengLiu <[email protected]>
1 parent 6092a93 commit b57230d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors/EntityExtractor.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
using System.Net.Http;
1+
using System.Collections.Generic;
2+
using System.Net.Http;
23
using System.Net.Http.Headers;
34
using System.Threading.Tasks;
45
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
5-
using System.Collections.Generic;
66
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
7+
using Microsoft.Extensions.Caching.Memory;
78

89
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
910
{
1011
public class EntityExtractor
1112
{
1213
public string baseUrl = "https://management.azure.com";
1314
internal Authentication auth = new Authentication();
15+
private static readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
1416

1517
public static async Task<string> CallApiManagementAsync(string azToken, string requestUrl)
1618
{
19+
if (_cache.TryGetValue(requestUrl, out string cachedResponseBody))
20+
{
21+
return cachedResponseBody;
22+
}
23+
1724
using (HttpClient httpClient = new HttpClient())
1825
{
1926
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
@@ -24,6 +31,9 @@ public static async Task<string> CallApiManagementAsync(string azToken, string r
2431

2532
response.EnsureSuccessStatusCode();
2633
string responseBody = await response.Content.ReadAsStringAsync();
34+
35+
_cache.Set(requestUrl, responseBody);
36+
2737
return responseBody;
2838
}
2939
}
@@ -58,4 +68,4 @@ public Template GenerateEmptyTemplateWithParameters(string policyXMLBaseUrl, str
5868
return armTemplate;
5969
}
6070
}
61-
}
71+
}

src/APIM_ARMTemplate/apimtemplate/apimtemplate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Include="Microsoft.Azure.Management.ApiManagement" Version="4.0.6-preview" />
2525
<PackageReference Include="Microsoft.Azure.Management.Authorization" Version="2.9.0-preview" />
2626
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.18.0" />
27+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.1" />
2728
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
2829
<PackageReference Include="Colors.Net" Version="1.1.0" />
2930
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />

0 commit comments

Comments
 (0)