Skip to content

Commit 31aab07

Browse files
FrostyApeOneFrostyApeOne
authored andcommitted
2 parents fdb5633 + 41d4162 commit 31aab07

6 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/DfE.ExternalApplications.Infrastructure/DfE.ExternalApplications.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="DfE.CoreLibs.Caching" Version="1.0.11-prerelease-77" />
1011
<PackageReference Include="DfE.CoreLibs.Contracts" Version="1.0.24" />
1112
<PackageReference Include="GovUK.Dfe.ExternalApplications.Api.Client" Version="0.1.3" />
1213
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.3.0" />
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
using DfE.ExternalApplications.Application.Interfaces;
1+
using DfE.CoreLibs.Caching.Helpers;
2+
using DfE.CoreLibs.Caching.Interfaces;
3+
using DfE.ExternalApplications.Application.Interfaces;
24
using DfE.ExternalApplications.Domain.Models;
35
using System.Diagnostics.CodeAnalysis;
46

57
namespace DfE.ExternalApplications.Infrastructure.Providers;
68

7-
public class FormTemplateProvider(ITemplateStore store, IFormTemplateParser parser) : IFormTemplateProvider
9+
public class FormTemplateProvider(
10+
ITemplateStore store,
11+
IFormTemplateParser parser,
12+
ICacheService<IMemoryCacheType> cacheService) : IFormTemplateProvider
813
{
914
[ExcludeFromCodeCoverage]
1015
public async Task<FormTemplate> GetTemplateAsync(string templateId, CancellationToken cancellationToken = default)
1116
{
12-
using var stream = await store.GetTemplateStreamAsync(templateId, cancellationToken);
13-
return await parser.ParseAsync(stream, cancellationToken);
17+
var cacheKey = $"FormTemplate_{CacheKeyHelper.GenerateHashedCacheKey(templateId)}";
18+
var methodName = nameof(GetTemplateAsync);
19+
20+
return await cacheService.GetOrAddAsync(
21+
cacheKey,
22+
async () =>
23+
{
24+
using var stream = await store.GetTemplateStreamAsync(templateId, cancellationToken);
25+
return await parser.ParseAsync(stream, cancellationToken);
26+
},
27+
methodName);
1428
}
1529
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using DfE.ExternalApplications.Application.Interfaces;
2-
using System.Text;
32
using GovUK.Dfe.ExternalApplications.Api.Client.Contracts;
43
using System.Diagnostics.CodeAnalysis;
4+
using System.Text;
55

66
namespace DfE.ExternalApplications.Infrastructure.Stores;
77

@@ -10,13 +10,8 @@ public class ApiTemplateStore(ITemplatesClient templateClient) : ITemplateStore
1010
[ExcludeFromCodeCoverage]
1111
public async Task<Stream> GetTemplateStreamAsync(string templateId, CancellationToken cancellationToken = default)
1212
{
13-
// TODO Implement caching
1413
var response = await templateClient.GetLatestTemplateSchemaAsync(new Guid(templateId), cancellationToken);
15-
1614
var utf8 = Encoding.UTF8.GetBytes(response.JsonSchema);
17-
18-
var stream = new MemoryStream(utf8);
19-
20-
return stream;
15+
return new MemoryStream(utf8);
2116
}
2217
}

src/DfE.ExternalApplications.Web/DfE.ExternalApplications.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="DfE.CoreLibs.Caching" Version="1.0.11-prerelease-77" />
1112
<PackageReference Include="GovUK.Dfe.ExternalApplications.Api.Client" Version="0.1.3" />
1213
<PackageReference Include="GovUk.Frontend.AspNetCore" Version="2.8.1" />
1314
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

src/DfE.ExternalApplications.Web/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
builder.Services.AddScoped<IApplicationResponseService, ApplicationResponseService>();
7373
builder.Services.AddSingleton<ITemplateStore, ApiTemplateStore>();
7474

75+
builder.Services.AddServiceCaching(configuration);
76+
7577
//var templatesPath = Path.Combine(builder.Environment.ContentRootPath, "templates");
7678
//builder.Services.AddSingleton<ITemplateStore>(
7779
// new JsonFileTemplateStore(templatesPath));

src/DfE.ExternalApplications.Web/appsettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@
3333
"transfer": "9A4E9C58-9135-468C-B154-7B966F7ACFB7",
3434
"sigchange": "form-sigchange-guid"
3535
}
36+
},
37+
"CacheSettings": {
38+
"Memory": {
39+
"DefaultDurationInSeconds": 60,
40+
"Durations": {
41+
"GetTemplateAsync": 86400
42+
}
43+
}
3644
}
3745
}

0 commit comments

Comments
 (0)