Home Reference Source
public class | source

CacheBase

Our base Cache implementation
Extend this class with new implementations to create different cache types (in-memory, database, file system etc.)

Constructor Summary

Public Constructor
public

constructor(options: Object)

Member Summary

Public Members
public

Create a stack containing all failed cache sessions

public

memCache: {}

public

Setting the memory options

public

Create a stack and wait before every caching session is finished

Method Summary

Public Methods
public

async get(key: string, getFullObject: boolean): Object | undefined

Get a cached object

public

async getKeys(prefix: string): array<string>

Get an array of all the cached keys matching the supplied prefix

public

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

Set a key in our cache

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

Private Methods
private abstract

async _del(key: string)

Internal operation to delete a key

private abstract

async _get(key: string): Object | undefined

Internal implementation of Get()

private abstract

async _getKeys(prefix: string)

Internal implementation of getKeys()

private abstract

async _set(key: string, object: Object)

Internal implementation of Set()

Public Constructors

public constructor(options: Object) source

Params:

NameTypeAttributeDescription
options Object
options.useMemoryCache boolean
  • optional
  • default: true

Use an in-memory layer on top of this cache Avoid hitting databases too often Not useful if using any distributed setup where memory will be out-of-sync between processes

options.memoryCacheTimeout number | null
  • optional
  • default: null

Timeout for in-memory cache values Default is null, which will use the incoming ttl values for each key

Public Members

public failedCacheWraps: {} source

Create a stack containing all failed cache sessions

public memCache: {} source

public memoryLayerEnabled: * source

Setting the memory options

public pendingCacheWraps: {} source

Create a stack and wait before every caching session is finished

Public Methods

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

Get a cached object

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

getFullObject boolean
  • optional

Get the full cache entry, including expiry time, even if expired

Return:

Object | undefined

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

public async getKeys(prefix: string): array<string> source

Get an array of all the cached keys matching the supplied prefix

Params:

NameTypeAttributeDescription
prefix string
  • optional
  • default: ''

Return:

array<string>

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

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:

*

Private Methods

private abstract async _del(key: string) source

Internal operation to delete a key

Params:

NameTypeAttributeDescription
key string

Key name to delete

private abstract async _get(key: string): Object | undefined source

Internal implementation of Get()

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

private abstract async _getKeys(prefix: string) source

Internal implementation of getKeys()

Params:

NameTypeAttributeDescription
prefix string

private abstract async _set(key: string, object: Object) source

Internal implementation of Set()

Params:

NameTypeAttributeDescription
key string

Unique key name for this cache entry

object Object

Data to be set