node package manager

mash

Mash

NPM version Build status Test coverage Dependency Status License Downloads Gittip

Use MongoDB as a key/value store. This is useful only for those who already use MongoDB and don't want to add another layer to their stack or need to cache MongoDB-specific things like ObjectIDs.

API

Documents are stored as:

{
  "_id": "<key>",
  "value": "<value>",
  "expires": "<Date>",
  "created": "<Date>"
}

var cache = new Mash(options)

Create a new mash instance. Options are:

  • collection - the MongoDB collection to use
  • maxAge - default expiration age

cache.maxAge(ms)

Set the default max age.

cache.collection = collection

Set the MongoDB collection to use. Does not support capped collections.

cache.set(key, value, [maxAge]).then( doc => )

Set the value. Can optionally override the default maxAge.

Optionally you can chain:

  • .new() - return the doc
  • .w('majority') - a write concern

cache.get(key, [maxAge]).then( doc => )

Retrieve the cached value if one exists. Optionally set a maxAge that overrides the one set by .set().

Optionally you can chain:

  • .readPreference('nearest') - read preference

cache.evict(key).then( => )

Evict all documents based on a key, which could also be a MongoDB expression.

Optionally you can chain:

  • .w('majority') - a write concern

cache.ensureIndex().then( => )

Ensure the index on the curret collection. Converts it into a TTL collection.