hypercache
v0.2.0
Published
Simple but powerful caching.
Downloads
15
Readme
Hypercache
Simple but powerful caching.
Hypercache caches async db operations and gives you the abbility to get things by id or search the database completly sync and with speeds otherwise impossible.
Why hypercache?
Fast
Zero-Dependencies
100% Test Coverage
Usage
new Hypercache(fnc, options)
fnc
: A function that is called asfnc(cb)
cb(err, res)
err
: The error that occured, if anyres
: An array of objects
options
name
: An optional name. If not provided the name will be guessed based on the functionkeys
: A list of unique properties like["id"]
sets
: A list of non-unique (also array) properties like["aliases"]
interval
: The interval to refresh in ms (default2500
)manual
: This will disable thefnc
andinterval
and will expose anupdate
function that is called asupdate(res)
sync
: A hypercache to sync with. This will override the call for fnc tofnc(opt.sync.getAll(), cb)
and disable the interval.
Events
ready
: Emitted when the first update occurs
update
: Emitted when an update occurs
error
: Emitted when a function without a callback throws an error
Methods
getAll()
: Returns an array of all elements
getMap(name)
: Returns the id->object map for key name
getBy(id, value)
: Returns an object with [id]==value
getSetMap(name)
: Returns the id->set map for key name
getSet(id, value)
: Returns an array with [id]==value || [id].indexOf(value)
search(str)
: Searches through all keys and returns the first with [key]==str
searchSets(str)
: Seracher through all sets and returns the first with [key]==value || [key].indexOf(value)
Examples
Fetch and cache data from mongoDB
const cache = new Hypercache(cb => Users.find({}, cb), {
keys: ["id", "username"],
sets: ["aliases"]
})
cache.once("ready", () => {
cache.getBy("username", "mkg20001") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
cache.search("1234") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
cache.searchSets("mkg") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
})