diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-11 20:56:16 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-11 20:56:16 +0200 |
commit | a069cfd6a0f7c53b902607e79037ffd90681a7b9 (patch) | |
tree | 0cfc47619d35e2d89568f5d1c151ba8e1906ec85 /MatrixRoomUtils.Core/RuntimeCache.cs | |
parent | Add license, deploy src (diff) | |
download | MatrixUtils-a069cfd6a0f7c53b902607e79037ffd90681a7b9.tar.xz |
Add state cache
Diffstat (limited to 'MatrixRoomUtils.Core/RuntimeCache.cs')
-rw-r--r-- | MatrixRoomUtils.Core/RuntimeCache.cs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/RuntimeCache.cs b/MatrixRoomUtils.Core/RuntimeCache.cs index 586cf88..affbd94 100644 --- a/MatrixRoomUtils.Core/RuntimeCache.cs +++ b/MatrixRoomUtils.Core/RuntimeCache.cs @@ -1,3 +1,7 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.JavaScript; +using System.Xml.Schema; +using MatrixRoomUtils.Core.Extensions; using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Core; @@ -11,6 +15,8 @@ public class RuntimeCache public static Dictionary<string, HomeServerResolutionResult> HomeserverResolutionCache { get; set; } = new(); // public static Dictionary<string, (DateTime cachedAt, ProfileResponse response)> ProfileCache { get; set; } = new(); + + public static Dictionary<string, ObjectCache<object>> GenericResponseCache { get; set; } = new(); } @@ -26,3 +32,54 @@ 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 TimeSpan DefaultExpiry { get; set; } = new(0, 5, 0); + public GenericResult<T> this[string key] + { + get + { + if (Random.Shared.Next(100) == 1) + { + // Console.WriteLine("Cleaning cache..."); + // foreach (var x in Cache.Where(x => x.Value.ExpiryTime < DateTime.Now).OrderBy(x => x.Value.ExpiryTime).Take(3).ToList()) + // { + // Console.WriteLine($"Removing {x.Key} from cache"); + // Cache.Remove(x.Key); + // } + } + + + if (Cache.ContainsKey(key)) + { + // Console.WriteLine($"Found item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}"); + if(Cache[key].ExpiryTime > DateTime.Now) + return Cache[key]; + + Console.WriteLine($"Expired item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}"); + try + { + Cache.Remove(key); + } + catch (Exception e) + { + Console.WriteLine($"Failed to remove {key} from cache: {e.Message}"); + } + } + return null; + } + set + { + Cache[key] = value; + if(Cache[key].ExpiryTime == null) Cache[key].ExpiryTime = DateTime.Now.Add(DefaultExpiry); + Console.WriteLine($"New item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}"); + // Console.Error.WriteLine("Full cache: " + Cache.ToJson()); + } + } +} +public class GenericResult<T> +{ + public T? Result { get; set; } + public DateTime? ExpiryTime { get; set; } +} |