Skip to content

Commit

Permalink
v0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Deren committed Nov 24, 2017
1 parent 01f0191 commit 92c3aec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
32 changes: 15 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,42 +60,40 @@ export default class {

const _write = res.write.bind(res);

this.client.hgetall(cacheKey, (err, result) => {
if ( result && result.body && result.body.length ) {
this.client.get(cacheKey, (err, result) => {
if ( result && result.length ) {
if (this.options.httpHeader) {
res.setHeader(`${this.options.httpHeader}`, 'HIT')
}
res.setHeader('Content-Type', 'application/json');
_write(result.body);
res.end();
if(binary) { //Convert back to binary buffer
_write(new Buffer(result, 'base64'));
res.end();
} else {
res.setHeader('Content-Type', 'application/json');
_write(result);
res.end();
}
} else {
if (this.options.httpHeader) {
res.setHeader(`${this.options.httpHeader}`, 'MISS')
}
return next()
}
});

res.write = (body) => {
/** convert binary to base64 string **/
if(binary && typeof body !== 'string'){
body = new Buffer(body).toString('base64');
}

if ( typeof body !== 'string' ) {
_write(body);
res.end();
}

let entry = {
body: body,
stale: this.options.stale,
created: +new Date(),
}

this.client.hmset(cacheKey, entry);
this.client.expire(cacheKey, this.options.ttl);
entry = null;

this.client.set(cacheKey, body, 'EX', this.options.ttl);

_write(body);
res.end();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/library.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 92c3aec

Please sign in to comment.