Giter Site home page Giter Site logo

cacheswitcher's Introduction

cacheswitcher

This library is a wrapper around https://github.com/groupon/node-cached and https://github.com/cayasso/cacheman-redis, it makes easy to switch from one to the other with configuration. It also make all APIs the same, same methods, same parameters. Not all APIs are wrapped but the common ones. It also add the Promise style on all the method

Supported APIs

  • set which set a value
    • key: string as the unique key to set
    • value: any type that you want to store
    • ttl: string that represent the time to live like 3d -> 3 days, 1h -> 1 hour ...
  • get
    • key: string as the unique key to retrieve
  • unset
    • key: string as the unique key to remove

Install

npm install cacheswitcher

usage

Simple memory cache

var cacheswitcher = require('cacheswitcher')('memory');
cacheswitcher.set('mykey',12345).then(function(){
    cacheswitcher.get('mykey').then(function(data){
        console.log(data);
        cacheswitcher.unset('mykey').then(function(){
            console.log('Clean');
        });
    })
})

Redis cache

var cacheswitcher = require('cacheswitcher')('redis',{server:'127.0.0.1',port:6379});
cacheswitcher.set('mykey',12345).then(function(){
    cacheswitcher.get('mykey').then(function(data){
        console.log(data);
        cacheswitcher.unset('mykey').then(function(){
            console.log('Clean');
        });
    })
})

Redis cache with ttl

var cacheswitcher = require('cacheswitcher')('redis',{server:'127.0.0.1',port:6379});
cacheswitcher.set('mykey',12345,'3d').then(function(){
    //3 days later
    cacheswitcher.get('mykey').then(function(data){
        console.log(data); // -> undefined
        cacheswitcher.unset('mykey').then(function(){
            console.log('Clean');
        });
    })
})

Constructor

  • type: string like memory | redis
  • opt: Object with following properties
    • server: only for redis
    • port: only for redis
    • name: only for memory namespace

Test

npm test

cacheswitcher's People

Contributors

krysalead avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.