using System; using System.Linq; using System.Threading.Tasks; namespace Cosmos.Debug.Common { public class CacheHelper where TKey: IEquatable { public CacheHelper(Func getValueFunc) { if (getValueFunc == null) { throw new ArgumentNullException("getValueFunc"); } mGetValueFunc = getValueFunc; } private readonly Func mGetValueFunc; private TValue mCachedValue; private TKey mCachedKey; private bool mHasCachedValue = false; public TValue GetValue(TKey key) { if (mHasCachedValue && mCachedKey.Equals(key)) { return mCachedValue; } mCachedValue = mGetValueFunc(key); mCachedKey = key; mHasCachedValue = true; return mCachedValue; } } }