Home Reference Source
import {ScopedCache} from '@alexvv13/tpapi/lib/cache/scopedCache.js'
public class | source

ScopedCache

A wrapper class for accessing the cache.
Prefixes a string before each key to avoid conflicts.

Constructor Summary

Public Constructor
public

constructor(keyPrefix: string, version: number)

Create a new ScopedCache object by passing in the key prefix descired

Member Summary

Public Members
public

cache: *

public

prefix: *

public

version: *

Private Members
private

Method Summary

Public Methods
public

Generate a scoped key by adding our prefix to the incoming key

public

async get(key: string): Object | undefined

Get a cached object

public

async getGlobal(key: string): Object | undefined

Get a cached object from the global cache (skipping the scope prefix)

public

async initCache(): *

Initialise the cache for this scope

public

async set(key: string, value: Object, ttl: Function | number): *

Set a key in our cache

public

async setGlobal(key: string, value: Object, ttl: Function | number): *

Set a key in our global cache, skipping the scoped prefix

public

async wrap(key: string, fn: function, ttl: function | number): *

A helper "wrap" function that will return a cached value if present
This will call the supplied function to fetch it if the value isn't present in the cache

public

async wrapGlobal(key: string, fn: function, ttl: function | number): *

A helper "wrap" function that will return a cached value if present (in the global scope)
This will call the supplied function to fetch it if the value isn't present in the cache

Private Methods
private

async _initCache(): *

Internal cache initialisation

Public Constructors

public constructor(keyPrefix: string, version: number) source

Create a new ScopedCache object by passing in the key prefix descired

Params:

NameTypeAttributeDescription
keyPrefix string
version number
  • optional

Cache version, bump this to invalidate existing cache entries for a scope

Public Members

public cache: * source

public prefix: * source

public version: * source

Private Members

private _initCachePromise: * source

Public Methods

public generateScopedKey(inKey: string): string source

Generate a scoped key by adding our prefix to the incoming key

Params:

NameTypeAttributeDescription
inKey string

Return:

string

Scoped key

public async get(key: string): Object | undefined source

Get a cached object

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

Return:

Object | undefined

Returns the object in the cache, or undefined if not present

public async getGlobal(key: string): Object | undefined source

Get a cached object from the global cache (skipping the scope prefix)

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

Return:

Object | undefined

Returns the object in the cache, or undefined if not present

public async initCache(): * source

Initialise the cache for this scope

Return:

*

public async set(key: string, value: Object, ttl: Function | number): * source

Set a key in our cache

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

value Object
ttl Function | number
  • optional
  • default: 3600000

How long the cache entry should last in milliseconds Can be a number or a function that will return a number Default 1 hour

Return:

*

public async setGlobal(key: string, value: Object, ttl: Function | number): * source

Set a key in our global cache, skipping the scoped prefix

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

value Object
ttl Function | number
  • optional
  • default: 3600000

How long the cache entry should last in milliseconds Can be a number or a function that will return a number Default 1 hour

Return:

*

public async wrap(key: string, fn: function, ttl: function | number): * source

A helper "wrap" function that will return a cached value if present
This will call the supplied function to fetch it if the value isn't present in the cache

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

fn function

Fetch function that will be called if the cache entry is not present

ttl function | number
  • optional

How long the cache entry should last in milliseconds Can be a number or a function that will return a number

Return:

*

public async wrapGlobal(key: string, fn: function, ttl: function | number): * source

A helper "wrap" function that will return a cached value if present (in the global scope)
This will call the supplied function to fetch it if the value isn't present in the cache

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

fn function

Fetch function that will be called if the cache entry is not present

ttl function | number
  • optional

How long the cache entry should last in milliseconds Can be a number or a function that will return a number

Return:

*

Private Methods

private async _initCache(): * source

Internal cache initialisation

Return:

*