Exemplo n.º 1
0
        private static void HandleExpiration <T>(HttpCacheShim that
                                                 , CacheAddParameters <T> parameters
                                                 , string cacheKey
                                                 , CacheItemUpdateReason reason
                                                 , out object expensiveObject
                                                 , out CacheDependency dependency
                                                 , out DateTime absoluteExpiration
                                                 , out TimeSpan slidingExpiration)
            where T : class
        {
            Log("Expired", () => "(" + reason + ") " + cacheKey);

            expensiveObject    = null;
            dependency         = null;
            absoluteExpiration = Cache.NoAbsoluteExpiration;
            slidingExpiration  = Cache.NoSlidingExpiration;

            // if we were not shutting down, might want to handle the reuse/refresh
            if (reason == CacheItemUpdateReason.Expired &&
                !AppDomain.CurrentDomain.IsFinalizingForUnload())
            {
                if (parameters.ShouldScheduleRefresh &&
                    HttpCacheShim.DisableBackfill == false &&
                    !BackgroundQueue.IsBacklogged())
                {
                    // we need queue a request to the underlying store to get more current data into the cache so it stays primed.
                    BackgroundQueue.Enqueue(parameters);
                }
            }
        }
Exemplo n.º 2
0
        private Tuple <IList <TElement>, bool> MemoizeCollection <TElement>(
            CacheAddParameters <IList <IBaseCacheKey> > parameters)
            where TElement : class
        {
            IList <TElement> collection = null;
            var result = parameters.Fill(FreshnessRequest.Normal);

            if (result.Item2 || result.Item1 == null)
            {
                return(Tuple.Create(collection, result.Item2));
            }

            var cachedKeys = result.Item1;

            collection = new List <TElement>(cachedKeys.Count);
            foreach (var cachedKey in cachedKeys)
            {
                var element = this.Get <TElement>(cachedKey.Key);

                if (element == null)
                {
                    return(null);   // failed to find one... declare failure and go to backing store
                }
                collection.Add(element);
            }

            // shove the answer-set into the cache only AFTER we validate all the entries.
            parameters.Put(cachedKeys);
            return(Tuple.Create(collection, false));
        }
Exemplo n.º 3
0
        private CacheItemUpdateCallback MakeCallback <T>(CacheAddParameters <T> parameters)
            where T : class
        {
            CacheItemUpdateCallback callback = (string updatedKey, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration) =>
            {
                var parms = parameters;
                HandleExpiration(this, parms, updatedKey, reason, out expensiveObject, out dependency, out absoluteExpiration, out slidingExpiration);
            };

            return(callback);
        }
Exemplo n.º 4
0
        private static Tuple <T, bool> MemoizeElement <T>(CacheAddParameters <T> parameters)
            where T : class
        {
            var result = parameters.Fill(FreshnessRequest.Normal);

            if (result != null && !result.Item2)
            {
                var value = result.Item1;
                parameters.Put(value);
            }

            return(result);
        }
Exemplo n.º 5
0
        private void InternalPut <T>(CacheAddParameters <T> parameters, T value)
            where T : class
        {
            string key = parameters.Name;

            if (value == null)
            {
                Log("InternalPut", () => key + " {REMOVE}");
                HttpRuntime.Cache.Remove(key);
            }
            else
            {
                var callback           = MakeCallback(parameters);
                var absoluteExpiration = parameters.AbsoluteExpiration;
                var slidingTimeout     = parameters.SlidingTimeout;
                Log("InternalPut", () => key + " @" + absoluteExpiration + " [" + slidingTimeout + "]");
                HttpRuntime.Cache.Insert(key, value, null, absoluteExpiration, slidingTimeout, callback);
            }
        }