Socket
Socket
Sign inDemoInstall

asyncache

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    asyncache

Transparently cache your asynchronous get functions.


Version published
Weekly downloads
2
Maintainers
1
Install size
40.4 kB
Created
Weekly downloads
 

Readme

Source

asyncache

Transparently cache your asynchronous get functions. Build Status

Usage

asyncGetFunction = cache.remember(asyncGetFunction)
asyncDeleteFunction = cache.forget(asyncDeleteFunction)

Example:

var cache = require('asyncache')();
// implement cache methods
cache.set = function(key, value, ttl, cb) { /* ... */ };
cache.get = function(key, cb) { /* ... */ };
cache.del = function(key, cb) { /* ... */ };

// sample async function
var getUserDetails = function(userId, cb) {
  database.get(userId, function(err, user) {
    cb(err, user);
  });
};

getUserDetails = cache.remember(getUserDetails);

// this function will fetch the result from the db
getUserDetails(19384, function(err, user) {
  // now the result will be fetched from cache
  getUserDetails(19384, function(err, user) {
    // faster
  });
});

For more complete implementations checkout the /examples folder. For more details read the source code, Luke.

Note

Be aware that cache.remember() and cache.forget() are not atomic unless you implement that by yourself inside cache.set() and cache.del() (which are used internally be the previous two). That means that unless you use some kind of lock you might end up with old data in your cache.

Tests

npm test

License

MIT

Keywords

FAQs

Last updated on 21 Jul 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc