Table of Contents

Interface ICacheService

Namespace
ToSic.Sxc.Services
Assembly
ToSic.Sxc.dll

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

  1. start by creating ICacheSpecs using a call to CreateSpecs(string, NoParamOrder, string, bool?)
  2. 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.
  3. 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

bool

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

bool

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 string

The main cache key (name) to use. It will be extended internally, to prevent collisions, so it can be fairly short.

protector NoParamOrder

see Convention: Named Parameters

regionName string

a 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

ICacheSpecs

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 ICacheSpecs
protector NoParamOrder

see Convention: Named Parameters

generate 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 ICacheSpecs
protector NoParamOrder

see Convention: Named Parameters

fallback 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 ICacheSpecs
value T
protector NoParamOrder

see Convention: Named Parameters

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 ICacheSpecs
value T

Returns

bool

true if found, false if not found

Type Parameters

T