public async Task <T> GetHttpWithSpool(string key, string url, int howLongToStoreSec, Func <string, string, T> decoder) { T data = null; try { SpoolItem <T> item = TrySpool(key, url, howLongToStoreSec); if (item.Data != null) { return(item.Data); } string jsonBody = await Client.GetHttpStringAsync(url); item.Data = decoder(key, jsonBody); return(item.Data); } catch (Exception ex) { Log.LogError(ex.StackTrace); return(null); } return(null); }
private SpoolItem <T> TrySpool(string key, string url, int howLongToStoreSec) { SpoolItem <T> item = DictLazy.Value.AddOrUpdate(key, (key) => { // Create New Instance for Spooler return(new SpoolItem <T>() { Key = key, ActualUntil = DateTime.Now.AddSeconds(howLongToStoreSec), Data = null }); }, (_url, _item) => { // Test of Existing Instance for Time Existance if (_item.ActualUntil < DateTime.Now) { _item.Data = null; Log.LogInformation($"Old tem {url} removed"); } return(_item); } ); return(item); }
public T TryGetValue(string key) { SpoolItem <T> item = null; if (Dict.TryGetValue(key, out item)) { if (item.ActualUntil <= DateTime.Now) { Dict.TryRemove(key, out item); item = null; } } return((item != null) ? item.Data : null); }
public T TryAddValue(string key, T p) { SpoolItem <T> item = Dict.AddOrUpdate(key, item => ToSpoolItem(p), (item, old) => ToSpoolItem(p)); return(item.Data); }