docvy-cache
An in-memory cache capable of being swapped from and to disk. For Node.js applications.
table of contents:
- example usage
- cache API
- installation
- todo
- license
example usage:
var Cache = require("docvy-cache");
var CacheConstructor = Cache.Cache;
var cache = new Cache({
maxAge: 1209600000,
cacheDir: __dirname + "/cache",
waitForRestore: true
});
cache.restore(function(err) {
if (err) { return console.log("could not RESTORE"); }
return console.log("RESTORED");
});
cache.set("appName", "docvy-app", function(err) {
if (err) { return console.log("could not SET"); }
return console.log("SET");
});
cache.get("appName", function(err, val) {
if (err) { return console.log("could not GET"); }
return console.log("GOT: " + val);
});
cache.unset("appName", function(err) {
if (err) { return console.log("could not UNSET"); }
return console.log("UNSET");
});
cache.save(function(err) {
if (err) { return console.log("could not SAVE"); }
return console.log("SAVED");
});
cache.refresh(function(err) {
if (err) { return console.log("could not REFRESH"); }
return console.log("REFRESHED");
});
cache.destroy(function(err) {
if (err) { return console.log("could not DESTROY"); }
return console.log("DESTROYED");
});
API
Cache([options])
This is the constructor for a Cache.
options
(Object):
cacheDir
(String): path to a directory where cache items can be saved tomaxAge
(Number): maximum amount of time to keep an item, in millisecondswaitForRestore
(Boolean): whether to wait for cache to be restored before executing any queries.
cache.restore([done])
Restores the cache from its directory, as specified in cacheDir
during instantiation.
done
(Function): other than a possible error is passed to the callback.
In case you had already saved a cache, you can restore it.
cache.save([done])
Saves the cache to its directory.
done
(Function): other than a possible error is passed to the callback.
cache.set(key, value [, options] [, done])
Sets an Item
key
(String): key of the itemvalue
(String): value of the itemoptions
(Object):
maxAge
: a custom maxAge
for this item. NOTE: this value is ignored for items already set into cache. It is only respected if its a new item being set.
done
(Function): function called once query is complete. Other than a possible error is passed to the callback.
cache.get(key, callback)
Returns value of item.
key
(String): key of the itemcallback
(Function): function passed the value of item.
- signature:
callback(err, value)
cache.unset(key [, done])
Removes an item from cache.
key
(String): key of the itemdone
(Function): function called once query is complete. Other than a possible error is passed.
cache.refresh([done])
Refreshes cache by removing expired items. All keys are checked to ensure they have not lived past their expiry time.
done
(Function): called once cache refreshing is complete. Other than a possible error is passed.
cache.destroy([done])
Destroys the entire cache both from in-memory and file-system.
done
(Function): function called once destroying cache is complete. Other than a possible error is passed.
installation:
(Bleeding Edge) Installing from github using npm:
⇒ npm install docvy-cache
todo:
license:
The MIT License (MIT)
Copyright (c) 2015 Forfuture we@forfuture.co.ke
Copyright (c) 2015 GochoMugo mugo@forfuture.co.ke