예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CacheUpdateEventArgs{TCacheValue}" /> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="region">The region.</param>
 /// <param name="maxRetries">The number of retries configured.</param>
 /// <param name="result">The result.</param>
 public CacheUpdateEventArgs(string key, string region, int maxRetries, UpdateItemResult <TCacheValue> result)
 {
     this.Key        = key;
     this.Region     = region;
     this.Result     = result;
     this.MaxRetries = maxRetries;
 }
예제 #2
0
        /// <summary>
        /// Updates an existing key in the cache.
        /// <para>
        /// The cache manager will make sure the update will always happen on the most recent version.
        /// </para>
        /// <para>
        /// If version conflicts occur, if for example multiple cache clients try to write the same
        /// key, and during the update process, someone else changed the value for the key, the
        /// cache manager will retry the operation.
        /// </para>
        /// <para>
        /// The <paramref name="updateValue"/> function will get invoked on each retry with the most
        /// recent value which is stored in cache.
        /// </para>
        /// </summary>
        /// <param name="key">The key to update.</param>
        /// <param name="region">The cache region.</param>
        /// <param name="updateValue">The function to perform the update.</param>
        /// <param name="maxRetries">The number of tries.</param>
        /// <returns>The update result which is interpreted by the cache manager.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="key"/>, <paramref name="region"/> or <paramref name="updateValue"/> is null.
        /// </exception>
        /// <remarks>
        /// If the cache does not use a distributed cache system. Update is doing exactly the same
        /// as Get plus Put.
        /// </remarks>
        public virtual UpdateItemResult <TCacheValue> Update(string key, string region, Func <TCacheValue, TCacheValue> updateValue, int maxRetries)
        {
            NotNull(updateValue, nameof(updateValue));
            CheckDisposed();

            lock (_updateLock)
            {
                var original = GetCacheItem(key, region);
                if (original == null)
                {
                    return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
                }

                var newValue = updateValue(original.Value);
                if (newValue == null)
                {
                    return(UpdateItemResult.ForFactoryReturnedNull <TCacheValue>());
                }

                var newItem = original.WithValue(newValue);

                newItem.LastAccessedUtc = DateTime.UtcNow;
                Put(newItem);
                return(UpdateItemResult.ForSuccess(newItem));
            }
        }
        private UpdateItemResult <TCacheValue> UpdateInternal(string key, string region, Func <TCacheValue, TCacheValue> updateValue, UpdateItemConfig config)
        {
            NotNull(updateValue, nameof(updateValue));
            NotNull(config, nameof(config));

            var retries = 0;

            do
            {
                retries++;

                var fullKey = GetKey(key, region);
                var item    = this.GetCacheItemInternal(key, region);
                if (item == null)
                {
                    return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
                }

                var newValue = updateValue(item.Value);
                var newItem  = item.WithValue(newValue);

                if (this.cache.TryUpdate(fullKey, newItem, item))
                {
                    return(UpdateItemResult.ForSuccess <TCacheValue>(newItem.Value, retries > 1, retries));
                }
            }while (retries <= config.MaxRetries);

            return(UpdateItemResult.ForTooManyRetries <TCacheValue>(retries));
        }
예제 #4
0
        /// <summary>
        /// Called when an item has been updated.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="region">The region.</param>
        /// <param name="result">The result.</param>
        /// <exception cref="System.ArgumentNullException">If key or result are null.</exception>
        public void OnUpdate(TKey key, UpdateItemResult <TKey, TValue> result)
        {
            if (!_isStatsEnabled)
            {
                return;
            }

            NotNull(key, nameof(key));
            NotNull(result, nameof(result));

            _counter.Add(CacheStatsCounterType.GetCalls, result.NumberOfTriesNeeded);
            _counter.Add(CacheStatsCounterType.Hits, result.NumberOfTriesNeeded);
            _counter.Increment(CacheStatsCounterType.PutCalls);
        }
예제 #5
0
        /// <summary>
        /// Updates an existing key in the cache.
        /// <para>
        /// The cache manager will make sure the update will always happen on the most recent version.
        /// </para>
        /// <para>
        /// If version conflicts occur, if for example multiple cache clients try to write the same
        /// key, and during the update process, someone else changed the value for the key, the
        /// cache manager will retry the operation.
        /// </para>
        /// <para>
        /// The <paramref name="updateValue"/> function will get invoked on each retry with the most
        /// recent value which is stored in cache.
        /// </para>
        /// </summary>
        /// <param name="key">The key to update.</param>
        /// <param name="region">The cache region.</param>
        /// <param name="updateValue">The function to perform the update.</param>
        /// <param name="config">The cache configuration used to specify the update behavior.</param>
        /// <returns>The update result which is interpreted by the cache manager.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// If key, region, updateValue or config are null.
        /// </exception>
        /// <remarks>
        /// If the cache does not use a distributed cache system. Update is doing exactly the same
        /// as Get plus Put.
        /// </remarks>
        public virtual UpdateItemResult <TCacheValue> Update(string key, string region, Func <TCacheValue, TCacheValue> updateValue, UpdateItemConfig config)
        {
            NotNull(updateValue, nameof(updateValue));

            var original = this.GetCacheItem(key, region);

            if (original == null)
            {
                return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
            }

            var value = updateValue(original.Value);

            this.Put(key, value, region);
            return(UpdateItemResult.ForSuccess <TCacheValue>(value));
        }
예제 #6
0
        /// <summary>
        /// Called when an item has been updated.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="region">The region.</param>
        /// <param name="result">The result.</param>
        /// <exception cref="System.ArgumentNullException">If key or result are null.</exception>
        public void OnUpdate(string key, string region, UpdateItemResult <TCacheValue> result)
        {
            if (!_isStatsEnabled)
            {
                return;
            }

            NotNullOrWhiteSpace(key, nameof(key));
            NotNull(result, nameof(result));

            foreach (var counter in GetWorkingCounters(region))
            {
                counter.Add(CacheStatsCounterType.GetCalls, result.NumberOfTriesNeeded);
                counter.Add(CacheStatsCounterType.Hits, result.NumberOfTriesNeeded);
                counter.Increment(CacheStatsCounterType.PutCalls);
            }
        }
예제 #7
0
        /// <summary>
        /// Updates an existing key in the cache.
        /// <para>
        /// The cache manager will make sure the update will always happen on the most recent version.
        /// </para>
        /// <para>
        /// If version conflicts occur, if for example multiple cache clients try to write the same
        /// key, and during the update process, someone else changed the value for the key, the
        /// cache manager will retry the operation.
        /// </para>
        /// <para>
        /// The <paramref name="updateValue"/> function will get invoked on each retry with the most
        /// recent value which is stored in cache.
        /// </para>
        /// </summary>
        /// <param name="key">The key to update.</param>
        /// <param name="region">The cache region.</param>
        /// <param name="updateValue">The function to perform the update.</param>
        /// <param name="maxRetries">The number of tries.</param>
        /// <returns>The update result which is interpreted by the cache manager.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="key"/>, <paramref name="region"/> or <paramref name="updateValue"/> is null.
        /// </exception>
        /// <remarks>
        /// If the cache does not use a distributed cache system. Update is doing exactly the same
        /// as Get plus Put.
        /// </remarks>
        public virtual UpdateItemResult <TCacheValue> Update(string key, string region, Func <TCacheValue, TCacheValue> updateValue, int maxRetries)
        {
            NotNull(updateValue, nameof(updateValue));
            this.CheckDisposed();

            lock (this.updateLock)
            {
                var original = this.GetCacheItem(key, region);
                if (original == null)
                {
                    return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
                }

                var value = updateValue(original.Value);
                this.Put(key, value, region);
                return(UpdateItemResult.ForSuccess <TCacheValue>(value));
            }
        }
