Преглед изворни кода

Add a way of resetting the cache

Robert Marshall пре 3 година
родитељ
комит
deb3811f7a

+ 14 - 1
src/Website/Controllers/Admin.cs

@@ -1,5 +1,6 @@
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Caching.Memory;
 using System.Threading.Tasks;
 using Website.Data;
 using Website.Models.Mailboxes;
@@ -9,10 +10,12 @@ namespace Website.Controllers {
 	public class Admin : Controller {
 		private readonly IApiKeyManager _apiKeyManager;
 		private readonly IMailboxesApi _mailboxesApi;
+		private readonly IMemoryCache _cache;
 
-		public Admin(IApiKeyManager apiKeyManager, IMailboxesApi mailboxesApi) {
+		public Admin(IApiKeyManager apiKeyManager, IMailboxesApi mailboxesApi, IMemoryCache cache) {
 			_apiKeyManager = apiKeyManager;
 			_mailboxesApi = mailboxesApi;
+			_cache = cache;
 		}
 
 		public async Task<IActionResult> ApiKeys() {
@@ -65,5 +68,15 @@ namespace Website.Controllers {
 			await _mailboxesApi.CreateAlias(alias);
 			return RedirectToAction(nameof(Mailboxes));
 		}
+
+		[HttpGet]
+		public async Task<IActionResult> Cache() => View(_cache);
+
+		[HttpPost]
+		public async Task<IActionResult> Cache(bool reset) {
+			if (reset && _cache is MemoryCache cache) 
+				cache.Compact(1.0);
+			return RedirectToAction(nameof(Cache));
+		}
 	}
 }

+ 3 - 0
src/Website/Views/Account/Index.cshtml

@@ -8,6 +8,9 @@
 <a asp-action="Logout">Logout</a>
 
 <h2>Admin</h2>
+<div>
+	<a asp-controller="Admin" asp-action="Cache">Manage Cache</a>
+</div>
 <div>
 	<a asp-controller="Admin" asp-action="ApiKeys">Manage API Keys</a>
 </div>

+ 14 - 0
src/Website/Views/Admin/Cache.cshtml

@@ -0,0 +1,14 @@
+@using Microsoft.Extensions.Caching.Memory
+@model Microsoft.Extensions.Caching.Memory.IMemoryCache
+
+@{
+	ViewBag.Title = "Cache Management";
+	Layout = "_Layout";
+
+	var cacheCount = Model is MemoryCache cache ? cache.Count : 0;
+}
+
+<p>Items: @cacheCount</p>
+<form method="post" asp-action="Cache">
+	<button type="submit" name="reset" value="true">Reset</button>
+</form>

+ 3 - 0
src/Website/Views/Admin/Index.cshtml

@@ -4,6 +4,9 @@
 }
 
 <h2>Admin Panel</h2>
+<div>
+	<a asp-controller="Admin" asp-action="Cache">Manage Cache</a>
+</div>
 <div>
 	<a asp-controller="Admin" asp-action="ApiKeys">Manage API Keys</a>
 </div>