1 changed files with 0 additions and 147 deletions
@ -1,147 +0,0 @@ |
|||
<h1>node-cashew</h1> |
|||
|
|||
<h1>Flexible NodeJS cache module</h1> |
|||
|
|||
<p>A cache module for nodejs that allows easy wrapping of functions in cache, |
|||
tiered caches, and a consistent interface.</p> |
|||
|
|||
<h2>Features</h2> |
|||
|
|||
<ul> |
|||
<li>Easy way to wrap any function in cache.</li> |
|||
<li>Tiered caches -- data gets stored in each cache and fetched from the highest |
|||
priority cache(s) first.</li> |
|||
<li>Use any cache you want, as long as it has the same API.</li> |
|||
<li>100% test coverage via <a href="https://github.com/visionmedia/mocha">mocha</a>, |
|||
<a href="https://github.com/yahoo/istanbul">istanbul</a>, and <a href="http://sinonjs.org">sinon</a>.</li> |
|||
</ul> |
|||
|
|||
<h2>Installation</h2> |
|||
|
|||
<pre><code>npm install node-cashew |
|||
</code></pre> |
|||
|
|||
<h2>Overview</h2> |
|||
|
|||
<p>First, node-cashew features the standard functions you'd expect in most caches:</p> |
|||
|
|||
<pre><code>set(key, val, cb) |
|||
get(key, cb) |
|||
del(key, cb) |
|||
</code></pre> |
|||
|
|||
<p>Second, it includes a <code>wrap</code> function that lets you wrap any function in cache. |
|||
(Note, this was inspired by <a href="https://github.com/mape/node-caching">node-caching</a>.)</p> |
|||
|
|||
<p>Third, node-cashew lets you set up a tiered cache strategy. This may be of |
|||
limited use in most cases, but imagine a scenario where you expect tons of |
|||
traffic, and don't want to hit Redis for every request. You decide to store |
|||
the most commonly-requested data in an in-memory cache (like <a href="https://github.com/isaacs/node-lru-cache">node-lru-cache</a>), |
|||
perhaps with a very short timeout and/or a small data size limit. But you |
|||
still want to store the data in Redis for backup, and for the requests that |
|||
aren't as common as the ones you want to store in memory. This is something |
|||
node-cashew handles easily and transparently.</p> |
|||
|
|||
<h2>Usage Examples</h2> |
|||
|
|||
<h3>Single Store</h3> |
|||
|
|||
<pre><code> var cashew = require('cashew'); |
|||
var redis_cache = cashew.caching({store: 'redis', db: 1, ttl: 100/*seconds*/}); |
|||
var memory_cache = cashew.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); |
|||
|
|||
redis_cache.set('foo', 'bar', function(err) { |
|||
if (err) { throw err; } |
|||
|
|||
redis_cache.get('foo', function(err, result) { |
|||
console.log(result); |
|||
// >> 'bar' |
|||
redis_cache.del('foo', function(err) {}); |
|||
}); |
|||
}); |
|||
|
|||
function get_user(id, cb) { |
|||
setTimeout(function () { |
|||
console.log("Returning user from slow database."); |
|||
cb(null, {id: id, name: 'Bob'}); |
|||
}, 100); |
|||
} |
|||
|
|||
var user_id = 123; |
|||
var key = 'user_' + user_id; |
|||
|
|||
redis_cache.wrap(key, function (cb) { |
|||
get_user(user_id, cb); |
|||
}, function (err, user) { |
|||
console.log(user); |
|||
|
|||
// Second time fetches user from redis_cache |
|||
redis_cache.wrap(key, function (cb) { |
|||
get_user(user_id, cb); |
|||
}, function (err, user) { |
|||
console.log(user); |
|||
}); |
|||
}); |
|||
|
|||
// Outputs: |
|||
// Returning user from slow database. |
|||
// { id: 123, name: 'Bob' } |
|||
// { id: 123, name: 'Bob' } |
|||
</code></pre> |
|||
|
|||
<h3>Multi-Store</h3> |
|||
|
|||
<pre><code> var multi_cache = cashew.multi_caching([memory_cache, redis_cache]); |
|||
user_id2 = 456; |
|||
key2 = 'user_' + user_id; |
|||
|
|||
// Sets in all caches. |
|||
multi_cache.set('foo2', 'bar2', function(err) { |
|||
if (err) { throw err; } |
|||
|
|||
// Fetches from highest priority cache that has the key. |
|||
multi_cache.get('foo2', function(err, result) { |
|||
console.log(result); |
|||
// >> 'bar2' |
|||
|
|||
// Delete from all caches |
|||
multi_cache.del('foo2'); |
|||
}); |
|||
}); |
|||
|
|||
multi_cache.wrap(key2, function (cb) { |
|||
get_user(user_id2, cb); |
|||
}, function (err, user) { |
|||
console.log(user); |
|||
|
|||
// Second time fetches user from memory_cache, since it's highest priority. |
|||
// If the data expires in the memory cache, the next fetch would pull it from |
|||
// the Redis cache, and set the data in memory again. |
|||
multi_cache.wrap(key2, function (cb) { |
|||
get_user(user_id2, cb); |
|||
}, function (err, user) { |
|||
console.log(user); |
|||
}); |
|||
}); |
|||
</code></pre> |
|||
|
|||
<h2>Tests</h2> |
|||
|
|||
<p>To run tests, first run:</p> |
|||
|
|||
<pre><code>npm install -d |
|||
</code></pre> |
|||
|
|||
<p>Run the tests and JShint:</p> |
|||
|
|||
<pre><code>make |
|||
</code></pre> |
|||
|
|||
<h2>Contribute</h2> |
|||
|
|||
<p>If you would like to contribute to the project, please fork it and send us a pull request. Please add tests |
|||
for any new features or bug fixes. Also run <code>make</code> before submitting the pull request.</p> |
|||
|
|||
<h2>License</h2> |
|||
|
|||
<p>node-easy-mysql is licensed under the MIT license.</p> |
Loading…
Reference in new issue