node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »

sf-filecache

sf-filecache

Simple and smart FS based cache system.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate

sf-filecache exists for two purposes:

  • having a streameable fs based cache system
  • opening only one file descriptor for cache queries

Usage

Buffer based:

var FileCache = require('sf-filecache');
 
var filecache = new FileCache();
 
var endOfLife = Date.now() + 36000;
 
// Set a value in the filecache for the 'plop' key 
fileCache.set('plop', new Buffer('plop!'), endOfLife, function(err) {
  if(err) {
    throw err;
  }
  // Retrieve it 
  fileCache.get('plop', function(err, data) {
    if(err) {
      return done(err);
    }
    console.log(data.toString()); // plop! 
    done();
  });
 
});

Stream based:

var FileCache = require('sf-filecache');
 
var filecache = new FileCache();
 
var endOfLife = Date.now() + 36000;
 
// Set a stream content in the filecache for the 'plop' key 
fileCache.setStream('plop', fs.createReadStream('file'), endOfLife, function(err) {
  if(err) {
    throw err;
  }
  // Retrieve it 
  fileCache.getStream('plop', function(err, stream) {
    if(err) {
      return done(err);
    }
    stream.pipe(process.stdout); // plop! 
    done();
  });
 
});

API

## FileCache(options) FileCache constructor

Kind: global function
Api: public

Param Type Description
options Object Options of the cache (dir, domain and clock)

### fileCache._keyToPath(key) ⇒ String Transform a key into a path were to save/read the contents

Kind: instance method of FileCache
Returns: String - The computed path
Api: private

Param Type Description
key String The key to transform

### fileCache._createHeader(header) ⇒ Buffer Create a bucket header

Kind: instance method of FileCache
Returns: Buffer - The header contents as a buffer
Api: private

Param Type Description
header Object Header description

### fileCache._readHeader(data) ⇒ Object Read the header description from a buffer

Kind: instance method of FileCache
Returns: Object - The header description

Param Type Description
data Buffer The buffer

### fileCache.get(key, cb) ⇒ void Get cached data for the given key

Kind: instance method of FileCache

Param Type Description
key String The key
cb function The callback ( signature function(err:Error, data:Buffer) {})

### fileCache.getStream(key, cb) ⇒ void Get cached data as a stream for the given key

Kind: instance method of FileCache

Param Type Description
key String The key
cb function The callback ( signature function(err:Error, stream:ReadableStream) {})

### fileCache.set(key, data, eol, cb) ⇒ void Set cached data at the given key

Kind: instance method of FileCache

Param Type Description
key String The key
data Buffer The data to store
eol Number The resource invalidity timestamp
cb function The callback ( signature function(err:Error) {})

### fileCache.setStream(key, stream, eol, cb) ⇒ void Set cached data via a stream at the given key

Kind: instance method of FileCache

Param Type Description
key String The key
stream ReadableStream The data to store as a readable stream
eol Number The resource invalidity timestamp
cb function The callback ( signature function(err:Error) {})

### fileCache.setEOL(key, eol, cb) ⇒ void Set end of life to the given key

Kind: instance method of FileCache

Param Type Description
key String The key
eol Number The resource invalidity timestamp
cb function The callback ( signature function(err:Error) {})