mash

MongoDB Key/Value Cache

npm install mash
8 downloads in the last week
18 downloads in the last month

Mash Build Status

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.

API

Documents are stored as:

{
  "_id": key,
  "value": value,
  "expire": Date
}

mash(key, maxAge || 30000)

Create a new cache object with a given key and a maxAge in milliseconds defaulting to 30 seconds.

var cache = mash('some key')

mash().get(callback)

Retrieve the cache document if one exists.

mash().set(value [, callback])

Save the value with the associated key.

mash().query(fn)

Creates a query function that you wish to cache. The only argument accepted by fn should be a callback.

mash().update([callback])

Updates the cache with the previously set query function.

mash().end(callback)

If a cache exists, retrieves the document. If one does not, executes the query function, returns it, and caches its result.

mash().evict([callback])

Delete the cache.

mash.maxAge

Set the default maxAge argument for all mash instances.

mash.onError

Set the default error handler if a callback is not set.

mash.evict(key [, callback])

Delete all documents that match the given key.

MongoDB Settings

Key types

mash is agnostic to the type of key used. You can use strings, arrays, or objects. Thus, if you use strings, you can also evict using regular expressions.

Index

You'd want to create a secondary index that deletes documents based on expire:

db.cache.ensureIndex({
  expire: -1
}, {
  expireAfterSeconds: 0
})

This index will expire all documents where doc.expire > new Date(), keeping your collection as small as possible.

License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you