protected virtual async Task UpdateAllData() { var values = await Database.StringGetAsync(CurrentCachedKeys).ConfigureAwait(false); for (var i = 0; i < CurrentCachedKeys.Length; ++i) { Cache.AddOrUpdate(CurrentCachedKeys[i], values[i]); } foreach (var cacheKey in Cache.Keys) { if (CachedKeys.ContainsKey(cacheKey)) { continue; } _tasks.Add(Task.Run(() => { Cache.TryRemove(cacheKey, out _); })); } if (_tasks.Count == 0) { return; } Task.WaitAll(_tasks.ToArray()); _tasks.Clear(); }
protected virtual void UpdateAllData() { _tasks.Clear(); for (var i = 0; i < CachedKeys.Keys.Count; i++) { var index = i; _tasks.Add(Task.Run(() => { var key = CachedKeys.Keys.ElementAt(index); Cache.AddOrUpdate(key, Database.StringGet(key)); })); } foreach (var cacheKey in Cache.Keys) { if (CachedKeys.ContainsKey(cacheKey)) { continue; } _tasks.Add(Task.Run(() => { Cache.TryRemove(cacheKey, out _); })); } Task.WaitAll(_tasks.ToArray()); }
protected void Unsubscribe(string key, int count = 1) { if (key is null) { throw new ArgumentNullException(nameof(key)); } if (CachedKeys.AddOrUpdate(key, 0, (k, i) => i - count) <= 0) { CachedKeys.TryRemove(key, out _); } }