Skip to content

Add ClearCache authenticated endpoint #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion rubberduckvba.Server/Api/Admin/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using rubberduckvba.Server.Services;

namespace rubberduckvba.Server.Api.Admin;


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

[Authorize("github")]
[EnableCors("CorsPolicy")]
[HttpPost("admin/cache/clear")]
public IActionResult ClearCache()
{
cache.Clear();
return Ok();
}

#if DEBUG
[HttpGet("admin/config/current")]
public IActionResult Config()
Expand Down
6 changes: 6 additions & 0 deletions rubberduckvba.Server/Services/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ public void Invalidate(AnnotationsFeatureViewModel newContent)
}
}

public void Clear()
{
_cache.Clear();
_logger.LogInformation("Cache was cleared.");
}

private bool IsTagsCacheValid() => IsCacheValid(TagsJobState, () => TagsJobState);

private bool IsXmldocCacheValid() => IsCacheValid(XmldocJobState, () => XmldocJobState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
Update xmldoc metadata
</button>
</div>
<hr />
<div ngbDropdownItem>
<button type="button" role="button" class="nav-link text-dark btn-link" title="" (click)="confirmClearCache()">
Clear cache
</button>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -100,3 +106,26 @@ <h6><fa-icon class="mx-1" [icon]="['fas', 'info-circle']"></fa-icon>Confirm</h6>
</div>
</div>
</ng-template>

<ng-template #confirmclearcachebox let-modal>
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5>Clear Cache</h5>
</div>
<div class="modal-body">
<div class="border-primary border-bottom border-top bg-light text-primary p-2 my-1">
<h6><fa-icon class="mx-1" [icon]="['fas', 'info-circle']"></fa-icon>Confirm</h6>
<p>This will clear the memory cache, forcing the next request to fetch from the backend database.</p>
</div>
<p class="mt-4">
Proceed?
</p>
</div>
<div class="modal-footer">
<button id="cancel-xmldoc" type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close" (click)="modal.dismiss('cancel')">Close</button>
<button id="accept-xmldoc" type="button" class="btn btn-primary" (click)="clearCache()">Proceed</button>
</div>
</div>
</div>
</ng-template>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class AuthMenuComponent implements OnInit {
@ViewChild('confirmbox', { read: TemplateRef }) confirmbox: TemplateRef<any> | undefined;
@ViewChild('confirmtagsbox', { read: TemplateRef }) confirmtagsbox: TemplateRef<any> | undefined;
@ViewChild('confirmxmldocsbox', { read: TemplateRef }) confirmxmldocsbox: TemplateRef<any> | undefined;
@ViewChild('confirmclearcachebox', { read: TemplateRef }) confirmclearcachebox: TemplateRef<any> | undefined;
public modal = inject(NgbModal);

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

public confirmClearCache(): void {
this.modal.open(this.confirmclearcachebox);
}

public signin(): void {
this.auth.signin();
this.getUserInfo();
Expand All @@ -76,4 +81,9 @@ export class AuthMenuComponent implements OnInit {
this.modal.dismissAll();
this.api.updateXmldocMetadata().subscribe(jobId => console.log(`UpdateXmldocMetadata has scheduled job id ${jobId}`));
}

public clearCache(): void {
this.modal.dismissAll();
this.api.clearCache().subscribe(() => console.log(`Cache has been cleared`));
}
}
5 changes: 5 additions & 0 deletions rubberduckvba.client/src/app/services/api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export class ApiClientService {
return this.data.postAsync(url);
}

public clearCache(): Observable<any> {
const url = `${environment.apiBaseUrl}admin/cache/clear`;
return this.data.postAsync(url);
}

public getIndenterDefaults(): Observable<IndenterViewModel> {
const url = `${environment.apiBaseUrl}indenter/defaults`;
return this.data.getAsync<IndenterViewModel>(url).pipe(map(model => new IndenterViewModelClass(model)));
Expand Down