diff --git a/Utils/CacheItem.cs b/Utils/CacheItem.cs index da86400..ebefd8d 100644 --- a/Utils/CacheItem.cs +++ b/Utils/CacheItem.cs @@ -8,6 +8,16 @@ public class CacheItem public bool HasExpired(ulong ttl) { + // this specific value indicates the item is manually staled + if (TimeToLiveTimestamp == ulong.MaxValue) + { + return true; + } return Time.GetTicksMsec() > TimeToLiveTimestamp + ttl; } + + public void Stale() + { + TimeToLiveTimestamp = ulong.MaxValue; + } } diff --git a/Utils/CacheStore.cs b/Utils/CacheStore.cs index 6e86deb..a116116 100644 --- a/Utils/CacheStore.cs +++ b/Utils/CacheStore.cs @@ -1,12 +1,12 @@ using Godot; using System.Collections.Generic; -public class CacheStore +public class CacheStore : IEnumerable>> { // default TTL is 1 min public ulong TimeToLive { get; } = 60000; - private Dictionary> _store = new(); + private readonly Dictionary> _store = new(); public CacheItem this[TKey key] { @@ -31,6 +31,16 @@ public class CacheStore } } + public IEnumerator>> GetEnumerator() + { + return _store.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + public TVal Retrieve(TKey key) { if (IsItemValid(key)) diff --git a/Utils/World.cs b/Utils/World.cs index 6163081..1eb9a50 100644 --- a/Utils/World.cs +++ b/Utils/World.cs @@ -337,5 +337,18 @@ public partial class World : Node CurrentPlayer.Spawn(); } + public void StaleCache() + { + foreach (var kv in _maps) + { + var map = kv.Value?.Value; + if (map is not null && IsInstanceValid(map)) + { + GD.Print($"Staling {kv.Key}"); + kv.Value.Stale(); + } + } + } + public Node FindEntity(string name) => CurrentMap.Entities.GetNode(name); }