Simple and smart FS based cache system.
sf-filecache
exists for two purposes:
- having a streameable fs based cache system
- opening only one file descriptor for cache queries
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();
});
});
FileCache constructor
Kind: global function
Api: public
Param | Type | Description |
---|---|---|
options | Object |
Options of the cache (dir, domain and clock) |
-
FileCache(options)
-
._keyToPath(key) ⇒
String
-
._createHeader(header) ⇒
Buffer
-
._readHeader(data) ⇒
Object
-
.get(key, cb) ⇒
void
-
.getStream(key, cb) ⇒
void
-
.set(key, data, eol, cb) ⇒
void
-
.setStream(key, stream, eol, cb) ⇒
void
-
.setEOL(key, eol, cb) ⇒
void
-
._keyToPath(key) ⇒
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 |
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 |
Read the header description from a buffer
Kind: instance method of FileCache
Returns: Object
- The header description
Param | Type | Description |
---|---|---|
data | Buffer |
The buffer |
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) {}) |
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) {}) |
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) {}) |
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) {}) |
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) {}) |