docvy-cache
An in-memory cache capable of being swapped from and to disk. For Node.js applications.
table of contents:
example usage:
// the module returns a Cache constructorvar Cache = ; // also this returns a Cache constructorvar CacheConstructor = CacheCache; // create a new cachevar cache = maxAge: 1209600000 // in milliseconds cacheDir: __dirname + "/cache" // where to swap from/to disk waitForRestore: true // wait for restoration to complete before any query; /*** All these operations are asynchronous. You might need to structure* your calls to the cache better*/ // restore cachecache; // setting an itemcache; // getting the itemcache; // removing the itemcache; // saving cachecache; // refreshing cache incase we had old itemscache; // we no longer need the cache?cache;
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 custommaxAge
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)
- signature:
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:
- test for memory leaks
- get estimate size of cache
- allow setting ttl (time to live) for items
- clearing of expired items
- manual
cache.refresh
- automated
- manual
- allow clearing/emptying of cache
cache.destroy
- use promises
- analyse performance against other in-memory Node.js caches
license:
The MIT License (MIT)
Copyright (c) 2015 Forfuture we@forfuture.co.ke
Copyright (c) 2015 GochoMugo mugo@forfuture.co.ke