Table of Contents

Interface ICacheSpecs

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

Cache Specs contain the definition of what the cached data should depend on, how long it should be cached and how the cache key should be generated. It uses a fluent API to continue adding rules / expirations / dependencies. Internally this is then used to create a cache policy.

[PublicApi]
public interface ICacheSpecs

Remarks

  • Introduced as experimental in v17.09
  • Released in 19.01
  • Enhancing with Enable/Disable in v20.01, especially with various user elevations.

Properties

IsEnabled

Determine if caching is enabled. Default is true.

[WorkInProgressApi("wip v20.00-05")]
bool IsEnabled { get; }

Property Value

bool

Remarks

Internally it will determine if the previously set rules match the current user elevation.

Methods

Disable()

ICacheSpecs Disable()

Returns

ICacheSpecs

Disable(UserElevation)

ICacheSpecs Disable(UserElevation elevation)

Parameters

elevation UserElevation

Returns

ICacheSpecs

Disable(UserElevation, UserElevation)

Disable caching for this data, so it will not be cached. Rarely used.

ICacheSpecs Disable(UserElevation minElevation, UserElevation maxElevation)

Parameters

minElevation UserElevation
maxElevation UserElevation

Returns

ICacheSpecs

Remarks

This rarely makes sense, since it's better to just not add something to the cache in the first place.

WIP v20.01: This is a work in progress, and the implementation may change in future versions.

Enable()

Disable caching for this data, so it will not be cached. Rarely used.

ICacheSpecs Enable()

Returns

ICacheSpecs

Remarks

WIP v20.01: This is a work in progress, and the implementation may change in future versions.

SetAbsoluteExpiration(DateTimeOffset)

Set absolute expiration, alternative is sliding expiration. If neither are set, a sliding expiration of 1 hour will be used.

ICacheSpecs SetAbsoluteExpiration(DateTimeOffset absoluteExpiration)

Parameters

absoluteExpiration DateTimeOffset

Returns

ICacheSpecs

Remarks

If neither absolute nor sliding are set, a sliding expiration of 1 hour will be used. Setting both is invalid and will throw an exception.

SetSlidingExpiration(TimeSpan?, NoParamOrder, int?)

Set sliding expiration, alternative is absolute expiration.

ICacheSpecs SetSlidingExpiration(TimeSpan? timeSpan = null, NoParamOrder protector = default, int? seconds = null)

Parameters

timeSpan TimeSpan?
protector NoParamOrder

see Convention: Named Parameters

seconds int?

time in seconds - if specified, takes precedence - new v20.01

Returns

ICacheSpecs

Remarks

If neither absolute nor sliding are set, a sliding expiration of 1 hour will be used. Setting both is invalid and will throw an exception.

VaryBy(string, int)

Vary the cache by a specific name and value. All cache items where this value is the same, will be considered the same. For example, this could be a category name or something where the data for this category is always the same.

ICacheSpecs VaryBy(string name, int value)

Parameters

name string
value int

Returns

ICacheSpecs

VaryBy(string, string, NoParamOrder, bool)

Vary the cache by a specific name and value. All cache items where this value is the same, will be considered the same. For example, this could be a category name or something where the data for this category is always the same.

ICacheSpecs VaryBy(string name, string value, NoParamOrder protector = default, bool caseSensitive = false)

Parameters

name string
value string
protector NoParamOrder

see Convention: Named Parameters

caseSensitive bool

Returns

ICacheSpecs

VaryByModel(string?, NoParamOrder, bool)

Vary the cache by values in the model, so that each sample has its own cache. Used in Partial-Caching only. WORK-IN-PROGRESS!

[WorkInProgressApi("WIP v20.01")]
ICacheSpecs VaryByModel(string? names = null, NoParamOrder protector = default, bool caseSensitive = false)

Parameters

names string

Names of one or more parameters, comma-separated

protector NoParamOrder

see Convention: Named Parameters

caseSensitive bool

Determines if the value should be treated case-sensitive, default is false

Returns

ICacheSpecs

Remarks

This is only meant for partial razor caching, since that would have a model available. If used elsewhere, it will be ignored.

WIP v20.01

VaryByModule()

Vary the cache by the current module, so that each module has its own cache.

ICacheSpecs VaryByModule()

Returns

ICacheSpecs

VaryByPage()

Vary the cache by the current page, so that each page has its own cache.

ICacheSpecs VaryByPage()

Returns

ICacheSpecs

VaryByPageParameters(string?, NoParamOrder, bool)

Vary the cache by one or more specific page parameter, like ?category=1 or ?category=1&sort=asc. Using this method will only vary the cache by the mentioned parameters and ignore the rest.

ICacheSpecs VaryByPageParameters(string? names = null, NoParamOrder protector = default, bool caseSensitive = false)

Parameters

names string

Names of one or more parameters, comma-separated. If null, all parameters are used, if "", no parameters are used.

protector NoParamOrder

see Convention: Named Parameters

caseSensitive bool

Determines if the value should be treated case-sensitive, default is false

Returns

ICacheSpecs

VaryByParameters(IParameters, NoParamOrder, string?, bool)

Vary the cache by a custom parameters list.

ICacheSpecs VaryByParameters(IParameters parameters, NoParamOrder protector = default, string? names = null, bool caseSensitive = false)

Parameters

parameters IParameters

parameters object

protector NoParamOrder

see Convention: Named Parameters

names string

Names of one or more parameters, comma-separated

caseSensitive bool

Determines if the value should be treated case-sensitive, default is false

Returns

ICacheSpecs

Remarks

This only makes sense when using with the CacheService, but not in output caching. Reason is that in output caching, the pre-flight would not have access to the parameters object and would never result in a cache hit.

VaryByUser()

Vary the cache by current user, so that each user has its own cache.

ICacheSpecs VaryByUser()

Returns

ICacheSpecs

WatchAppData(NoParamOrder)

Depend on the app data, so if any data changes, the cache will be invalidated.

ICacheSpecs WatchAppData(NoParamOrder protector = default)

Parameters

protector NoParamOrder

Returns

ICacheSpecs

WatchAppFolder(NoParamOrder, bool?)

Depend on the app folder, so if any file in the app folder changes, the cache will be invalidated. WIP!

ICacheSpecs WatchAppFolder(NoParamOrder protector = default, bool? withSubfolders = null)

Parameters

protector NoParamOrder

see Convention: Named Parameters

withSubfolders bool?

should it also watch subfolders? default is true

Returns

ICacheSpecs