A really simple caching system insipred by ActiveSupport::Cache
Releases are available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):
npm install thoroughfare
var thoroughfare = require('thoroughfare');
memory_store = thoroughfare('memory', {prefix: "test"});
memory_store.connect(function(err, store){
if (err) throw err;
store.write("key", "value", function(err){
if (err) throw err;
console.log("Set key to value");
});
});
<a name="memorystore"/ >
Options
- prefix - The prefix to be prepended to every key in the store
Reads a key from the store and returns a value if found, otherwise returning null
Arguments
- key - A string key for which to search.
- callback(err, value) - A callback which returns if there's an error or passes the retrieved value
Example
store.read('key', function(err, value) {
// if there was a problem interacting with the backend store then err will contain that err
console.log(value);
});
### store.write(key, value, callback)
Writes a value to the store for key
Arguments
- key - A key of type string
- value - data to be stored for the key. It will be JSON.stringified before being stored
- callback(err) - A callback to be called when the operation is complete.
Example
store.write('key', {name: "Patrick"}, function(err) {
if (err) throw err; // Oh noes there was an error
})
### store.fetch(key, generate, callback)
Similar to read except that Thoroughfare will execute the generate function if a null value is returned from the store and store the result under key
Arguments
- key - A key of type string
- generate(key) - A function that generates the desired result for the key
- callback(err, value) - A callback which returns with the value retrieved from the store or generated if it didn't exist
Example
var generate = function(key, cb) {
return cb(null, key + "-generated");
});
store.fetch(key, generate, function(err, value) {
if (err) throw err;
console.log(value);
});
### store.remove(key, callback) Removes the value stored for given key from the store
Arguments
- key - A key of type string
- callback(err) - Callback called on successful deletion from the store
Example
store.remove('no-longer-needed', function(err){
console.log("Successfully deleted key");
});
### store.exists(key, callback)
Checks the store for the existence of a value for given key and returns true / false as to its presence
Arguments
- key - key of type string
- callback(err, exists) - A callback that returns the existence of the given key
Example
store.exists(key, function(err, exists) {
if (err) throw err;
console.log("I " + (exists ? "found" : "didn't find") the key");
});
### store.clear(callback)
Empties the store of all key/value pairs
Arguments
- callback(err) - A callback which is called after all the operation completes.
Example
store.clear(function(err) {
if (err) throw err;
console.log("Emptied the store");
});