Skip to content

Commit 204a712

Browse files
committed
0.0.1 release
1 parent 2a51739 commit 204a712

19 files changed

+1005
-243
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_module
1+
node_modules
2+
coverage

.jshintrc

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
// Settings
3+
"passfail" : false, // Stop on first error.
4+
"maxerr" : 500, // Maximum errors before stopping.
5+
"multistr" : true,
6+
7+
8+
// Predefined globals whom JSHint will ignore.
9+
"browser" : true, // Standard browser globals e.g. `window`, `document`.
10+
11+
"node" : false,
12+
"rhino" : false,
13+
"couch" : false,
14+
"wsh" : true, // Windows Scripting Host.
15+
16+
"jquery" : true,
17+
"prototypejs" : false,
18+
"mootools" : false,
19+
"dojo" : false,
20+
21+
"predef" : [ // Extra globals.
22+
"__dirname",
23+
"Buffer",
24+
"event",
25+
"exports",
26+
"global",
27+
"module",
28+
"process",
29+
"require",
30+
"daisyjs",
31+
"after",
32+
"afterEach",
33+
"before",
34+
"beforeEach",
35+
"context",
36+
"describe",
37+
"it"
38+
],
39+
40+
// Development.
41+
"debug" : false, // Allow debugger statements e.g. browser breakpoints.
42+
"devel" : true, // Allow development statements e.g. `console.log();`.
43+
44+
45+
// EcmaScript 5.
46+
"es5" : true, // Allow EcmaScript 5 syntax.
47+
"strict" : false, // Require `use strict` pragma in every file.
48+
"globalstrict" : false, // Allow global "use strict" (also enables 'strict').
49+
50+
51+
"asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
52+
"laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
53+
"bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
54+
"boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
55+
"curly" : true, // Require {} for every new block or scope.
56+
"eqeqeq" : false, // Require triple equals i.e. `===`.
57+
"eqnull" : false, // Tolerate use of `== null`.
58+
"evil" : false, // Tolerate use of `eval`.
59+
"expr" : false, // Tolerate `ExpressionStatement` as Programs.
60+
"forin" : false, // Tolerate `for in` loops without `hasOwnProperty`.
61+
"immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
62+
"latedef" : true, // Prohibit variable use before definition.
63+
"loopfunc" : true, // Allow functions to be defined within loops.
64+
"maxparams" : 4,
65+
"maxdepth" : 5,
66+
"maxcomplexity" : 8,
67+
"maxstatements" : 40,
68+
"noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
69+
"regexp" : false, // Prohibit `.` and `[^...]` in regular expressions.
70+
"regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
71+
"scripturl" : true, // Tolerate script-targeted URLs.
72+
"shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
73+
"supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
74+
"undef" : true, // Require all non-global variables be declared before they are used.
75+
76+
77+
"newcap" : false, // Require capitalization of all constructor functions e.g. `new F()`.
78+
"noempty" : true, // Prohibit use of empty blocks.
79+
"nonew" : false, // Prohibit use of constructors for side-effects.
80+
"nomen" : false, // Prohibit use of initial or trailing underbars in names.
81+
"onevar" : false, // Allow only one `var` statement per function.
82+
"plusplus" : false, // Prohibit use of `++` & `--`.
83+
"sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
84+
"trailing" : true, // Prohibit trailing whitespaces. (only works if white is 'true')
85+
"white" : true, // Check against strict whitespace and indentation rules.
86+
"indent" : 4,
87+
"unused" : true
88+
}

History.md

Whitespace-only changes.

LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyrights for code authored by MOG Inc. is licensed under the following terms:
2+
3+
MIT License
4+
5+
Copyright (c) 2011 MOG Inc. All Rights Reserved.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to
9+
deal in the Software without restriction, including without limitation the
10+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
sell copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BASE = .
2+
3+
ISTANBUL = ./node_modules/.bin/istanbul
4+
COVERAGE_OPTS = --lines 99 --statements 95 --branches 90 --functions 95
5+
6+
main: lint test
7+
8+
cover:
9+
$(ISTANBUL) cover test/run.js
10+
11+
check-coverage:
12+
$(ISTANBUL) check-coverage $(COVERAGE_OPTS)
13+
14+
test: cover check-coverage
15+
16+
17+
test-cov: cover check-coverage
18+
open coverage/lcov-report/index.html
19+
20+
lint:
21+
./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
22+
./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc
23+
24+
25+
.PHONY: test

README.md

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
node-cashew
2+
======================
3+
4+
# Flexible NodeJS cache module
5+
6+
A cache module for nodejs that allows easy wrapping of functions in cache,
7+
tiered caches, and a consistent interface.
8+
9+
## Features
10+
11+
* Easy way to wrap any function in cache.
12+
* Tiered caches -- data gets stored in each cache and fetched from the highest
13+
priority cache(s) first.
14+
* Use any cache you want, as long as it has the same API.
15+
* 100% test coverage via [mocha](https://github.com/visionmedia/mocha),
16+
[istanbul](https://github.com/yahoo/istanbul), and [sinon](http://sinonjs.org).
17+
18+
19+
## Installation
20+
21+
npm install node-cashew
22+
23+
## Overview
24+
25+
First, node-cashew features the standard functions you'd expect in most caches:
26+
27+
set(key, val, cb)
28+
get(key, cb)
29+
del(key, cb)
30+
31+
Second, it includes a `wrap` function that lets you wrap any function in cache.
32+
(Note, this was inspired by [node-caching](https://github.com/mape/node-caching).)
33+
34+
Third, node-cashew lets you set up a tiered cache strategy. This may be of
35+
limited use in most cases, but imagine a scenario where you expect tons of
36+
traffic, and don't want to hit Redis for every request. You decide to store
37+
the most commonly-requested data in an in-memory cache (like [node-lru-cache](https://github.com/isaacs/node-lru-cache)),
38+
perhaps with a very short timeout and/or a small data size limit. But you
39+
still want to store the data in Redis for backup, and for the requests that
40+
aren't as common as the ones you want to store in memory. This is something
41+
node-cashew handles easily and transparently.
42+
43+
44+
## Usage Examples
45+
46+
### Single Store
47+
48+
var cashew = require('cashew');
49+
var redis_cache = cashew.caching({store: 'redis', db: 1, ttl: 100/*seconds*/});
50+
var memory_cache = cashew.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
51+
52+
redis_cache.set('foo', 'bar', function(err) {
53+
if (err) { throw err; }
54+
55+
redis_cache.get('foo', function(err, result) {
56+
console.log(result);
57+
// >> 'bar'
58+
redis_cache.del('foo', function(err) {});
59+
});
60+
});
61+
62+
function get_user(id, cb) {
63+
setTimeout(function () {
64+
console.log("Returning user from slow database.");
65+
cb(null, {id: id, name: 'Bob'});
66+
}, 100);
67+
}
68+
69+
var user_id = 123;
70+
var key = 'user_' + user_id;
71+
72+
redis_cache.wrap(key, function (cb) {
73+
get_user(user_id, cb);
74+
}, function (err, user) {
75+
console.log(user);
76+
77+
// Second time fetches user from redis_cache
78+
redis_cache.wrap(key, function (cb) {
79+
get_user(user_id, cb);
80+
}, function (err, user) {
81+
console.log(user);
82+
});
83+
});
84+
85+
// Outputs:
86+
// Returning user from slow database.
87+
// { id: 123, name: 'Bob' }
88+
// { id: 123, name: 'Bob' }
89+
90+
91+
### Multi-Store
92+
93+
var multi_cache = cashew.multi_caching([memory_cache, redis_cache]);
94+
user_id2 = 456;
95+
key2 = 'user_' + user_id;
96+
97+
// Sets in all caches.
98+
multi_cache.set('foo2', 'bar2', function(err) {
99+
if (err) { throw err; }
100+
101+
// Fetches from highest priority cache that has the key.
102+
multi_cache.get('foo2', function(err, result) {
103+
console.log(result);
104+
// >> 'bar2'
105+
106+
// Delete from all caches
107+
multi_cache.del('foo2');
108+
});
109+
});
110+
111+
multi_cache.wrap(key2, function (cb) {
112+
get_user(user_id2, cb);
113+
}, function (err, user) {
114+
console.log(user);
115+
116+
// Second time fetches user from memory_cache, since it's highest priority.
117+
// If the data expires in the memory cache, the next fetch would pull it from
118+
// the Redis cache, and set the data in memory again.
119+
multi_cache.wrap(key2, function (cb) {
120+
get_user(user_id2, cb);
121+
}, function (err, user) {
122+
console.log(user);
123+
});
124+
});
125+
126+
127+
## Tests
128+
129+
To run tests, first run:
130+
131+
npm install -d
132+
133+
Run the tests and JShint:
134+
135+
make
136+
137+
138+
## Contribute
139+
140+
If you would like to contribute to the project, please fork it and send us a pull request. Please add tests
141+
for any new features or bug fixes. Also run ``make`` before submitting the pull request.
142+
143+
144+
## License
145+
146+
node-cashew is licensed under the MIT license.

examples/example.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var cashew = require('cashew');
2+
var redis_cache = cashew.caching({store: 'redis', db: 1, ttl: 100/*seconds*/});
3+
var memory_cache = cashew.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
4+
5+
redis_cache.set('foo', 'bar', function(err) {
6+
if (err) { throw err; }
7+
8+
redis_cache.get('foo', function(err, result) {
9+
console.log(result);
10+
// >> 'bar'
11+
redis_cache.del('foo', function(err) {});
12+
});
13+
});
14+
15+
function get_user(id, cb) {
16+
setTimeout(function () {
17+
console.log("Returning user from slow database.");
18+
cb(null, {id: id, name: 'Bob'});
19+
}, 100);
20+
}
21+
22+
var user_id = 123;
23+
var key = 'user_' + user_id;
24+
25+
redis_cache.wrap(key, function (cb) {
26+
get_user(user_id, cb);
27+
}, function (err, user) {
28+
console.log(user);
29+
30+
// Second time fetches user from redis_cache
31+
redis_cache.wrap(key, function (cb) {
32+
get_user(user_id, cb);
33+
}, function (err, user) {
34+
console.log(user);
35+
});
36+
});
37+
38+
// Outputs:
39+
// Returning user from slow database.
40+
// { id: 123, name: 'Bob' }
41+
// { id: 123, name: 'Bob' }
42+
43+
44+
var multi_cache = cashew.multi_caching([memory_cache, redis_cache]);
45+
user_id2 = 456;
46+
key2 = 'user_' + user_id;
47+
48+
multi_cache.wrap(key2, function (cb) {
49+
get_user(user_id2, cb);
50+
}, function (err, user) {
51+
console.log(user);
52+
53+
// Second time fetches user from memory_cache, since it's highest priority.
54+
// If the data expires in the memory cache, the next fetch would pull it from
55+
// the Redis cache, and set the data in memory again.
56+
multi_cache.wrap(key2, function (cb) {
57+
get_user(user_id2, cb);
58+
}, function (err, user) {
59+
console.log(user);
60+
});
61+
62+
// Sets in all caches.
63+
multi_cache.set('foo2', 'bar2', function(err) {
64+
if (err) { throw err; }
65+
66+
// Fetches from highest priority cache that has the key.
67+
multi_cache.get('foo2', function(err, result) {
68+
console.log(result);
69+
// >> 'bar2'
70+
71+
// Delete from all caches
72+
multi_cache.del('foo2', function(err) {
73+
process.exit();
74+
});
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)