manual cache staling
							parent
							
								
									29b146558e
								
							
						
					
					
						commit
						0d4605009b
					
				|  | @ -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; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -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)) | ||||
|  |  | |||
|  | @ -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); | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue