manual cache staling

controller-support
HumanoidSandvichDispenser 2023-08-17 01:05:21 -07:00
parent 29b146558e
commit 0d4605009b
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
3 changed files with 35 additions and 2 deletions

View File

@ -8,6 +8,16 @@ public class CacheItem<T>
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;
}
}

View File

@ -1,12 +1,12 @@
using Godot;
using System.Collections.Generic;
public class CacheStore<TKey, TVal>
public class CacheStore<TKey, TVal> : IEnumerable<KeyValuePair<TKey, CacheItem<TVal>>>
{
// default TTL is 1 min
public ulong TimeToLive { get; } = 60000;
private Dictionary<TKey, CacheItem<TVal>> _store = new();
private readonly Dictionary<TKey, CacheItem<TVal>> _store = new();
public CacheItem<TVal> this[TKey key]
{
@ -31,6 +31,16 @@ public class CacheStore<TKey, TVal>
}
}
public IEnumerator<KeyValuePair<TKey, CacheItem<TVal>>> GetEnumerator()
{
return _store.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public TVal Retrieve(TKey key)
{
if (IsItemValid(key))

View File

@ -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);
}