Exemplo n.º 1
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));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 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="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));
        }
Exemplo n.º 6
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));
            }
        }