Skip to content

Commit 4d57060

Browse files
authored
Merge pull request #73 from rubberduck-vba/webhook
Add ClearCache authenticated endpoint
2 parents fd697f5 + 8e2a606 commit 4d57060

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

rubberduckvba.Server/Api/Admin/AdminController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using Microsoft.AspNetCore.Cors;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Options;
5+
using rubberduckvba.Server.Services;
56

67
namespace rubberduckvba.Server.Api.Admin;
78

89

910
[ApiController]
10-
public class AdminController(ConfigurationOptions options, HangfireLauncherService hangfire) : ControllerBase
11+
public class AdminController(ConfigurationOptions options, HangfireLauncherService hangfire, CacheService cache) : ControllerBase
1112
{
1213
/// <summary>
1314
/// Enqueues a job that updates xmldoc content from the latest release/pre-release tags.
@@ -35,6 +36,15 @@ public IActionResult UpdateTagMetadata()
3536
return Ok(jobId);
3637
}
3738

39+
[Authorize("github")]
40+
[EnableCors("CorsPolicy")]
41+
[HttpPost("admin/cache/clear")]
42+
public IActionResult ClearCache()
43+
{
44+
cache.Clear();
45+
return Ok();
46+
}
47+
3848
#if DEBUG
3949
[HttpGet("admin/config/current")]
4050
public IActionResult Config()

rubberduckvba.Server/Services/CacheService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ public void Invalidate(AnnotationsFeatureViewModel newContent)
150150
}
151151
}
152152

153+
public void Clear()
154+
{
155+
_cache.Clear();
156+
_logger.LogInformation("Cache was cleared.");
157+
}
158+
153159
private bool IsTagsCacheValid() => IsCacheValid(TagsJobState, () => TagsJobState);
154160

155161
private bool IsXmldocCacheValid() => IsCacheValid(XmldocJobState, () => XmldocJobState);

rubberduckvba.client/src/app/components/auth-menu/auth-menu.component.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
Update xmldoc metadata
2727
</button>
2828
</div>
29+
<hr />
30+
<div ngbDropdownItem>
31+
<button type="button" role="button" class="nav-link text-dark btn-link" title="" (click)="confirmClearCache()">
32+
Clear cache
33+
</button>
34+
</div>
2935
</div>
3036
</div>
3137
</div>
@@ -100,3 +106,26 @@ <h6><fa-icon class="mx-1" [icon]="['fas', 'info-circle']"></fa-icon>Confirm</h6>
100106
</div>
101107
</div>
102108
</ng-template>
109+
110+
<ng-template #confirmclearcachebox let-modal>
111+
<div class="modal-dialog modal-lg">
112+
<div class="modal-content">
113+
<div class="modal-header">
114+
<h5>Clear Cache</h5>
115+
</div>
116+
<div class="modal-body">
117+
<div class="border-primary border-bottom border-top bg-light text-primary p-2 my-1">
118+
<h6><fa-icon class="mx-1" [icon]="['fas', 'info-circle']"></fa-icon>Confirm</h6>
119+
<p>This will clear the memory cache, forcing the next request to fetch from the backend database.</p>
120+
</div>
121+
<p class="mt-4">
122+
Proceed?
123+
</p>
124+
</div>
125+
<div class="modal-footer">
126+
<button id="cancel-xmldoc" type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close" (click)="modal.dismiss('cancel')">Close</button>
127+
<button id="accept-xmldoc" type="button" class="btn btn-primary" (click)="clearCache()">Proceed</button>
128+
</div>
129+
</div>
130+
</div>
131+
</ng-template>

rubberduckvba.client/src/app/components/auth-menu/auth-menu.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class AuthMenuComponent implements OnInit {
2626
@ViewChild('confirmbox', { read: TemplateRef }) confirmbox: TemplateRef<any> | undefined;
2727
@ViewChild('confirmtagsbox', { read: TemplateRef }) confirmtagsbox: TemplateRef<any> | undefined;
2828
@ViewChild('confirmxmldocsbox', { read: TemplateRef }) confirmxmldocsbox: TemplateRef<any> | undefined;
29+
@ViewChild('confirmclearcachebox', { read: TemplateRef }) confirmclearcachebox: TemplateRef<any> | undefined;
2930
public modal = inject(NgbModal);
3031

3132
constructor(private auth: AuthService, private api: ApiClientService, private fa: FaIconLibrary) {
@@ -57,6 +58,10 @@ export class AuthMenuComponent implements OnInit {
5758
this.modal.open(this.confirmxmldocsbox);
5859
}
5960

61+
public confirmClearCache(): void {
62+
this.modal.open(this.confirmclearcachebox);
63+
}
64+
6065
public signin(): void {
6166
this.auth.signin();
6267
this.getUserInfo();
@@ -76,4 +81,9 @@ export class AuthMenuComponent implements OnInit {
7681
this.modal.dismissAll();
7782
this.api.updateXmldocMetadata().subscribe(jobId => console.log(`UpdateXmldocMetadata has scheduled job id ${jobId}`));
7883
}
84+
85+
public clearCache(): void {
86+
this.modal.dismissAll();
87+
this.api.clearCache().subscribe(() => console.log(`Cache has been cleared`));
88+
}
7989
}

rubberduckvba.client/src/app/services/api-client.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export class ApiClientService {
6565
return this.data.postAsync(url);
6666
}
6767

68+
public clearCache(): Observable<any> {
69+
const url = `${environment.apiBaseUrl}admin/cache/clear`;
70+
return this.data.postAsync(url);
71+
}
72+
6873
public getIndenterDefaults(): Observable<IndenterViewModel> {
6974
const url = `${environment.apiBaseUrl}indenter/defaults`;
7075
return this.data.getAsync<IndenterViewModel>(url).pipe(map(model => new IndenterViewModelClass(model)));

0 commit comments

Comments
 (0)