Filesystem store with in-memory cache
This was built for usage with eight-track
, an HTTP fixture library. It is designed for ease-of-access while debugging. By default, items will be stored to separate .json
files in the folder.
Install the module with: npm install fs-memory-store
// Generate a store inside of `http-fixtures`
var Store = require('fs-memory-store');
var store = new Store(__dirname + '/http-fixtures');
// Save a value
store.set('hello', {world: true}, function (err) {
// If there was an error, `err` will be it
// We have created `http-fixtures/hello.json`
/*
{
"world": true
}
*/
// Load the value
store.get('hello', function (err, val) {
// If there was an error, `err` will be it
// Log our value
console.log(val); /* {world: true} */
});
// Load an non-existent value
store.get('wat', function (err, val) {
// If there was an error, `err` will be it
// Log our value
console.log(val); /* null */
});
});
fs-memory-store
returns Store
as its module.exports
.
Constructor for a new store
- dir
String
, Directory to generate our store inside of - options
Object
, Container for options/flags- ext
String
, Extension to save values under. By default, this is.json
- stringify
Function
, Stringifier to pass values through when saving to disk- By default, this is
JSON.stringify
with an indenation of 2
- By default, this is
- parse
Function
, Parser to pass values through when loading from disk- By default, this is
JSON.parse
- By default, this is
- ext
Retrieve an item from memory with a fallback to disk.
- key
String
, Identifier to retrieve item by - cb
Function
, Error-first callback function to receive item value- Signature should be
(err, val)
- err
Error|null
, If there was an error, this will be it - val
Mixed|null
, If the value was found, this will be it. If it was not found, this will benull
.
- Signature should be
Save an item to memory and disk
- key
String
, Identifier to save item under - val
Mixed
, Value to save under thekey
- cb
Function
, Error-first callback function to handle errors- Signature should be
(err)
- err
Error|null
, If there was an error, this will be it
- Signature should be
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
Support this project and others by twolfson via gittip.
Copyright (c) 2014 Todd Wolfson
Licensed under the MIT license.