0.0.4 • Published 11 years ago

thoroughfare v0.0.4

Weekly downloads
2
License
-
Repository
github
Last release
11 years ago

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

Standard Methods

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

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

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

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

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

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

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago