redis-mongodb

0.1.8 • Public • Published

redis caching with mongodb

setup

To configure redis and MongoDB do

const rm = require('redis-mongodb');
 
rm({
  url: 'example.com',
  dbName: 'anything'
}, {
    // host: '/tmp/redis.sock', //unix domain
    // host: '127.0.0.1', //can be IP or hostname
    port: 6379,
    maxRetries: 10, //reconnect retries, default -1 (infinity)
    auth: '', //optional password, if needed
    db: 0, //optional db selection
    autoConnect: true, //will connect after creation
    doNotSetClientName: false, //will set connection name (you can see connections by running CLIENT LIST on redis server)
    doNotRunQuitOnEnd: false, //when you call `end()`, driver tries to send `QUIT` command to redis before actual end
});
 

To connect to a remote database click here

Redis handling

isCached

To check if an key is cached in redis you write the following

rm.isCached(key, callback);

cache

To cache data you run the following function

rm.cache(
  key,
  document,
  settings,
  callback
);

Settigns could be the following

{
  forceUpdate: true,
  duration: -1
}

forceUpdate set to true overwrites the data if already cached duration is how long the key should be active, -1 is infinite

uncache

To uncache data the following function is called

rm.unCache(
  key,
  settings,
  callback
);

Settings could be the following

{
  forceDel:  true,
  duration: 10
}

forceDel deletes the key without taking duration into account duration sets a duration for when it should delete

MongoDB manipulation

set data

To set data you run the following function

rm.setData(
  key,
  document,
  data,
  settings,
  callback
);

settings can be

{
    cache: true,
    duration: 100,
    forceUpdate: false
}

cache is whether the data should be cached duration and forceUpdate is only taking into consideration if cache is true duration is how long the key should be cached forceUpdate overwrites any old cache with the same key

get data

to get data you run

rm.getData(
  key,
  document,
  settings,
  callback
);

settings can be

{
    cache: true,
    duration: 100,
    forceUpdate: false
}

cache is whether the data should be cached duration and forceUpdate is only taking into consideration if cache is true duration is how long the key should be cached forceUpdate overwrites any old cache with the same key

update data

to update data run

rm.updateData(
  key,
  doc,
  query,
  data,
  settings
);

settings take

{
    cache: true,
    duration: 100,
    forceUpdate: false
}

cache is whether the data should be cached duration and forceUpdate is only taking into consideration if cache is true duration is how long the key should be cached forceUpdate overwrites any old cache with the same key

query is written in the following way

{
  key: key,
  // Any know data
}

delete data

delete data with

rm.deleteData(
  key,
  document,
  query,
  settings,
  callback
);

settings take

{
    keepCached: true,
    duration: 100,
}

keepCached is if the data being removed should be stored duration is how long the key should be cached. Can't be -1 as it should be deleted

Readme

Keywords

none

Package Sidebar

Install

npm i redis-mongodb

Weekly Downloads

0

Version

0.1.8

License

ISC

Unpacked Size

27.1 kB

Total Files

19

Last publish

Collaborators

  • hardekerlis