about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/RuntimeCache.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
commit712ad189c99570f686ab779782b2a873e172428e (patch)
tree6102e4719416e71522e9143fa4e06951258bd77c /MatrixRoomUtils.Core/RuntimeCache.cs
parentFix passwords being visible during editing (diff)
downloadMatrixUtils-712ad189c99570f686ab779782b2a873e172428e.tar.xz
Change syntax style
Diffstat (limited to 'MatrixRoomUtils.Core/RuntimeCache.cs')
-rw-r--r--MatrixRoomUtils.Core/RuntimeCache.cs119
1 files changed, 45 insertions, 74 deletions
diff --git a/MatrixRoomUtils.Core/RuntimeCache.cs b/MatrixRoomUtils.Core/RuntimeCache.cs
index 4f73341..a2fcf40 100644
--- a/MatrixRoomUtils.Core/RuntimeCache.cs
+++ b/MatrixRoomUtils.Core/RuntimeCache.cs
@@ -3,9 +3,21 @@ using MatrixRoomUtils.Core.Responses;
 
 namespace MatrixRoomUtils.Core;
 
-public class RuntimeCache
-{
+public class RuntimeCache {
     public static bool WasLoaded = false;
+
+    static RuntimeCache() =>
+        Task.Run(async () => {
+            while (true) {
+                await Task.Delay(1000);
+                foreach (var (key, value) in GenericResponseCache)
+                    if (value.Cache.Any())
+                        SaveObject("rory.matrixroomutils.generic_cache:" + key, value);
+                    else
+                        RemoveObject("rory.matrixroomutils.generic_cache:" + key);
+            }
+        });
+
     public static string? LastUsedToken { get; set; }
     public static AuthenticatedHomeServer CurrentHomeServer { get; set; }
     public static Dictionary<string, UserInfo> LoginSessions { get; set; } = new();
@@ -18,110 +30,69 @@ public class RuntimeCache
     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)
-            {
-                await Task.Delay(1000);
-                foreach (var (key, value) in GenericResponseCache)
-                {
-                    if (value.Cache.Any())
-                        SaveObject("rory.matrixroomutils.generic_cache:" + key, value);
-                    else
-                    {
-                        RemoveObject("rory.matrixroomutils.generic_cache:" + key);
-                    }
-                }
-            }
-        });
-    }
 }
 
-public class UserInfo
-{
+public class UserInfo {
     public ProfileResponse Profile { get; set; } = new();
     public LoginResponse LoginResponse { get; set; }
 
-    public string AccessToken
-    {
-        get => LoginResponse.AccessToken;
-    }
+    public string AccessToken => LoginResponse.AccessToken;
 }
 
-public class HomeServerResolutionResult
-{
+public class HomeServerResolutionResult {
     public string Result { get; set; }
     public DateTime ResolutionTime { get; set; }
 }
 
-public class ObjectCache<T> where T : class
-{
+public class ObjectCache<T> where T : class {
+    public ObjectCache() =>
+        //expiry timer
+        Task.Run(async () => {
+            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);
+                //RuntimeCache.SaveObject("rory.matrixroomutils.generic_cache:" + Name, this);
+            }
+        });
+
     public Dictionary<string, GenericResult<T>> Cache { get; set; } = new();
     public string Name { get; set; } = null!;
 
-    public GenericResult<T> this[string key]
-    {
-        get
-        {
-            if (Cache.ContainsKey(key))
-            {
+    public GenericResult<T> this[string key] {
+        get {
+            if (Cache.ContainsKey(key)) {
                 // 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)
-                    Console.WriteLine($"WARNING: item {key} in cache {Name} expired at {Cache[key].ExpiryTime}:\n{Cache[key].Result.ToJson(indent: false)}");
+                    Console.WriteLine($"WARNING: item {key} in cache {Name} expired at {Cache[key].ExpiryTime}:\n{Cache[key].Result.ToJson(false)}");
                 return Cache[key];
             }
 
             Console.WriteLine($"cache.get({key}): miss");
             return null;
         }
-        set
-        {
-            Cache[key] = value;
-            // Console.WriteLine($"set({key}) = {Cache[key].Result.ToJson(indent:false)}");
-            // Console.WriteLine($"new_state: {this.ToJson(indent:false)}");
-            // Console.WriteLine($"New item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}");
-            // Console.Error.WriteLine("Full cache: " + Cache.ToJson());
-        }
-    }
-
-    public ObjectCache()
-    {
-        //expiry timer
-        Task.Run(async () =>
-        {
-            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);
-                }
-                //RuntimeCache.SaveObject("rory.matrixroomutils.generic_cache:" + Name, this);
-            }
-        });
+        set => Cache[key] = value;
+        // Console.WriteLine($"set({key}) = {Cache[key].Result.ToJson(indent:false)}");
+        // Console.WriteLine($"new_state: {this.ToJson(indent:false)}");
+        // Console.WriteLine($"New item in cache: {key} - {Cache[key].Result.ToJson(indent: false)}");
+        // Console.Error.WriteLine("Full cache: " + Cache.ToJson());
     }
 
     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()
-    {
+public class GenericResult<T> {
+    public GenericResult() {
         //expiry timer
     }
 
-    public GenericResult(T? result, DateTime? expiryTime = null) : this()
-    {
+    public GenericResult(T? result, DateTime? expiryTime = null) : this() {
         Result = result;
         ExpiryTime = expiryTime;
     }
+
+    public T? Result { get; set; }
+    public DateTime? ExpiryTime { get; set; } = DateTime.Now;
 }
\ No newline at end of file