about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Room.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-11 20:56:16 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-11 20:56:16 +0200
commita069cfd6a0f7c53b902607e79037ffd90681a7b9 (patch)
tree0cfc47619d35e2d89568f5d1c151ba8e1906ec85 /MatrixRoomUtils.Core/Room.cs
parentAdd license, deploy src (diff)
downloadMatrixUtils-a069cfd6a0f7c53b902607e79037ffd90681a7b9.tar.xz
Add state cache
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs41
1 files changed, 38 insertions, 3 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index be470fa..3ef6ffa 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -16,17 +16,52 @@ public class Room
     
     public async Task<JsonElement?> GetStateAsync(string type, string state_key="", bool logOnFailure = false)
     {
-        var url = $"/_matrix/client/r0/rooms/{RoomId}/state";
+        var url = $"/_matrix/client/v3/rooms/{RoomId}/state";
         if (!string.IsNullOrEmpty(state_key)) url += $"/{type}/{state_key}";
         else if (!string.IsNullOrEmpty(type)) url += $"/{type}";
-        
+        var cache_key = "room_states_"+type;
+        if (!RuntimeCache.GenericResponseCache.ContainsKey(cache_key))
+        {
+            Console.WriteLine($"[!!] No cache for {cache_key}, creating...");
+            RuntimeCache.GenericResponseCache.Add(cache_key, new ObjectCache<object?>()
+            {
+                DefaultExpiry = type switch
+                {
+                    "m.room.name" => TimeSpan.FromMinutes(15),
+                    _ => TimeSpan.FromMinutes(5)
+                }
+            });
+        }
+
+        if (RuntimeCache.GenericResponseCache[cache_key][url] != null)
+        {
+            if(RuntimeCache.GenericResponseCache[cache_key][url].ExpiryTime > DateTime.Now)
+            {
+                Console.WriteLine($"[:3] Found cached state: {RuntimeCache.GenericResponseCache[cache_key][url].Result}");
+                return (JsonElement?)RuntimeCache.GenericResponseCache[cache_key][url].Result;
+            }
+            else
+            {
+                Console.WriteLine($"[!!] Cached state expired at {RuntimeCache.GenericResponseCache[cache_key][url].ExpiryTime}: {RuntimeCache.GenericResponseCache[cache_key][url].Result}");
+            }
+        }
+        else
+        {
+            Console.WriteLine($"[!!] No cached state for {url}");
+        }
+
         var res = await _httpClient.GetAsync(url);
         if (!res.IsSuccessStatusCode)
         {
             if(logOnFailure) Console.WriteLine($"{RoomId}/{state_key}/{type} - got status: {res.StatusCode}");
             return null;
         }
-        return await res.Content.ReadFromJsonAsync<JsonElement>();
+        var result = await res.Content.ReadFromJsonAsync<JsonElement>();
+        RuntimeCache.GenericResponseCache[cache_key][url] = new GenericResult<object>()
+        {
+            Result = result
+        };
+        return result;
     }
     public async Task<string?> GetNameAsync()
     {