Ejemplo n.º 1
0
        /// <inheritdoc />
        /// <summary>
        /// Retrieves an item from the Cache
        /// </summary>
        /// <typeparam name="T">The type of item</typeparam>
        /// <param name="key">Name of the item in cache</param>
        /// <param name="item">If the object is not found in the Cache, this will be populated</param>
        /// <returns>True if the object was found, False if not found</returns>
        public virtual bool Get <T>(string key, out T item)
        {
            // If in L1 cache, just return it
            if (Options.UseLocalCache && L1Cache.TryGetValue(key, out item))
            {
                return(true);
            }

            // Check L2 cache
            if (Options.UseDistributedCache)
            {
                byte[] bytes = null;
                try
                {
                    bytes = L2Cache.Get(key);
                }
                catch { }

                // Was not found
                if (bytes == null)
                {
                    item = default;
                    return(false);
                }

                // Object was found
                var obj = bytes.FromByteArray <L2CacheItem <T> >();
                item = obj.Item;

                // Store the object back into L1 cache
                // ReSharper disable once InvertIf
                if (Options.UseLocalCache)
                {
                    var options = new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = obj.RemainingCacheTime(),
                        Priority = obj.Priority
                    };
                    L1Cache.Set(key, item, options);
                }

                // Return it now
                return(true);
            }

            item = default;
            return(false);
        }