Table of Contents

Interface ICacheService

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

Experimental! Cache service to help your code cache data.

It does quite a bit of magic, for example:

  • scope the cache to the current App, so a key "data" will not bleed to other apps
  • change the key when the data is for the current user only
  • ...and more.

It's not yet fully documented.

[InternalApi_DoNotUse_MayChangeWithoutNotice("WIP v17.09")]
public interface ICacheService

Remarks

Basic use will just use a string key. The internal key will be more complex, while advanced use would first create the specs using CreateSpecs(string, NoParamOrder, string, bool?) and then use those specs for all operations.

Methods

Contains(string)

Check if the cache contains data for the given key.

bool Contains(string key)

Parameters

key string

Returns

bool

Contains(ICacheSpecs)

Check if the cache contains data for the given specs.

bool Contains(ICacheSpecs specs)

Parameters

specs ICacheSpecs

Returns

bool

Contains<T>(string)

Check if the cache contains data of specified type for the given key.

bool Contains<T>(string key)

Parameters

key string

Returns

bool

Type Parameters

T

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>(string, NoParamOrder, Func<T>)

Get or set data in the cache for the given key, with optional generation and specs-tweaking.

T GetOrSet<T>(string key, NoParamOrder protector = default, Func<T> generate = null)

Parameters

key string
protector NoParamOrder

see Convention: Named Parameters

generate Func<T>

Returns

T

Type Parameters

T

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>(string, NoParamOrder, T)

Get data from the cache of the given type for the given key, with optional fallback.

T Get<T>(string key, NoParamOrder protector = default, T fallback = default)

Parameters

key string
protector NoParamOrder

see Convention: Named Parameters

fallback 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(string)

Remove a cache entry.

object Remove(string key)

Parameters

key string

Returns

object

The object if it was in the cache, otherwise null.

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>(string, T, NoParamOrder)

Set a value in the cache.

void Set<T>(string key, T value, NoParamOrder protector = default)

Parameters

key string
value T
protector NoParamOrder

see Convention: Named Parameters

Type Parameters

T

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>(string, out T)

Try to get data of the specified type from the cache for the given key.

bool TryGet<T>(string key, out T value)

Parameters

key string
value T

Returns

bool

true if found, false if not found

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