File tree Expand file tree Collapse file tree
DfE.ExternalApplications.Infrastructure
DfE.ExternalApplications.Web Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff line change 1- using DfE . ExternalApplications . Application . Interfaces ;
1+ using DfE . CoreLibs . Caching . Helpers ;
2+ using DfE . CoreLibs . Caching . Interfaces ;
3+ using DfE . ExternalApplications . Application . Interfaces ;
24using DfE . ExternalApplications . Domain . Models ;
35using System . Diagnostics . CodeAnalysis ;
46
57namespace 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}
Original file line number Diff line number Diff line change 11using DfE . ExternalApplications . Application . Interfaces ;
2- using System . Text ;
32using GovUK . Dfe . ExternalApplications . Api . Client . Contracts ;
43using System . Diagnostics . CodeAnalysis ;
4+ using System . Text ;
55
66namespace 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}
Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff line change 7272builder . Services . AddScoped < IApplicationResponseService , ApplicationResponseService > ( ) ;
7373builder . 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));
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments