Пример #1
0
        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);
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        public static void Remove <T>(this ObjectCache cache, string key)
        {
            string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key);

            cache.Remove(cacheKey);
        }
Пример #4
0
        public static bool IsSet <T>(this ObjectCache cache, string key)
        {
            string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key);

            return(cache.IsSet(cacheKey));
        }