Example #1
0
        /// <summary>
        /// Executes a method and stores the result in cache using the given cache key.  If the data already exists in cache, it returns the data
        /// and doesn't execute the method.  Thread safe, although the method parameterisn't guaranteed to be thread safe.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache"></param>
        /// <param name="cacheKey">Each method has it's own isolated set of cache items,so cacheKeys won't overlap accross methods.</param>
        /// <param name="method"></param>
        /// <param name="expirationSeconds">Lifetime of cache items, in seconds</param>
        /// <returns></returns>
        public static T Data <T>(string cacheKey,
                                 int expirationSeconds, System.Func <T> method)
        {
            if (!EnableCache)
            {
                return(method());
            }
            EnsureHttpRuntime();
            var cache = HttpRuntime.Cache;
            var hash  = method.GetHashCode().ToString();
            var data  = (T)cache[hash + cacheKey];

            if (data == null)
            {
                data = method();

                if (expirationSeconds > 0 && data != null)
                {
                    lock (sync)
                    {
                        cache.Insert(hash + cacheKey, data, null, DateTime.Now.AddSeconds
                                         (expirationSeconds), Cache.NoSlidingExpiration);
                    }
                }
            }
            return(data);
        }
Example #2
0
        public override int GetHashCode()
        {
            int prime  = 31;
            int result = base.GetHashCode();

            result = prime * result + _function.GetHashCode();
            return(result);
        }