https://github.com/mostlygeek/node-simple-cache
Simple disk caching module for node.js
https://github.com/mostlygeek/node-simple-cache
Last synced: 6 months ago
JSON representation
Simple disk caching module for node.js
- Host: GitHub
- URL: https://github.com/mostlygeek/node-simple-cache
- Owner: mostlygeek
- License: mit
- Created: 2011-10-25T02:37:30.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2012-08-24T17:19:12.000Z (over 13 years ago)
- Last Synced: 2025-06-24T01:17:37.319Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 111 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](http://travis-ci.org/mostlygeek/Node-Simple-Cache)
# About
A *really* simple cache to disk library. It will either return data from the disk cache file or fetch it from the source.
# Install
> npm install Simple-Cache
# Usage Examples
# Basic Example
/**
* Like just run this. It should be self explanitory
* how it works.
*/
var cache = require('Simple-Cache').SimpleCache("/tmp", console.log);
/**
* cache.get returns a promise, cause tons of callback chains
* suck.
*
* Usage: get(string key, cacheMissFn) { .... }
*/
var promise = cache.get('my testing data', function(callback) {
// some async operation...
callback(results);
});
## Multiple Callback Handlers on the Promise
promise.fulfilled(function(results) {
console.log("Callback 1: " + results);
});
promise.fulfilled(function(results) {
console.log("Callback 2: " + results);
});
## Fluent Interfaces
promise.fulfilled(function(results) {
console.log("Fluent interface 1");
}).fulfilled(function(results) {
console.log("Fluent interface 2");
}).fulfilled(function(results) {
console.log("Fluent interface 3. Ok we get it");
});
# How To Use It In Real Life
/**
*
* A Practical example. Cachine Web results.
*
*/
cache.get('omgz! the same file!', function(callback) {
console.log("Fetching from https://raw.github.com/...");
require('https').get({
host : 'raw.github.com',
path : '/mostlygeek/Node-Simple-Cache/master/examples.js'
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
callback(data);
});
});
}).fulfilled(function(data) {
console.log("Blam!, Got back " + data.length + " bytes");
});