Exemple #1
0
        /// <summary>
        ///     Attempts to get a value from cache.
        ///     If it is found in cache, return true, and the value.
        ///     If it is not found in cache, return false, and use the valueFactory callback to generate and return the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value"></param>
        /// <param name="valueFactory">A callback that can create the value.</param>
        /// <returns>
        ///     True if the value came from cache, otherwise false.
        /// </returns>
        public bool TryGetOrAdd(TKey key, out TValue value, Func <TKey, TValue> valueFactory)
        {
            RedisValue redisValue;
            RedisKey   redisKey = GetRedisKey(key);

            if (!_memoryStore.TryGetString(redisKey, out redisValue))
            {
                if (InnerCache != null)
                {
                    bool cameFromCache = InnerCache.TryGetOrAdd(key, out value, valueFactory);

                    _memoryStore.StringSet(redisKey, GetRedisValue(key, value), KeyExpiry);

                    return(cameFromCache);
                }

                value = valueFactory(key);

                _memoryStore.StringSet(redisKey, GetRedisValue(key, value), KeyExpiry);

                return(false);
            }

            value = GetValue(redisValue);
            return(true);
        }
Exemple #2
0
        /// <summary>
        ///     Attempts to get a value from cache.
        ///     If it is found in cache, return true, and the value.
        ///     If it is not found in cache, return false, and use the valueFactory callback to generate and return the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="valueFactory">A callback that can create the value.</param>
        /// <returns>
        ///     True if the value came from cache, otherwise false.
        /// </returns>
        public bool TryGetOrAdd(TKey key, out TValue value, Func <TKey, TValue> valueFactory)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException(nameof(valueFactory));
            }

            bool cameFromCache = InnerCache.TryGetOrAdd(key, out value, valueFactory);

            if (cameFromCache)
            {
                Interlocked.Increment(ref _hits);
            }
            else
            {
                Interlocked.Increment(ref _misses);
                IncrementCount();
                MetricReporter.NotifySizeChange(Name);
            }

            MetricReporter.NotifyHitsAndMissesChange(Name);

            return(cameFromCache);
        }
 /// <summary>
 ///     Attempts to get a value from cache.
 ///     If it is found in cache, return true, and the value.
 ///     If it is not found in cache, return false, and use the valueFactory callback to generate and return the value.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="value">The value.</param>
 /// <param name="valueFactory">A callback that can create the value.</param>
 /// <returns>
 ///     True if the value came from cache, otherwise false.
 /// </returns>
 public bool TryGetOrAdd(TKey key, out TValue value, Func <TKey, TValue> valueFactory)
 {
     return(InnerCache.TryGetOrAdd(key, out value, valueFactory));
 }