internal CacheStrategyAsync <T> CompleteAsync <T>()
        {
            var copy = new CacheStrategyAsync <T>(Cache, BaseKey);

            copy.CopyFrom(this);
            return(copy);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Specifies a data retrieval strategy if the desired value does not exist in the cache or is invalid
        /// </summary>
        /// <param name="retrieve">An asynchronous delegate that specifies how the value is to be retrieved</param>
        /// <returns>An updated cache strategy that includes the retrieval strategy</returns>
        public CacheStrategyAsync <T> RetrieveUsingAsync <T>(Func <P1, Task <T> > retrieve)
        {
            CacheStrategyAsync <T> strategy = base.CompleteAsync <T>();
            var p1 = strategy.GetParameter <P1>(0);

            strategy.RetrieveCallback = () => retrieve(p1);

            return(strategy);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously gets all cached results
        /// </summary>
        public async Task <IList <CachedValue <TResult> > > GetAllAsync()
        {
            var keysToLoad = Keys.ToList();
            var results    = new List <CachedValue <TResult> >(Keys.Count);

            foreach (TKey key in Keys)
            {
                string itemKey = GetItemKey(key);
                CacheStrategyAsync <TResult> itemStrategy = new CacheStrategyAsync <TResult>(Cache, itemKey).WithRegion(Region);

                if (ValidateCallback != null)
                {
                    itemStrategy = itemStrategy.ValidateAsync(ValidateCallback);
                }

                CachedValue <TResult> cachedValue = await itemStrategy.GetAsync();

                if (cachedValue != null)
                {
                    keysToLoad.Remove(key);
                    results.Add(cachedValue);
                }
            }

            if (RetrieveCallback != null)
            {
                ICollection <KeyValuePair <TKey, TResult> > newResults = await RetrieveCallback(keysToLoad);

                foreach (KeyValuePair <TKey, TResult> result in newResults)
                {
                    string  itemKey = GetItemKey(result.Key);
                    TResult value   = result.Value;

                    CachedValue <TResult> cachedValue = Cache.Set(itemKey, Region, value, DefaultExpiration);

                    results.Add(cachedValue);
                }
            }

            return(results);
        }