Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

async-cache@0.0.2

  • Loading branch information...
commit db3ad82ca5103580e2eedb3391243fd95d7ec13b 1 parent f5a2f91
@isaacs isaacs authored
View
64 node_modules/async-cache/README.md
@@ -0,0 +1,64 @@
+# async-cache
+
+Cache your async lookups and don't fetch the same thing more than
+necessary.
+
+## Example
+
+Let's say you have to look up stat info from paths. But you are ok
+with only looking up the stat info once every 10 minutes (since it
+doesn't change that often), and you want to limit your cache size to
+1000 objects, and never have two stat calls for the same file
+happening at the same time (since that's silly and unnecessary).
+
+You can do this:
+
+```javascript
+var stats = new AsyncCache({
+ // options passed directly to the internal lru cache
+ max: 1000,
+ maxAge: 1000 * 60 * 10,
+ // method to load a thing if it's not in the cache.
+ // key must be unique in the context of this cache.
+ load: function (key, cb) {
+ // the key can be something like the path, or fd+path, or whatever.
+ // something that will be unique.
+ // this method will only be called if it's not already in cache, and will
+ // cache the result in the lru.
+ getTheStatFromTheKey(key, cb)
+ }
+})
+
+// then later..
+stats.get(fd + ':' + path, function (er, stat) {
+ // maybe loaded from cache, maybe just fetched
+})
+```
+
+Except for the `load` method, all the options are passed unmolested to
+the internal [lru-cache](http://npm.im/lru-cache).
+
+### Differences from [lru-cache](http://npm.im/lru-cache)
+
+Since values are fetched asynchronously, the `get` method takes a
+callback, rather than returning the value synchronously.
+
+While there is a `set(k,v)` method to manually seed the cache,
+typically you'll just call `get` and let the load function fetch the
+key for you.
+
+Keys must uniquely identify a single object, and must contain all the
+information required to fetch an object, and must be strings.
+
+## Methods
+
+* `get(key, cb)` If the key is in the cache, then calls `cb(null,
+ cached)` on nextTick. Otherwise, calls the `load` function that was
+ supplied in the options object. If it doesn't return an error, then
+ cache the result. Multiple `get` calls with the same key will only
+ ever have a single `load` call at the same time.
+
+* `set(key, val)` Seed the cache. This doesn't have to be done, but
+ can be convenient if you know that something will be fetched soon.
+
+* `reset()` Drop all the items in the cache.
View
64 node_modules/async-cache/ac.js
@@ -0,0 +1,64 @@
+module.exports = AsyncCache;
+
+var LRU = require('lru-cache');
+
+function AsyncCache(opt) {
+ if (!opt || typeof opt !== 'object') {
+ throw new Error('options must be an object');
+ }
+
+ if (!opt.load) {
+ throw new Error('load function is required');
+ }
+
+ if (!(this instanceof AsyncCache)) {
+ return new AsyncCache(opt);
+ }
+
+ this._opt = opt;
+ this._cache = new LRU(opt);
+ this._load = opt.load;
+ this._loading = {};
+}
+
+AsyncCache.prototype.get = function(key, cb) {
+ if (this._loading[key]) {
+ this._loading[key].push(cb);
+ return;
+ }
+
+ var cached = this._cache.get(key);
+ if (cached) {
+ return process.nextTick(function() {
+ cb(null, cached);
+ });
+ }
+
+ this._loading[key] = [ cb ];
+ this._load(key, function(er, res) {
+ if (!er) this._cache.set(key, res);
+
+ var cbs = this._loading[key];
+ delete this._loading[key];
+
+ cbs.forEach(function (cb) {
+ cb(er, res);
+ });
+ }.bind(this));
+};
+
+AsyncCache.prototype.set = function(key, val) {
+ return this._cache.set(key, val);
+};
+
+AsyncCache.prototype.reset = function() {
+ return this._cache.reset();
+};
+
+AsyncCache.prototype.has = function(key) {
+ return this._cache.get(key);
+};
+
+AsyncCache.prototype.del = function(key) {
+ return this._cache.del(key);
+};
View
36 node_modules/async-cache/package.json
@@ -0,0 +1,36 @@
+{
+ "name": "async-cache",
+ "version": "0.0.2",
+ "description": "Cache your async lookups and don't fetch the same thing more than necessary.",
+ "main": "ac.js",
+ "directories": {
+ "test": "test"
+ },
+ "dependencies": {
+ "lru-cache": "~2.0.0"
+ },
+ "devDependencies": {
+ "tap": "~0.3.0"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/isaacs/async-cache"
+ },
+ "keywords": [
+ "async",
+ "cache",
+ "lru"
+ ],
+ "author": {
+ "name": "Isaac Z. Schlueter",
+ "email": "i@izs.me",
+ "url": "http://blog.izs.me/"
+ },
+ "license": "BSD",
+ "readme": "# async-cache\n\nCache your async lookups and don't fetch the same thing more than\nnecessary.\n\n## Example\n\nLet's say you have to look up stat info from paths. But you are ok\nwith only looking up the stat info once every 10 minutes (since it\ndoesn't change that often), and you want to limit your cache size to\n1000 objects, and never have two stat calls for the same file\nhappening at the same time (since that's silly and unnecessary).\n\nYou can do this:\n\n```javascript\nvar stats = new AsyncCache({\n // options passed directly to the internal lru cache\n max: 1000,\n maxAge: 1000 * 60 * 10,\n // method to load a thing if it's not in the cache.\n // key must be unique in the context of this cache.\n load: function (key, cb) {\n // the key can be something like the path, or fd+path, or whatever.\n // something that will be unique.\n // this method will only be called if it's not already in cache, and will\n // cache the result in the lru.\n getTheStatFromTheKey(key, cb)\n }\n})\n\n// then later..\nstats.get(fd + ':' + path, function (er, stat) {\n // maybe loaded from cache, maybe just fetched\n})\n```\n\nExcept for the `load` method, all the options are passed unmolested to\nthe internal [lru-cache](http://npm.im/lru-cache).\n\n### Differences from [lru-cache](http://npm.im/lru-cache)\n\nSince values are fetched asynchronously, the `get` method takes a\ncallback, rather than returning the value synchronously.\n\nWhile there is a `set(k,v)` method to manually seed the cache,\ntypically you'll just call `get` and let the load function fetch the\nkey for you.\n\nKeys must uniquely identify a single object, and must contain all the\ninformation required to fetch an object, and must be strings.\n\n## Methods\n\n* `get(key, cb)` If the key is in the cache, then calls `cb(null,\n cached)` on nextTick. Otherwise, calls the `load` function that was\n supplied in the options object. If it doesn't return an error, then\n cache the result. Multiple `get` calls with the same key will only\n ever have a single `load` call at the same time.\n\n* `set(key, val)` Seed the cache. This doesn't have to be done, but\n can be convenient if you know that something will be fetched soon.\n\n* `reset()` Drop all the items in the cache.\n",
+ "_id": "async-cache@0.0.2",
+ "_from": "async-cache"
+}
View
64 node_modules/async-cache/test/basic.js
@@ -0,0 +1,64 @@
+var test = require('tap').test;
+var AC = require('../ac.js');
+var fs = require('fs');
+
+test('basic', function(t) {
+ var ac = new AC({
+ max: 1,
+ load: function(key, cb) {
+ fs.stat(key, cb);
+ }
+ });
+
+ var called = 0;
+ var stFirst = null;
+ var stSecond = null;
+
+ ac.get(__filename, afterFirst);
+ function afterFirst(er, st) {
+ if (er) throw er;
+ called ++;
+ stFirst = st;
+ t.pass('called the first one');
+ if (called === 2) next();
+ }
+
+ var expectLoading = {}
+ expectLoading[__filename] = [afterFirst]
+ t.deepEqual(ac._loading, expectLoading);
+
+ ac.get(__filename, afterSecond);
+ function afterSecond(er, st) {
+ if (er) throw er;
+ called ++;
+ stSecond = st;
+ t.pass('called the second one');
+ if (called === 2) next();
+ }
+
+ expectLoading[__filename].push(afterSecond);
+ t.deepEqual(ac._loading, expectLoading);
+
+ function next() {
+ t.equal(stFirst, stSecond, 'should be same stat object');
+ t.deepEqual(ac._loading, {});
+ t.equal(called, 2);
+ ac.get(__filename, function(er, st) {
+ if (er) throw er;
+ t.equal(st, stFirst, 'should be cached stat object');
+ next2();
+ });
+ }
+
+ function next2() {
+ // now make it fall out of cache by fetching a new one.
+ ac.get(__dirname, function(er, st) {
+ if (er) throw er;
+ ac.get(__filename, function(er, st) {
+ if (er) throw er;
+ t.notEqual(st, stFirst, 'should have re-fetched');
+ t.end();
+ });
+ });
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.