Comment on page
Cache
The cache policies that this application uses.
Any object that can be used as storage, valid values can be
{}
, window.localStorage
, window.sessionStorage
or any class that can be used as a key/value storage.Create a cache instance that can be used in the SDK.
const myCache = new Cache(window.localStorage)
Attempt to get a cached value based on a key, it is not found in the cache, create it from the callback function.
The callback function can be asynchronous.
const value = await myCache.cache('key', () => 'value')
Remove a key or a cache group from the cache.
key
must be a string that matches the cache key or a regular expression;
myCache.invalidate('key')
// ... or
myCache.invalidate(/^key/)
Add a new cache policy to the cache object.
key
must be a string that matches the cache key or a regular expression;lifespan
specifies for how long the cache object should be valid in seconds;
myCache.addPolicy('key', 300) // keep the object for 5 minutes
// ... or
myCache.addPolicy(/^key/, 300) // the same length as above but as a regex
Use a specific cache object for the global cache.
cacheBuilder
must be a function that returnsCache
;
Cache.use(() => myCache)
// ... or
Cache.use(() => new Cache(window.sessionStorage)
.addPolicy(/^key/, 300)
.addPolicy('other', 5))
Get the global cache object.
const globalCache = Cache.get()
Cache something globally.
const value = await cache('key', () => 'value')
Invalidate something from global cache.
Escape a variable to be used in a regex.
const value = '/-/' // this cannot be used in a regex without modifying it
const escapedValue = escapePoliciyPart(value)
Last modified 1yr ago