Files
WebServer.Test/WebServer/Utils/CachedResponse.cs
Kaveman b21a1caabc Test Project Created
+ Implemented Request Processing (Cache => Regex Events => Razor Pages => Static Content)
 + Rendering based on domain works
 - Areas are not implemented
 + Content Type Map now defines `.cshtml` files and default mime type was changed to "text/plain"
 + GetStaticFile no longer returns private templates/layouts (used to be able to view those in plain text by requesting with a fully qualified path)
I will make more concise tests in a bit (I was not pushing to git properly, so have to make big commits for the time being)
2024-08-15 01:10:06 -07:00

49 lines
1.9 KiB
C#

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace WebServer.Utils {
public class CachedResponse : HttpResponse {
#region Static Methods, etc
public static bool BypassCache = false;
public static List<CachedResponse> Instances { get; private set; } = new List<CachedResponse>();
public static ushort RaiseAllUpdateFlags() {
ushort flagsRaised = 0;
foreach (CachedResponse resource in Instances) {
if (resource.NeedsUpdate) continue;
flagsRaised++;
resource.RaiseUpdateFlag();
//Logger.LogDebug($"Raised Update Flag for '{resource.Name}'");
}
return flagsRaised;
}
public static CachedResponse? Get(HttpServer server, string path) {
path = path.Trim();
return Instances.FirstOrDefault(x => x.Server == server && x.Path == path);
}
public static bool TryGet(HttpServer server, string path, out CachedResponse? resource)
=> (resource = Get(server, path)) != null;
#endregion
public readonly HttpServer Server;
public string Path;
bool UpdateFlagRaised;
Stopwatch TimeSinceLastUpdate;
public HttpServer.Callback? UpdateMethod; // If null, HttpServer will attempt to find one or GetStaticFile
public bool NeedsUpdate => UpdateFlagRaised || BypassCache || Server.Config.MaxCacheAge < TimeSinceLastUpdate.Elapsed.TotalSeconds;
public CachedResponse(HttpServer parentServer, HttpServer.Callback? updateMethod) {
Instances.Add(this);
Server = parentServer;
TimeSinceLastUpdate = Stopwatch.StartNew();
UpdateMethod = updateMethod;
}
public void RaiseUpdateFlag() => UpdateFlagRaised = true;
public void ClearFlag() => UpdateFlagRaised = false;
}
}