vig-cache
v0.3.1
Published
vig-cache
Downloads
12
Readme
KVCache
Key-value based Cache taking url, params, query and session info into account.
Very simple to use. JSON is recommanded, but String can be saved too.
Improvement
Before:
Requests per second: 12.98 [#/sec] (mean)
After:
Requests per second: 307.32 [#/sec] (mean)
Usage
// for ts
import { KVCache } from "vig-cache";
import * as redis from "redis";
or
// for js
const KVCache = require('vig-cache').KVCache;
const Redis = require("redis");
then
let config = {
host: '127.0.0.1',
port: 6379,
db: 0
};
let redis = Redis.createClient(config);
const cache = new KVCache(redis);
// JSON storage
await cache.setJSON(req, user, {value: 100});
await cache.getJSON(req, user);
// String storage
await cache.set(req, user, 'string');
let saved = await KVCache.get(req, user');
await cache.clear(req); // For Both JSON and String
// with Expressjs
cache.attach(app)
app.get('/', async function(req, res) {
if (req.cache.data) {
return res.send(req.cache.data.text)
}
await req.cache.setJSON(req, user, {text: 'toBeCached'});
res.send('toBeCached');
});