about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/RuntimeCache.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-23 08:51:02 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-23 08:51:02 +0200
commit3ef3e5caa65458e595e2303358322626c7da1dda (patch)
tree6a60d401da8b5f906a423601ac4c135d44e896d1 /MatrixRoomUtils.Core/RuntimeCache.cs
parentLocal changes (diff)
downloadMatrixUtils-3ef3e5caa65458e595e2303358322626c7da1dda.tar.xz
Add numerous new things
Diffstat (limited to 'MatrixRoomUtils.Core/RuntimeCache.cs')
-rw-r--r--MatrixRoomUtils.Core/RuntimeCache.cs45
1 files changed, 30 insertions, 15 deletions
diff --git a/MatrixRoomUtils.Core/RuntimeCache.cs b/MatrixRoomUtils.Core/RuntimeCache.cs
index affbd94..4a96f50 100644
--- a/MatrixRoomUtils.Core/RuntimeCache.cs
+++ b/MatrixRoomUtils.Core/RuntimeCache.cs
@@ -1,6 +1,3 @@
-using System.Runtime.InteropServices;
-using System.Runtime.InteropServices.JavaScript;
-using System.Xml.Schema;
 using MatrixRoomUtils.Core.Extensions;
 using MatrixRoomUtils.Core.Responses;
 
@@ -40,17 +37,6 @@ public class ObjectCache<T> where T : class
     {
         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)}");
@@ -67,19 +53,48 @@ public class ObjectCache<T> where T : class
                     Console.WriteLine($"Failed to remove {key} from cache: {e.Message}");
                 }
             }
+            Console.WriteLine($"No item in cache: {key}");
             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.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 (true)
+            {
+                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);   
+                }
+            }
+        });
+    }
 }
 public class GenericResult<T>
 {
     public T? Result { get; set; }
     public DateTime? ExpiryTime { get; set; }
+    
+    public GenericResult()
+    {
+        //expiry timer
+        
+    }
+    public GenericResult(T? result, DateTime? expiryTime = null) : this()
+    {
+        Result = result;
+        ExpiryTime = expiryTime;
+    }
 }