An abstract object cache interface
npm install abstract-cacheabstract-cache is a module that provides a common interface to multiple
caching strategies. It allows for requiring abstract-cache everywhere while
defining the strategy via a simple configuration.
abstract-cache is heavily inspired by the excellent [Catbox][catbox]. The
decision to create abstract-cache was predicated on a desire to require
implementing clients accept previously established connections in addtion to
accepting configuration to create their own connections. It also seeks to
allow using either the callback or async/await style of asynchronous
programming.
[catbox]: https://npm.im/catbox
+ abstract-cache-mongo
+ abstract-cache-redis
The following example uses abstract-cache's included in-memory cache. Of note,
is that the included in-memory cache is actually callback based.
``js
const cache = requre('abstract-cache')({useAwait: true})
async function doSomething () {
const val = await cache.get('foo')
console.log(val)
}
cache.set('foo', 'foo', 100)
.then(doSomething)
.catch(console.error)
`
This example shows instantiating abstract-cache with a specific store that
relies upon a connection to a remote system. In this example we are supplying
an already existing connection to the remote system; all abstract-cache
compliant clients must support this.
`js
const cache = require('abstract-cache')({
useAwait: true,
driver: {
name: 'abstract-cache-redis',
options: {
client: require('redis')({url: 'some.redis.url'})
}
}
})
async function doSomething () {
const val = await cache.get('foo')
console.log(val)
}
cache.set('foo', 'foo', 100)
.then(doSomething)
.catch(console.error)
`
abstract-client accepts an options object with the following properties:
+ useAwait (Default: false): designate that the resulting cache clientasync/await
should use functions. When false, every method accepts acallback(err, result)
standard .client
+ (Default: undefined): an already instantiated strategy client.useAwait
In combination with the client can be wrapped accordingly. Specifyingclient
a superceeds the driver configuration.driver
+ :name
* (Default: undefined): specifies the implementing strategy tooptions
load. The default value results in the buil-in in-memory strategy being
loaded -- this is not recommended for production environments.
* (Default: {}): an options object to pass to the strategy
while loading. The strategy should describe this object.
The included in-memory client is available as:
`js`
const memclientFactory = require('abstract-cache').memclient
It accepts an options object:
+ segment (Default: abstractMemcache): the default segment in which to storemaxItems
items.
+ (Default: 100000): the maximum number of items to keep in the
cache. The backing is an LRU cache with an upper bound.
All implementing strategies must implement the protocol described in this
section.
1. The module should export a factory function (optionsObject) {}.optionsObject
1. Accept an existing connection to data stores via the .key
1. Manage connections created by itself.
1. In all cases where a is required, the key may be a simple string,{id: 'name', segment: 'name'}
or it may be an object of the format . It isawait
up to the implementing strategy to decide how to handle these keys.
1. The factory function should return an object (client) that has the following
methods and properties:
* (boolean property): true indicates that the strategy's methodsasync
are all functions. If false, all methods must have acallback(err, result)
as the last parameter.delete(key[, callback])
* : removes the specified item from the cache.get(key[, callback])
* : retrieves the desired item from the cache. Theitem
returned item should be a deep copy of the stored value to prevent alterations
from affecting the cache. The result should be an object with the properties:
+ : the item the user cached.stored
+ : a Date, in Epoch milliseconds, indicating when the itemttl
was stored.
+ : the remaining lifetime of the item in the cache (milliseconds).has(key[, callback])
* : returns a boolean result indicating if the cachekey
contains the desired .set(key, value, ttl[, callback])
* : stores the specified value in thekey
cache under the specified for the time ttl in milliseconds.start([callback])
* (optional): clients that require extra initialization,async
e.g. to start a database connection, may export this method. When present,
this method must be invoked by the user before any other method. This
method may be an function at the discretion of the implementor.stop([callback])
* (optional): required when start() is present. Thisstart()
should shutdown any connections/processes started via . It isasync` function at the discretion of the implementor.
left to the user to invoke this method in their shutdown procedure. This
method may be an