This is a simple NodeJS library to enable data caching in your project.
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);
});
}
npm install oh-cache --save
- Lightweight
- Simple key/value mapping to handle your variables cache
- TTL (Time To Live) can be configurated by users
{
timestamp: 123092384,
ttl: LIVE_DATA_TTL,
data: Object
}
-
hasCache(key)
: To check whether cache with keykey
exists -
hasVaidCache(key)
: To check whether cache with keykey
, which is not expired yet, exists -
getCache(key)
: Get data from cache entry with keykey
-
updateCache(key, data, ttl)
: Add/Update cache entry if it is expired (ttl
to specify the Time To Live of the entry) -
forceUpdateCache(key, data, ttl)
: Add/Update cache entry even it may not expired (ttl
to specify the Time To Live of the entry) -
deleteCache(key)
: Delete cache entry with keykey
-
PERSIST_DATA_TTL
: 3600 seconds -
LIVE_DATA_TTL
: 5 seconds
- Determine TTL automatically
Feel free to contribute this project if you are interested ❤