示例#1
0
        /// <summary>
        /// Retrieves the object by the specified key from the cache.  If the object does not exist in the cache, it will be added.
        /// </summary>
        /// <typeparam name="T">Type of object to return</typeparam>
        /// <param name="cache">Cache to work with</param>
        /// <param name="key">Cache key for the object</param>
        /// <param name="cacheAction">Action to perform if the object does not exist in the cache.</param>
        /// <returns></returns>
        public static T GetOrCache <T>(this ICacheClient cache, string key, Func <ICacheContext, T> cacheAction) where T : class
        {
            var item      = cache.Get <T>(key);
            var eventArgs = new CacheHitEventArgs
            {
                CacheKey = key,
                Type     = typeof(T)
            };

            if (item == null)
            {
                CacheStackSettings.OnCacheMiss(cache, eventArgs);
                var context = new CacheContext(cache);

                item = cacheAction(context);

                // No need to cache null values
                if (item != null)
                {
                    cache.CacheAndSetTriggers(context, key, item);
                }
            }
            else
            {
                CacheStackSettings.OnCacheHit(cache, eventArgs);
            }
            return(item);
        }
示例#2
0
 internal static void OnCacheMiss(object cacheClient, CacheHitEventArgs e)
 {
     CacheMiss(cacheClient, e);
 }