Skip to content

TwoLevelCacheExtensions.GetLocalStoreOnly<TItem> method (1 of 2)

Tries to read a value from local cache, and if it is not found there, or any of the groups it depends on is expired, produces the value by calling a loader function and adds it to local cache. The distributed cache is only used for the generations of the groups. An item can also expire about 5 seconds later on other servers, as the generations are cached locally for that long.

public static TItem? GetLocalStoreOnly<TItem>(this ITwoLevelCache cache, string cacheKey, 
    TimeSpan localExpiration, IReadOnlyList<string> groupKeys, Func<TItem?>? loader)
    where TItem : class
parameter description
TItem Data type
cache Two level cache
cacheKey The item key for local cache
localExpiration Local expiration
groupKeys Group keys that hold the generations. Expiring any of them expires the item.
loader The delegate that will be called to generate value, if not found in local cache or the found item is expired.

Exceptions

exception condition
ArgumentException groupKeys is empty.

See Also


TwoLevelCacheExtensions.GetLocalStoreOnly<TItem> method (2 of 2)

Tries to read a value from local cache. If it is not found there produces value by calling a loader function and adds the value to local cache for a given expiration time. By using a generation (item version) key, all items on local cache that are members of this group can be expired at once.

public static TItem? GetLocalStoreOnly<TItem>(this ITwoLevelCache cache, string cacheKey, 
    TimeSpan localExpiration, string groupKey, Func<TItem?>? loader)
    where TItem : class
parameter description
TItem Data type
cache Two level cache
cacheKey The item key for local and distributed cache
localExpiration Local expiration
groupKey Group key that will hold generation (version). Can be used to expire all items that depend on it. This can be a table name. When a table changes, you change its version, and all cached data that depends on that table is expired.
loader The delegate that will be called to generate value, if not found in local cache, or distributed cache, or all found items are expired.

Remarks

The difference between this and Get method is that this one only caches items in local cache, but uses distributed cache for versioning. To not check group generation every time an item is requested, generation number itself is also cached in local cache. Thus, when a generation number changes, local cached items might expire after about 5 seconds. This means that, if you use this strategy in a web farm setup, when a change occurs in one server, other servers might continue to use old local cached data for 5 seconds more. If this is a problem for your configuration, use DistributedCache directly.

See Also