using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Teknik.Utilities { public class ObjectCache { private readonly static Dictionary> objectCache = new Dictionary>(); private readonly int _cacheSeconds; public ObjectCache(int cacheSeconds) { _cacheSeconds = cacheSeconds; } public T GetObject(string key, Func getObjectFunc) { T foundObject; var cacheDate = DateTime.UtcNow; if (objectCache.TryGetValue(key, out var result) && result.Item1 > cacheDate.Subtract(new TimeSpan(0, 0, _cacheSeconds))) { cacheDate = result.Item1; foundObject = (T)result.Item2; } else { foundObject = getObjectFunc(key); } if (foundObject != null) objectCache[key] = new Tuple(cacheDate, foundObject); return foundObject; } public void UpdateObject(string key, T update) { var cacheDate = DateTime.UtcNow; if (objectCache.TryGetValue(key, out var result)) { if (result.Item1 <= cacheDate.Subtract(new TimeSpan(0, 0, _cacheSeconds))) DeleteObject(key); else objectCache[key] = new Tuple(result.Item1, update); } } public void DeleteObject(string key) { objectCache.Remove(key); } } }