A simple caching library for node-js with support for redis, memcache and in-memory stores.
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
LICENSE
README.md
index.js
package.json

README.md

Thoroughfare

A really simple caching system insipred by ActiveSupport::Cache

Download

Releases are available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install thoroughfare

Example

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");
  });
});

Documentation

Standard Methods

Stores

Stores

<a name="memorystore"/ >

MemoryStore

Options

  • prefix - The prefix to be prepended to every key in the store

RedisStore

__Options__ * prefix - Prefix to be prepended to all keys in the store * host - The hostname of the redis server you'd like to use * port - The port that the redis server you want to use listens on * password - If the redis server requires authentication * database - Select the database that you'd like to use for storage * expire - The expiration time of all keys in the store in seconds (default 0)

Standard Methods

### store.read(key, callback)

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");
});