using System; using System.Collections.Concurrent; using NzbDrone.Common.EnsureThat; namespace NzbDrone.Common.Cache { public class Cached : ICached { private readonly ConcurrentDictionary _store; public Cached() { _store = new ConcurrentDictionary(); } public void Set(string key, T value) { Ensure.That(() => key).IsNotNullOrWhiteSpace(); _store.TryAdd(key, value); } public T Get(string key, Func function) { Ensure.That(() => key).IsNotNullOrWhiteSpace(); T value; if (!_store.TryGetValue(key, out value)) { value = function(); Set(key, value); } return value; } public bool ContainsKey(string key) { Ensure.That(() => key).IsNotNullOrWhiteSpace(); return _store.ContainsKey(key); } public void Clear() { _store.Clear(); } public void Remove(string key) { Ensure.That(() => key).IsNotNullOrWhiteSpace(); T value; _store.TryRemove(key, out value); } } }