oh-cache
v1.0.0
Published
Simple cache for NodeJS
Readme
Oh! Cache [WIP]
This is a simple NodeJS library to enable data caching in your project.
Example
const oc = require('oh-cache');
const { PERSIST_DATA_TTL, LIVE_DATA_TTL } = oc;
function getData(key) {
if(oc.hasVaidCache(key))
return oc.getCache(key);
getDataFromDB(function(results) {
oc.updateCache(key, results, PERSIST_DATA_TTL);
return oc.getCache(key);
});
}Install
npm install oh-cache --save
Features
- Lightweight
- Simple key/value mapping to handle your variables cache
- TTL (Time To Live) can be configurated by users
Cache entry structure
{
timestamp: 123092384,
ttl: LIVE_DATA_TTL,
data: Object
}API
hasCache(key): To check whether cache with keykeyexistshasVaidCache(key): To check whether cache with keykey, which is not expired yet, existsgetCache(key): Get data from cache entry with keykeyupdateCache(key, data, ttl): Add/Update cache entry if it is expired (ttlto specify the Time To Live of the entry)forceUpdateCache(key, data, ttl): Add/Update cache entry even it may not expired (ttlto specify the Time To Live of the entry)deleteCache(key): Delete cache entry with keykey
Default TTLs
PERSIST_DATA_TTL: 3600 secondsLIVE_DATA_TTL: 5 seconds
TODO
- Determine TTL automatically
Feel free to contribute this project if you are interested ❤
