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-list
will not bleed to other apps - help invalidate the cache if the app data changes
- vary the cache by specific parameters such as the
category
in 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
specs
ICacheSpecs
Returns
Contains<T>(ICacheSpecs)
Check if the cache contains data of specified type for the given specs.
bool Contains<T>(ICacheSpecs specs)
Parameters
specs
ICacheSpecs
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
key
stringThe main cache key (name) to use. It will be extended internally, to prevent collisions, so it can be fairly short.
protector
NoParamOrderregionName
stringa cache region to segment the cache into multiple regions
shared
bool?If set to
true
it 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
specs
ICacheSpecsprotector
NoParamOrdergenerate
Func<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
specs
ICacheSpecsprotector
NoParamOrderfallback
T
Returns
- T
Type Parameters
T
Remove(ICacheSpecs)
Remove a cache entry.
object Remove(ICacheSpecs key)
Parameters
key
ICacheSpecs
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
specs
ICacheSpecsvalue
Tprotector
NoParamOrder
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
specs
ICacheSpecsvalue
T
Returns
- bool
true
if found,false
if not found
Type Parameters
T