Esempio n. 1
0
        /// <summary>
        ///     Get a cached item. If it's not in the cache yet, then load and cache it
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="key">Cache key</param>
        /// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param>
        /// <param name="acquire">Function to load item if it's not in the cache yet</param>
        /// <returns>Cached item</returns>
        public static CacheValue <T> Get <T>(this ICacheManager cacheManager, string key, int cacheTime, Func <T> acquire)
        {
            if (cacheTime <= 0)
            {
                return(new CacheValue <T>(acquire(), true));
            }

            var cacheValue = cacheManager.Get <T>(key);

            if (!cacheValue.HasValue)
            {
                cacheValue = new CacheValue <T>(acquire(), true);
                cacheManager.Set(key, cacheValue.Value, cacheTime);
            }
            return(cacheValue);
        }
Esempio n. 2
0
        /// <summary>
        ///     Get a cached item. If it's not in the cache yet, then load and cache it
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="key">Cache key</param>
        /// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param>
        /// <param name="acquire">Async Function to load item if it's not in the cache yet</param>
        /// <param name="continueOnCapturedContext"></param>
        /// <returns>Cached item</returns>
        public static async Task <CacheValue <T> > GetAsync <T>(this ICacheManager cacheManager,
                                                                string key,
                                                                int cacheTime,
                                                                Func <Task <T> > acquire,
                                                                bool continueOnCapturedContext = false)
        {
            if (cacheTime <= 0)
            {
                return(new CacheValue <T>(await acquire().ConfigureAwait(continueOnCapturedContext), true));
            }

            var cacheValue = await cacheManager.GetAsync <T>(key)
                             .ConfigureAwait(continueOnCapturedContext);

            if (!cacheValue.HasValue)
            {
                cacheValue = new CacheValue <T>(await acquire().ConfigureAwait(continueOnCapturedContext), true);
                await cacheManager.SetAsync(key, cacheValue.Value, cacheTime)
                .ConfigureAwait(continueOnCapturedContext);

                return(cacheValue);
            }
            return(cacheValue);
        }