예제 #8
0
        /// <summary>
        /// Updates an existing key in the cache.
        /// <para>
        /// The cache manager will make sure the update will always happen on the most recent version.
        /// </para>
        /// <para>
        /// If version conflicts occur, if for example multiple cache clients try to write the same
        /// key, and during the update process, someone else changed the value for the key, the
        /// cache manager will retry the operation.
        /// </para>
        /// <para>
        /// The <paramref name="updateValue"/> function will get invoked on each retry with the most
        /// recent value which is stored in cache.
        /// </para>
        /// </summary>
        /// <param name="key">The key to update.</param>
        /// <param name="updateValue">The function to perform the update.</param>
        /// <param name="config">The cache configuration used to specify the update behavior.</param>
        /// <returns>The update result which is interpreted by the cache manager.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// If key, updateValue or config are null.
        /// </exception>
        /// <remarks>
        /// If the cache does not use a distributed cache system. Update is doing exactly the same
        /// as Get plus Put.
        /// </remarks>
        public virtual UpdateItemResult <TCacheValue> Update(string key, Func <TCacheValue, TCacheValue> updateValue, UpdateItemConfig config)
        {
            if (updateValue == null)
            {
                throw new ArgumentNullException("updateValue");
            }

            var original = this.GetCacheItem(key);

            if (original == null)
            {
                return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
            }

            var value = updateValue(original.Value);

            this.Put(key, value);
            return(UpdateItemResult.ForSuccess <TCacheValue>(value));
        }
예제 #9
0
        /// <summary>
        /// Updates an existing key in the cache.
        /// <para>
        /// The cache manager will make sure the update will always happen on the most recent version.
        /// </para>
        /// <para>
        /// If version conflicts occur, if for example multiple cache clients try to write the same
        /// key, and during the update process, someone else changed the value for the key, the
        /// cache manager will retry the operation.
        /// </para>
        /// <para>
        /// The <paramref name="updateValue"/> function will get invoked on each retry with the most
        /// recent value which is stored in cache.
        /// </para>
        /// </summary>
        /// <param name="key">The key to update.</param>
        /// <param name="updateValue">The function to perform the update.</param>
        /// <param name="maxRetries">The number of tries.</param>
        /// <returns>The update result which is interpreted by the cache manager.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="key"/> or <paramref name="updateValue"/> is null.
        /// </exception>
        /// <remarks>
        /// If the cache does not use a distributed cache system. Update is doing exactly the same
        /// as Get plus Put.
        /// </remarks>
        public virtual UpdateItemResult <TCacheValue> Update(string key, Func <TCacheValue, TCacheValue> updateValue, int maxRetries)
        {
            NotNull(updateValue, nameof(updateValue));
            this.CheckDisposed();

            lock (this.updateLock)
            {
                var original = this.GetCacheItem(key);
                if (original == null)
                {
                    return(UpdateItemResult.ForItemDidNotExist <TCacheValue>());
                }

                var value   = updateValue(original.Value);
                var newItem = original.WithValue(value);
                newItem.LastAccessedUtc = DateTime.UtcNow;
                this.Put(newItem);
                return(UpdateItemResult.ForSuccess <TCacheValue>(value));
            }
        }
예제 #10
0
        /// <summary>
        /// Called when an item has been updated.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="region">The region.</param>
        /// <param name="result">The result.</param>
        /// <exception cref="System.ArgumentNullException">If key or result are null.</exception>
        public void OnUpdate(string key, string region, UpdateItemResult <TCacheValue> result)
        {
            if (!this.isStatsEnabled)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException("key");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            foreach (var counter in this.GetWorkingCounters(region))
            {
                counter.Add(CacheStatsCounterType.GetCalls, result.NumberOfTriesNeeded);
                counter.Add(CacheStatsCounterType.Hits, result.NumberOfTriesNeeded);
                counter.Increment(CacheStatsCounterType.PutCalls);
            }
        }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CacheUpdateEventArgs{TCacheValue}" /> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="region">The region.</param>
 /// <param name="config">The configuration.</param>
 /// <param name="result">The result.</param>
 public CacheUpdateEventArgs(string key, string region, UpdateItemConfig config, UpdateItemResult <TCacheValue> result)
 {
     this.Key    = key;
     this.Region = region;
     this.Result = result;
     this.Config = config;
 }