Interface ICacheService
Service on Kit.Cache to help your code cache data.
[PublicApi]
public interface ICacheService
Remarks
It does quite a bit of magic, for example:
- scope the cache to the current App, so a key like
main-listwill not bleed to other apps - help invalidate the cache if the app data changes
- vary the cache by specific parameters such as the
categoryin the URL. - vary the cache by the current user only
- ...and more.
In most cases, you'll
- start by creating ICacheSpecs using a call to CreateSpecs(string, NoParamOrder, string?, bool?)
- use a fluid API on the specs to determine what you want, like VaryByPageParameters(string?, NoParamOrder, bool), VaryByUser() WatchAppData(NoParamOrder) or just set different expiry options.
- Then use these specs to either check if it Contains(ICacheSpecs) or use TryGet<T>(ICacheSpecs, out T?) or GetOrSet<T>(ICacheSpecs, NoParamOrder, Func<T>?)
History
- Was in internal beta since v17.09
- Released v19.01
- Requires the SmartDataCache feature (Patron Perfectionist); if not enabled, will work without caching anything.
Methods
Contains(ICacheSpecs)
Check if the cache contains data for the given specs.
bool Contains(ICacheSpecs specs)
Parameters
specsICacheSpecs
Returns
Contains<T>(ICacheSpecs)
Check if the cache contains data of specified type for the given specs.
bool Contains<T>(ICacheSpecs specs)
Parameters
specsICacheSpecs
Returns
Type Parameters
T
CreateSpecs(string, NoParamOrder, string?, bool?)
Create cache specs for a specific key and optional segment.
This is used for complex setups where the same specs will be reused for multiple operations.
ICacheSpecs CreateSpecs(string key, NoParamOrder protector = default, string? regionName = null, bool? shared = null)
Parameters
keystringThe main cache key (name) to use. It will be extended internally, to prevent collisions, so it can be fairly short.
protectorNoParamOrderregionNamestringa cache region to segment the cache into multiple regions
sharedbool?If set to
trueit will make this key available on other apps which access the data with allApps =true. By default, each app has its own region, preventing key collisions between apps.
Returns
GetOrSet<T>(ICacheSpecs, NoParamOrder, Func<T>?)
Get or set data in the cache for the given specs, with optional generation.
T? GetOrSet<T>(ICacheSpecs specs, NoParamOrder protector = default, Func<T>? generate = null)
Parameters
specsICacheSpecsprotectorNoParamOrdergenerateFunc<T>
Returns
- T
Type Parameters
T
Get<T>(ICacheSpecs, NoParamOrder, T?)
Get data from the cache of the given type for the given specs, with optional fallback.
T? Get<T>(ICacheSpecs specs, NoParamOrder protector = default, T? fallback = default)
Parameters
specsICacheSpecsprotectorNoParamOrderfallbackT
Returns
- T
Type Parameters
T
Remove(ICacheSpecs)
Remove a cache entry.
object? Remove(ICacheSpecs key)
Parameters
keyICacheSpecs
Returns
- object
The object if it was in the cache, otherwise null.
Set<T>(ICacheSpecs, T, NoParamOrder)
Set a value in the cache.
void Set<T>(ICacheSpecs specs, T value, NoParamOrder protector = default)
Parameters
specsICacheSpecsvalueTprotectorNoParamOrder
Type Parameters
T
TryGet<T>(ICacheSpecs, out T?)
Try to get data of the specified type from the cache for the given specs.
bool TryGet<T>(ICacheSpecs specs, out T? value)
Parameters
specsICacheSpecsvalueT
Returns
- bool
trueif found,falseif not found
Type Parameters
T