Table of Contents

Interface IProgressiveCache

Namespace
CMS.Helpers
Assembly
CMS.Helpers.dll

Provides optimization of parallel data loads. The data are loaded once, stored into cache and the result is shared.

public interface IProgressiveCache
Extension Methods

Remarks

Methods

LoadAsync<TData>(Func<CacheSettings, CancellationToken, Task<TData>>, CacheSettings, CancellationToken)

Asynchronously loads data. If multiple threads are loading the same data, only one thread executes the load, stores the result into cache and the result is distributed to other waiting threads. If an exception is thrown during loadDataFuncAsync execution, the exception is distributed as well. No data are cached in such a case.

Task<TData> LoadAsync<TData>(Func<CacheSettings, CancellationToken, Task<TData>> loadDataFuncAsync, CacheSettings settings, CancellationToken cancellationToken)

Parameters

loadDataFuncAsync Func<CacheSettings, CancellationToken, Task<TData>>

Function accepting cache settings and a cancellation token and returning the asynchronous data loading task.

settings CacheSettings

Settings configuring the cache.

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task<TData>

Returns a task returning the loaded data.

Type Parameters

TData

Type of data to be loaded.

Remarks

Based on the settings the data is stored into cache. If Cached is set to false, no caching is performed and fresh data are retrieved.

If both progressive load is allowed (both AllowProgressiveCaching and CMS.Helpers.CacheHelper.ProgressiveCaching are set to true), the fresh data retrieval is subject to progressive load (i.e. only one threads executes the load and the result is distributed to other waiting threads). Otherwise, the loadDataFuncAsync is always executed.

No assumptions must be made on which thread executes the loadDataFuncAsync. When multiple callers load the same item at the same time, only the function of the first caller is executed, on that caller's execution context (e.g. its ambient database connection scope), and its result is shared with the other callers.

The cancellationToken cancels only the waiting of the current caller. The progressive load continues for other waiting callers and the token passed to loadDataFuncAsync is cancelled only when all callers waiting for the load have been cancelled. The caller whose function is executed stops waiting only when the load ends, because the load runs on that caller's execution context (e.g. its ambient database connection scope) and may use the state captured by the function. If that caller is cancelled, it throws System.OperationCanceledException once the load ends, so a function that does not honor its token keeps it waiting until the function completes. Always use the token passed to loadDataFuncAsync within the function, never the cancellationToken. When neither caching nor progressive caching is enabled, the function is executed directly with the cancellationToken. A subsequent call does not wait for a cancelled load and starts a new one, so a function that does not honor its token can run twice for the same item. A result of a load that completes after all its callers have been cancelled is not stored in the cache, so it does not overwrite data stored by a newer load (except for a narrow race when the load completes at the very moment it is abandoned).

Exceptions

ArgumentNullException

Thrown when loadDataFuncAsync or settings is null.

InvalidOperationException

Thrown when multiple threads are loading the same item (identified by CacheItemName) with incompatible TData.

LoadAsync<TData>(Func<CacheSettings, Task<TData>>, CacheSettings)

Asynchronously loads data. If multiple threads are loading the same data, only one thread executes the load, stores the result into cache and the result is distributed to other waiting threads. If an exception is thrown during loadDataFuncAsync execution, the exception is distributed as well. No data are cached in such a case.

Task<TData> LoadAsync<TData>(Func<CacheSettings, Task<TData>> loadDataFuncAsync, CacheSettings settings)

Parameters

loadDataFuncAsync Func<CacheSettings, Task<TData>>

Function accepting cache settings and returning the asynchronous data loading task.

settings CacheSettings

Settings configuring the cache.

Returns

Task<TData>

Returns a task returning the loaded data.

Type Parameters

TData

Type of data to be loaded.

Remarks

Based on the settings the data is stored into cache. If Cached is set to false, no caching is performed and fresh data are retrieved.

If both progressive load is allowed (both AllowProgressiveCaching and CMS.Helpers.CacheHelper.ProgressiveCaching are set to true), the fresh data retrieval is subject to progressive load (i.e. only one threads executes the load and the result is distributed to other waiting threads). Otherwise, the loadDataFuncAsync is always executed.

No assumptions must be made on which thread executes the loadDataFuncAsync. When multiple callers load the same item at the same time, only the function of the first caller is executed, on that caller's execution context (e.g. its ambient database connection scope), and its result is shared with the other callers.

The function must not use a cancellation token of the caller, as its load is shared by all callers waiting for the same item. Use the overload accepting a cancellation token when the load needs to be cancellable.

Exceptions

ArgumentNullException

Thrown when loadDataFuncAsync or settings is null.

InvalidOperationException

Thrown when multiple threads are loading the same item (identified by CacheItemName) with incompatible TData.

Load<TData>(Func<CacheSettings, TData>, CacheSettings)

Loads data. If multiple threads are loading the same data, only one thread executes the load, stores the result into cache and the result is distributed to other waiting threads. If an exception is thrown during loadDataFunc execution, the exception is distributed as well. No data are cached in such a case.

TData Load<TData>(Func<CacheSettings, TData> loadDataFunc, CacheSettings settings)

Parameters

loadDataFunc Func<CacheSettings, TData>

Function accepting cache settings and returning the data.

settings CacheSettings

Settings configuring the cache.

Returns

TData

Type Parameters

TData

Type of data to be loaded.

Remarks

Based on the settings the data is stored into cache. If Cached is set to false, no caching is performed and fresh data are retrieved.

If both progressive load is allowed (both AllowProgressiveCaching and CMS.Helpers.CacheHelper.ProgressiveCaching are set to true), the fresh data retrieval is subject to progressive load (i.e. only one threads executes the load and the result is distributed to other waiting threads). Otherwise, the loadDataFunc is always executed.

The internals of the method are based on CachedSection<TData>.

Exceptions

ArgumentNullException

Thrown when loadDataFunc or settings is null.