Skip to content

Commit ca548a7

Browse files
FrostyApeOneFrostyApeOne
authored andcommitted
Fixed Template cache invalidation issue
1 parent 0e798ee commit ca548a7

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/DfE.ExternalApplications.Web/Pages/RenderForm.cshtml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public async Task<IActionResult> OnGetAutocompleteAsync(string endpoint, string
170170

171171
private async Task LoadTemplateAsync()
172172
{
173+
_logger.LogDebug("Loading template {TemplateId} for RenderForm", TemplateId);
173174
Template = await _templateProvider.GetTemplateAsync(TemplateId);
174175
}
175176

src/DfE.ExternalApplications.Web/Pages/TemplateManager.cshtml.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ public async Task<IActionResult> OnPostAsync()
9191
try
9292
{
9393
await CreateNewTemplateVersionAsync(templateId);
94-
InvalidateTemplateCache(templateId);
94+
95+
await Task.Delay(2000);
96+
97+
await InvalidateTemplateCacheAsync(templateId);
9598

9699
_logger.LogInformation("Successfully created template version {NewVersion} for {TemplateId}",
97100
NewVersion, templateId);
@@ -121,14 +124,22 @@ private async Task LoadTemplateDataAsync(string templateId)
121124
{
122125
try
123126
{
127+
_logger.LogDebug("Loading template data for {TemplateId}", templateId);
128+
124129
var apiResponse = await _templatesClient.GetLatestTemplateSchemaAsync(new Guid(templateId));
125130
CurrentVersionNumber = apiResponse.VersionNumber;
126131

132+
_logger.LogDebug("API returned template version {VersionNumber} for {TemplateId}",
133+
CurrentVersionNumber, templateId);
134+
127135
CurrentTemplate = await _formTemplateProvider.GetTemplateAsync(templateId);
128136
if (CurrentTemplate != null)
129137
{
130138
var options = new JsonSerializerOptions { WriteIndented = true };
131139
CurrentTemplateJson = JsonSerializer.Serialize(CurrentTemplate, options);
140+
141+
_logger.LogDebug("Successfully loaded template {TemplateId} with {TaskGroupCount} task groups",
142+
templateId, CurrentTemplate.TaskGroups?.Count ?? 0);
132143
}
133144
}
134145
catch (Exception ex)
@@ -183,20 +194,41 @@ await _templatesClient.CreateTemplateVersionAsync(new Guid(templateId),
183194
new CreateTemplateVersionRequest(VersionNumber: NewVersion!, JsonSchema: base64Schema));
184195
}
185196

186-
private void InvalidateTemplateCache(string templateId)
197+
private async Task InvalidateTemplateCacheAsync(string templateId)
187198
{
188199
try
189200
{
190201
var cacheKey = $"FormTemplate_{CacheKeyHelper.GenerateHashedCacheKey(templateId)}";
202+
_logger.LogInformation("Attempting to invalidate cache for template {TemplateId} with key {CacheKey}",
203+
templateId, cacheKey);
204+
191205
_cacheService.Remove(cacheKey);
192-
_logger.LogDebug("Invalidated cache for template {TemplateId} with key {CacheKey}", templateId, cacheKey);
206+
_logger.LogInformation("Successfully invalidated cache for template {TemplateId} with key {CacheKey}",
207+
templateId, cacheKey);
208+
209+
// Verify the new template version is available by attempting to load it
210+
await VerifyNewTemplateVersionAsync(templateId);
193211
}
194212
catch (Exception ex)
195213
{
196214
_logger.LogWarning(ex, "Failed to invalidate cache for template {TemplateId}", templateId);
197215
// Don't throw - cache invalidation failure shouldn't break the operation
198216
}
199217
}
218+
219+
private async Task VerifyNewTemplateVersionAsync(string templateId)
220+
{
221+
try
222+
{
223+
// Try to load the new template version to ensure it's available
224+
var newTemplate = await _formTemplateProvider.GetTemplateAsync(templateId);
225+
_logger.LogDebug("Successfully verified new template version is available for {TemplateId}", templateId);
226+
}
227+
catch (Exception ex)
228+
{
229+
_logger.LogWarning(ex, "Failed to verify new template version for {TemplateId}", templateId);
230+
}
231+
}
200232

201233
private void HandleApiError(Exception exception)
202234
{

src/DfE.ExternalApplications.Web/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
5+
"Microsoft.AspNetCore": "Warning",
6+
"DfE.ExternalApplications": "Debug"
67
}
78
},
89
"AllowedHosts": "*",

0 commit comments

Comments
 (0)