Ejemplo n.º 1
0
 public async Task <TCacheItem> AwaitCacheItem(LazyAwaitableCacheItem <TCacheItem> itemToAwait)
 {
     try
     {
         return(await itemToAwait.GetTask().ConfigureAwait(false));
     }
     catch (Exception ex)
     {
         itemToAwait.Reset();
         throw ex;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets or creates an item in the cache.
        /// </summary>
        /// <param name="key">The key under which the item is stored.</param>
        /// <param name="itemFactory">A fsctory operation for creating the item.</param>
        /// <returns>A Task representing the cache item.</returns>
        public async Task <TCacheItem> GetOrCreateItem(
            string key,
            Func <Task <TCacheItem> > itemFactory,
            TimeSpan?expiresAfter = null)
        {
            LazyAwaitableCacheItem <TCacheItem> cacheItem =
                this.cache.GetOrAdd(
                    key,
                    new LazyAwaitableCacheItem <TCacheItem>(itemFactory));

            this.OnItemAdd(this, new AddItemEventArgs(key, expiresAfter ?? this.expiresAfterOnDefault));

            return(await this.awaitingStrategy.AwaitCacheItem(cacheItem));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads a particular item out of the cache.
        /// If under the supplied key no item is found,
        /// default(TCacheItem) is retured.
        /// </summary>
        /// <param name="key">Specifies the key of the requested item.
        /// Must not be null, whitespace or empty.</param>
        /// <returns>A Task representing the requested item.</returns>
        public async Task <TCacheItem> GetItem(string key)
        {
            if (String.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            LazyAwaitableCacheItem <TCacheItem> encachedValue = null;
            Task <TCacheItem> cacheItemTask = null;

            if (cache.TryGetValue(key, out encachedValue))
            {
                cacheItemTask = this.awaitingStrategy.AwaitCacheItem(encachedValue);
            }
            else
            {
                cacheItemTask = Task.FromResult <TCacheItem>(default(TCacheItem));
            }
            return(await cacheItemTask);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Tries to remove an item from the cache.
 /// </summary>
 /// <param name="key">The of the item.</param>
 /// <param name="removedCacheItem">References the cacheitem when removing succeeded.</param>
 /// <returns>Returns true when removing succeeded. Otherwise false.</returns>
 public bool TryRemove(
     string key,
     out LazyAwaitableCacheItem <TCacheItem> removedCacheItem)
 {
     return(this.cache.TryRemove(key, out removedCacheItem));
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="itemToAwait"></param>
 /// <returns></returns>
 public async Task <TCacheItem> AwaitCacheItem(LazyAwaitableCacheItem <TCacheItem> itemToAwait)
 {
     return(await itemToAwait.GetTask().ConfigureAwait(false));
 }