1.0.2 • Published 8 years ago

bitrixhater-node-cache v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

node-cache

A little cache manager, used in my own projects I did not test it yet, but I think it works

Usage

Install

$ npm install bitrixhater-node-cache --save

Require

Auto-detect preferer cache manager

const cacheManager = require('bitrixhater-node-cache')({
  cacheTime: 0,
  modelName: 'MongoDBCacheManager',
  uri: false
})

RAM cache manager

const cacheManager = new (require('./node-cache')).RAMCacheManager({
  cacheTime: 15
})

MongoDB cache manager (require connected mongoose (or pass uri option to connect))

const cacheManager = new (require('./node-cache')).MongoDBCacheManager({
  cacheTime: 15,
  modelName: 'MongoDBCacheManager',
  uri: 'mongodb://127.0.0.1/MongoDBCacheManager' 
})

Options list

KeyTypeDefault
cacheTimeint0 - infinity, sec
modelNamestring'MongoDBCacheManager'
uristring'mongodb://127.0.0.1/MongoDBCacheManager'

Methods

MethodArgumentsDescription
setkey = string, data = object, cacheTime = intSave variable into the memory, cacheTime == 0 is infinity
getkey = stringGet variable from the memory
removekey = stringRemove variable from the memory
clearFlush storage

Code example

You can find examples in the ./test/test.js file. But if you dont like read a code I wrote examples below only for you, my friend <3

/**
 * Just require it and pass the options. That's it.
 *
 * If lib detect mongoose and if mongoose has been connected to db,
 * you will be use MongoDBCacheManager, otherwise - RAMCacheManager. 
 * If you dont have active mongodb connection and you want to connect,
 * just pass uri option
 */
const cacheManager = require('bitrixhater-node-cache')({
  cacheTime: 0
})

let testResults = {}

cacheManager.set('testKey', ['testValue'])
.then(() => cacheManager.get('testKey')
  .then(testValue => {
    testResults[1] = (testValue[0] === 'testValue' && (testValue.length === 1))
  })
  .catch(function() {
    testResults[1] = false
  })
)

cacheManager.set('testKey', ['testValue'], 0.01)
setTimeout(() => {
  cacheManager.get('testKey')
  .then(testValue => {
    testResults[2] = (testValue[0] === 'testValue' && (testValue.length === 1))
  })
  .catch(function() {
    testResults[2] = false
  })
}, 5)

cacheManager.set('testKey', ['testValue'], 0.005)
setTimeout(() => {
  cacheManager.get('testKey')
  .then(testValue => {
    testResults[3] = (!testValue)
  })
  .catch(function() {
    testResults[3] = false
  })
}, 10)

console.log(testResults)

Tests

$ npm test

Todos

  1. Trash cleaner
  2. Other types of database, include redis, memcached..
  3. Test it

Contributions

PRs and issues are open. But just use only 2 spaces, not tabs for PRs

License

MIT

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago