The default storage. Does no caching at all.
import cacheNull from 'gw2api-client/build/cache/null'
api.cacheStorage(cacheNull())
Caches the data using a basic in-memory storage.
Options:
gcTick
(optional) - How often the garbage collection should clean out expired data (in ms). Defaults to5 * 60 * 1000
.
import cacheMemory from 'gw2api-client/build/cache/memory'
const options = {
gcTick: 5 * 60 * 1000
}
api.cacheStorage(cacheMemory(options))
Caches the data in-memory but saves a persistent version into IndexedDB to hydrate the cache from on page load.
Options:
storageKey
(optional) - The cache key. Defaults togw2api-cache
.gcTick
(optional) - How often the garbage collection should clean out expired data (in ms). Defaults to5 * 60 * 1000
.persistDebounce
(optional) - How much time has to pass between the API request and the persisting into storage (in ms). Defaults to3 * 1000
.storageEngine
- (optional) - A storage engine, with the same interface asidb-keyval
import cacheBrowserStorage from 'gw2api-client/build/cache/browser'
const options = {
gcTick: 3 * 60 * 1000
}
api.cacheStorage(cacheBrowserStorage(options))
Caches the data using Redis.
Options:
redis
- An instance of node_redisprefix
(optional) - The prefix for the cache keys. Defaults togw2api-
.
import redis from 'redis'
import cacheRedis from 'gw2api-client/build/cache/redis'
const options = {
redis: redis.createClient(),
prefix: 'optional-prefix-'
}
api.cacheStorage(cacheRedis(options))
You can write a custom storage to fit your architecture. It has to be a function which takes a configuration object:
function cacheCustom (config) {
// Do configuration things with the `config` object ...
// Return an object with the implementations
return {
get: (key) => ...,
...
}
}
api.cacheStorage(cacheCustom({foo: 'bar'}))
This function has to return an object containing implementations of the following functions, which all have to return a Promise
object.
get(key)
- Gets a single value by key. Has to resolve tonull
if the value does not exist or is expired.mget([key, key, ...])
- Gets multiple values by keys. Resolves an array. Missing and expired elements have to be set tonull
. Has to maintain the order of keys when resolving into values.set(key, value, expiresInSeconds)
- Sets a single value by key.mset([[key, value, expiresInSeconds], ...])
- Sets multiple values.flush()
- Clears the cache data (only needed for tests).