diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 01:49:10 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 01:49:10 +0200 |
commit | fc749b3e57098740377e6eabd5d010d133256fa5 (patch) | |
tree | cc97267a3d4222c910769e46bdb37c96c7c31531 /MatrixRoomUtils.Core/RuntimeCache.cs | |
parent | unknown changes (diff) | |
download | MatrixUtils-fc749b3e57098740377e6eabd5d010d133256fa5.tar.xz |
Improved many features
Diffstat (limited to 'MatrixRoomUtils.Core/RuntimeCache.cs')
-rw-r--r-- | MatrixRoomUtils.Core/RuntimeCache.cs | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/MatrixRoomUtils.Core/RuntimeCache.cs b/MatrixRoomUtils.Core/RuntimeCache.cs index 25b3682..4f73341 100644 --- a/MatrixRoomUtils.Core/RuntimeCache.cs +++ b/MatrixRoomUtils.Core/RuntimeCache.cs @@ -14,38 +14,41 @@ public class RuntimeCache // public static Dictionary<string, (DateTime cachedAt, ProfileResponse response)> ProfileCache { get; set; } = new(); public static Dictionary<string, ObjectCache<object>> GenericResponseCache { get; set; } = new(); - - public static Action Save { get; set; } = () => - { - Console.WriteLine("RuntimeCache.Save() was called, but no callback was set!"); - }; - public static Action<string, object> SaveObject { get; set; } = (key, value) => - { - Console.WriteLine($"RuntimeCache.SaveObject({key}, {value}) was called, but no callback was set!"); - }; + + public static Action Save { get; set; } = () => { Console.WriteLine("RuntimeCache.Save() was called, but no callback was set!"); }; + public static Action<string, object> SaveObject { get; set; } = (key, value) => { Console.WriteLine($"RuntimeCache.SaveObject({key}, {value}) was called, but no callback was set!"); }; + public static Action<string> RemoveObject { get; set; } = key => { Console.WriteLine($"RuntimeCache.RemoveObject({key}) was called, but no callback was set!"); }; static RuntimeCache() { Task.Run(async () => { - while (true) + while(true) { await Task.Delay(1000); - foreach (var (key, value) in RuntimeCache.GenericResponseCache) + foreach (var (key, value) in GenericResponseCache) { - SaveObject("rory.matrixroomutils.generic_cache:" + key, value); + if (value.Cache.Any()) + SaveObject("rory.matrixroomutils.generic_cache:" + key, value); + else + { + RemoveObject("rory.matrixroomutils.generic_cache:" + key); + } } } }); } } - public class UserInfo { public ProfileResponse Profile { get; set; } = new(); public LoginResponse LoginResponse { get; set; } - public string AccessToken { get => LoginResponse.AccessToken; } + + public string AccessToken + { + get => LoginResponse.AccessToken; + } } public class HomeServerResolutionResult @@ -53,10 +56,12 @@ public class HomeServerResolutionResult public string Result { get; set; } public DateTime ResolutionTime { get; set; } } + public class ObjectCache<T> where T : class { public Dictionary<string, GenericResult<T>> Cache { get; set; } = new(); public string Name { get; set; } = null!; + public GenericResult<T> this[string key] { get @@ -65,11 +70,11 @@ public class ObjectCache<T> where T : class { // Console.WriteLine($"cache.get({key}): hit"); // Console.WriteLine($"Found item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}"); - if(Cache[key].ExpiryTime < DateTime.Now) + if (Cache[key].ExpiryTime < DateTime.Now) Console.WriteLine($"WARNING: item {key} in cache {Name} expired at {Cache[key].ExpiryTime}:\n{Cache[key].Result.ToJson(indent: false)}"); return Cache[key]; - } + Console.WriteLine($"cache.get({key}): miss"); return null; } @@ -82,19 +87,19 @@ public class ObjectCache<T> where T : class // Console.Error.WriteLine("Full cache: " + Cache.ToJson()); } } - + public ObjectCache() { //expiry timer Task.Run(async () => { - while (true) + while (Cache.Any()) { await Task.Delay(1000); foreach (var x in Cache.Where(x => x.Value.ExpiryTime < DateTime.Now).OrderBy(x => x.Value.ExpiryTime).Take(15).ToList()) { // Console.WriteLine($"Removing {x.Key} from cache"); - Cache.Remove(x.Key); + Cache.Remove(x.Key); } //RuntimeCache.SaveObject("rory.matrixroomutils.generic_cache:" + Name, this); } @@ -103,19 +108,20 @@ public class ObjectCache<T> where T : class public bool ContainsKey(string key) => Cache.ContainsKey(key); } + public class GenericResult<T> { public T? Result { get; set; } public DateTime? ExpiryTime { get; set; } = DateTime.Now; - + public GenericResult() { //expiry timer - } + public GenericResult(T? result, DateTime? expiryTime = null) : this() { Result = result; ExpiryTime = expiryTime; } -} +} \ No newline at end of file |