/// <summary>Trys to extract immutable object from cache.</summary>
        /// <typeparam name="T">Type of presisted value.</typeparam>
        /// <param name="checksum">Checksum of the value that should be fetched.</param>
        /// <param name="value">The value that is returned from cache.</param>
        /// <returns>Returns true if found value in cache.</returns>
        /// <exception cref="ArgumentNullException">Throws System.ArgumentNullException if checksum is null.</exception>
        public bool TryGet <T>(Checksum checksum, out StoreResult <T> value)
        {
            if (checksum == null)
            {
                throw new ArgumentNullException(nameof(checksum));
            }

            var key = checksum.ToString();

            value = (StoreResult <T>) this.memoryCache.Get(key);
            return(value != null);
        }
コード例 #2
0
        /// <inheritdoc/>
        public bool Equals(StoreResult <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            if (this.Contains != other.Contains)
            {
                return(false);
            }

            if (!this.Contains)
            {
                return(true);
            }

            return(this.presistedValue.Equals(other.GetPresistedValue()));
        }
        /// <summary>Adds instance to cache.</summary>
        /// <typeparam name="T">Type of presisted value.</typeparam>
        /// <param name="value">Values that should be added to cache.</param>
        /// <returns>Returns value currently stored in cache.</returns>
        /// <exception cref="ArgumentNullException">Throws <see cref="ArgumentNullException"/> if value is null.</exception>
        /// <exception cref="ArgumentException">Throws <see cref="ArgumentException"/> if value did not have Contains set to true.</exception>
        public StoreResult <T> AddOrGet <T>(StoreResult <T> value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (!value.Contains)
            {
                throw new ArgumentException("Contains was false.", nameof(value));
            }

            var key        = value.GetPresistedValue().Checksum.ToString();
            var cacheValue = (StoreResult <T>) this.memoryCache.AddOrGetExisting(key, value, new CacheItemPolicy());

            if (cacheValue == null)
            {
                return(value);
            }

            return(cacheValue);
        }