Simple Cache Solution for Node and Web.
Sections:
- π How to use
- π Functions and Methods
- πͺ Events
- β° Auto Cleanup Expired
- β Deleted / Removed
const { V_Core_Cache } = require('v_core_cache');
const cache = new V_Core_Cache();
// OR
const { createCache } = require('v_core_cache');
const cache = createCache();
await cache.get(key) //> anything you put in
cache.getSync(key)
Returns all cache.
cache.getAll() //> object
Returns the approximate size of the cache in bytes.
await cache.size(); //> 1507114
cache.sizeSync();
Returns true if the key exists in the cache and is not expired.
cache.has(key)
Set/Create/Update an item in the cache. Will overwrite existing item.
await cache.set(key, data, expires?)
cache.setSync(key, data, expires?)
Returns true if cache was successfully purged. Otherwise, returns false if cache is already empty.
await cache.purge()
await cache.del(key); //> true/false
cache.delSync(key);
Returns stats about the cache.
cache.stats() //> { hits: 156, misses: 15, count: 33, size: 1507114 }
This basically just resets counters for hits and misses.
cache.purgeStats() //> { hits: 0, misses: 0, count: 33, size: 1507114 }
Returns the time in milliseconds when the item will expire.
cache.getExpire(key); //> 150123456789 [ Date.now() + expires]
Returns the number of expired items removed.
await cache.cleanup();
Returns the number of items in cache.
await cache.count();
cache.countSync();
cache.addListener("set", (data) => console.log(data));
// or
cache.on("set", (data) => console.log(data));
cache.removeListener("set", (data) => console.log(data));
// or
cache.off("set", (data) => console.log(data));
cache.prependListener("set", (data) => console.log(data));
// or
cache.pre("set", (data) => console.log(data));
console.log(cache.eventNames());
Removes all registered listeners for a single event
cache.removeAllListeners('set')
Removes all registered listeners for all registered events
cache.purgeAllListeners()
Returns {key, value} pair.
cache.on('set', (item) => console.log(item.key, item.value))
In this case we are returning the value only.
cache.on('set/{key}', (value) => console.log(value))
cache.on('get', (item) => console.log(data)) //> { key, value } - value can be undefined
cache.on('hit', (item) => console.log(item)) //> { key, value }
cache.on('miss', (item) => console.log(item)) //> { key }
cache.on('purge', (status) => console.log(status)) //> true/false - can return false if already empty
cache.on('purge_stats', (data) => console.log(data)) //> { hits, misses, count, size } - returns stats after purging them.
returns number of affected items
cache.on('cleanup', (data) => console.log(data)) //> number
New listener added
cache.on('addListener', (data) => console.log(data))
Removed event listener
cache.on('addListener', (data) => console.log(data))
const V_Core_Cache = require('v_core_cache');
const cache = new V_Core_Cache({ cleanInterval: 250 }); // Number in milliseconds
NOTE: When using autoCleanup you should stop the cleanup interval by calling
cache.stopCleanup()