项目作者: saidM

项目描述 :
:post_office: Blazing fast in-memory caching library for Node.js
高级语言: JavaScript
项目地址: git://github.com/saidM/hoard.js.git
创建时间: 2017-10-20T23:58:57Z
项目社区:https://github.com/saidM/hoard.js

开源协议:MIT License

下载


hoard.js

Build Status Coverage Status NPM Downloads license XO code style

Blazing fast in-memory caching library for Node.js.

Installation

  1. $ npm install hoard.js

Usage

The first step if to require the module:

  1. const cache = require('hoard.js')

cache.set(key, value, ttl = null)

Stores an item in the cache. You can specify the time to live (in seconds).

  1. // won't expire
  2. cache.set('foo', 'bar')
  3. .then(data => console.log(data)) // 'bar'
  4. // will expire in 60 seconds
  5. cache.set('me', 'Hi this is me', 60)
  6. .then(data => console.log(data)) // 'Hi this is me'

cache.get(key)

Retrieves an item from the cache. It resolves the promise if the item was found or rejects the promise if the item is not present in the cache or has expired.

  1. cache.get('foo')
  2. .then(data => console.log(data)) // bar
  3. cache.get('unknown')
  4. .catch(err => console.error(err)) // ITEM_NOT_FOUND

cache.incr(key)

Increments the value of the given key. Will reject the promise if the value is not of number type.

  1. cache.set('age', 24)
  2. .then(() => cache.incr('age'))
  3. .then(data => console.log(data)) // 25
  4. cache.set('foo', 'bar')
  5. .then(() => cache.incr('foo'))
  6. .catch(err => console.error(err)) // ITEM_IS_NOT_A_NUMBER

cache.decr(key)

Decrements the value of the given key. Will reject the promise if the value is not of number type.

  1. cache.set('age', 24)
  2. .then(() => cache.decr('age'))
  3. .then(data => console.log(data)) // 23
  4. cache.set('foo', 'bar')
  5. .then(() => cache.decr('foo'))
  6. .catch(err => console.error(err)) // ITEM_IS_NOT_A_NUMBER

cache.del(key)

Deletes an item from the cache. If the item was successfully deleted, the promise resolves with the item value, otherwise it rejects the promise.

  1. cache.del('foo')
  2. .then(data => console.log(data)) // 'bar'

cache.clear()

Deletes all items from the cache.

  1. cache.clear()
  2. .then(() => console.log('Cache is now empty'))

Licence

MIT