private static async Task <T> GetAsync <T>(this ObjectCache cache, string key, Func <Task <T> > acquireAsync, Action <string, object> set) { string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key); if (cache.IsSet(cacheKey)) { var acquiredValue = cache.Get(cacheKey); if (acquiredValue is NullObject) { return(default(T)); } return((T)acquiredValue); } using (await CacheLock.LockAsync(cacheKey)) { if (cache.IsSet(cacheKey)) { return((T)cache.Get(cacheKey)); } var acquiredValue = await acquireAsync(); if (acquiredValue != null) { set(cacheKey, acquiredValue); } else { set(cacheKey, NullObject.Instance); } return(acquiredValue); } }
public void ShouldGenerateCacheKeyPrefixedWithGenericTypeParameterFullName() { string personTypeFullName = typeof(Person).FullName; string key = "FindPerson"; string expectedCacheKey = string.Format("XperiCode.SimpleCache-[{0}][{1}]", personTypeFullName, key); string cacheKey = CacheKeyGenerator.GenerateCacheKey <Person>(key); Assert.AreEqual(expectedCacheKey, cacheKey); }
public static void Remove <T>(this ObjectCache cache, string key) { string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key); cache.Remove(cacheKey); }
public static bool IsSet <T>(this ObjectCache cache, string key) { string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key); return(cache.IsSet(cacheKey)); }