GithubHelp home page GithubHelp logo

obcache's Introduction

ObCache

ObCache is an Object caching module for node.js. Objects are cached in memory, via a backing store.

Currently 2 stores are supported.

  • Memory
  • Redis

Use Redis for persistent caches.

Usage

var obcache = require('obcache');

// create a cache with max 10000 items and a TTL of 300 seconds
var cache = new obcache.Create({ max: 10000, maxAge: 300 });

The max parameter above indicates the maximum keys that can be cached. If your values are variable sized and you want a finer control on cache memory usage, specify maxSize instead. If no max is specified, max keys supported are Inifinity, and size of the cache will be governed by maxAge alone.

Then wrap your original function like this

var wrapper = cache.wrap(original);

Now call the wrapper as you would call the original

wrapper(arg1,arg2...argn,function(err,res) {
  if (!err) {
     // do something with res
  }
});

API

obcache.Create

Creates a new cache and returns it

cache.wrap

Wraps a given function and returns a cached version of it. Functions to be wrapped must have a callback function as the last argument. The callback function is expected to recieve 2 arguments - err and data. data gets stored in the cache. Sometimes, you may want to use a different value of this inside the caller function. cache.wrap has an optional second argument which becomes the this object when calling the original function.

The first n-1 arguments are used to create the key. Subsequently, when the wrapped function is called with the same n arguments, it would lookup the key in LRU, and if found, call the callback with the associated data. It is expected that the callback will never modified the returned data, as any modifications of the original will change the object in cache.

cache.warmup

Warmup the cache.

The first argument is the cache function, and the last argument is the value.

E.g.

var myfunc = cache.wrap(function(q, r, cb) {
  ...
});

cache.warmup(myfunc, q, r, 123);

cache.invalidate

Invalidate the cache contents. Subsequent calls will trigger a new fetch.

cache.invalidate(myfunc,q,r);

cache.debug

The debug interface exposes 2 functions, register and view. register is used to register a cache for debugging. view is a connect middleware that can be used to view all the registered caches and their data/keys.

obcache's People

Contributors

kunalkukreja avatar niklabh avatar qzaidi avatar sakhtar 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.