Sdl.Web.Tridion/Providers/Caching/CacheProvider.cs
/// <summary>
/// Tries to gets a value for a given key and cache region. If not found, add a value obtained from a given function.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="region">The name of the cache region. Different cache regions can have different retention policies.</param>
/// <param name="addFunction">A function (delegate) used to obtain the value to add in case an existing cached value is not found.</param>
/// <param name="dependencies">An optional set of dependent item IDs. Can be used to invalidate the cached item.</param>
/// <typeparam name="T">The type of the value to get or add.</typeparam>
/// <remarks>
/// This method is thread-safe; it prevents the same key being added by multiple threads in case of a race condition.
/// </remarks>
/// <returns>The cached value.</returns>
Tries to gets a value for a given key and cache region. If not found, add a value obtained from a given function.
Tries to gets a value for a given key and cache region . If not found add a value obtained from a given function .
public virtual T GetOrAdd<T>(string key, string region, Func<T> addFunction, IEnumerable<string> dependencies = null)
        {
            T result;
            if (TryGet(key, region, out result))
            {
                return result;
            }
            EventWaitHandle addingEvent = new ManualResetEvent(false);
            string qualifiedKey = GetQualifiedKey(key, region);
            try
            {
                _addingEvents.Add(qualifiedKey, addingEvent);
            }
            catch (ArgumentException)
            {
                if (TryGet(key, region, out result))
                {
                    return result;
                }
            }
            try
            {
                result = addFunction();
                Store(key, region, result, dependencies);
            }
            finally
            {
                addingEvent.Set();
                _addingEvents.Remove(qualifiedKey);
            }
            return result;
        }
public virtual T GetOrAdd < T > ( string key , string region , Func < T > addFunction , IEnumerable < string > dependencies = null ) { T result ; if ( TryGet ( key , region , out result ) ) { return result ; } EventWaitHandle addingEvent = new ManualResetEvent ( false ) ; string qualifiedKey = GetQualifiedKey ( key , region ) ; try { _addingEvents . Add ( qualifiedKey , addingEvent ) ; } catch ( ArgumentException ) { if ( TryGet ( key , region , out result ) ) { return result ; } } try { result = addFunction ( ) ; Store ( key , region , result , dependencies ) ; } finally { addingEvent . Set ( ) ; _addingEvents . Remove ( qualifiedKey ) ; } return result ; }
Tries to gets a value for a given key and cache region.
Tries to gets a value for a given key and cache region .
// Indicate to other threads that we're adding a value // Another thread is already adding a value (race condition); retry getting the value. // Obtain the actual value. This may be time-consuming (otherwise there would be no reason to cache). // Unblock other threads which are awaiting it.
The cached value.
The cached value .
key
string
None
region
string
The name of the cache region. Different cache regions can have different retention policies.
The name of the cache region . Different cache regions can have different retention policies .
addFunction
Func<T>
A function (delegate) used to obtain the value to add in case an existing cached value is not found.
A function ( delegate ) used to obtain the value to add in case an existing cached value is not found .
dependencies
IEnumerable<string>
An optional set of dependent item IDs. Can be used to invalidate the cached item.
An optional set of dependent item IDs . Can be used to invalidate the cached item .
typeparam
The type of the value to get or add.
The type of the value to get or add .
remarks
This method is thread-safe; it prevents the same key being added by multiple threads in case of a race condition.
This method is thread - safe ; it prevents the same key being added by multiple threads in case of a race condition .