Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/node-simple-cache
Like, really simple.
https://github.com/hughsk/node-simple-cache
Last synced: 8 days ago
JSON representation
Like, really simple.
- Host: GitHub
- URL: https://github.com/hughsk/node-simple-cache
- Owner: hughsk
- Created: 2011-11-28T12:05:01.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2017-09-15T17:36:03.000Z (about 7 years ago)
- Last Synced: 2024-10-16T14:21:35.597Z (23 days ago)
- Language: JavaScript
- Homepage:
- Size: 93.8 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-simple-cache #
Super simple key/value storage for NodeJS. Wouldn't say it's production ready at all but if you're working on speeding up a small hack or prototype it's easy to use for caching and similar requirements.## Usage ##
Unless you're getting a lot of concurrent requests, you can safely stick to using `Storage.get()` and `Storage.set()` to store and retrieve data.var Storage = require('simple-cache').Storage;
var cache = new Storage();if (!cache.exists('hello')) {
cache.set('hello', world);
}cache.get('hello'); //Returns 'world'
But when requests start to pile up, calling this within a non-blocking function such as `fs.readFile()` means that you could still be calling the function multiple times before it's stored in the cache. In this case, 'Storage.async()' keeps calls in line until the value is set.
var fs = require('fs');
var cache = new (require('simple-cache').Storage)();//Incorrect
fs.readFile('filename', function(err, data) {
if (cache.exists('hello')) {
console.log(cache.get('hello'));
} else {
cache.set('hello', world);
console.log(cache.get('hello'));
}
});// Correct
cache.async('hello', {
set: function(setValue) {
fs.readFile('filename', function(err, data) {
setValue(data);
});
},
get: function(value) {
console.log(value);
}
});