forked from ilkerc/loopback-redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rediscache.js
137 lines (120 loc) · 4.86 KB
/
rediscache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module.exports = function(Model, options) {
var redis = require("redis")
var client = null
if (options.client) {
var clientSettings = options.client;
client = redis.createClient(clientSettings);
} else {
var app = require('../../server/server.js');
if (app.redisClient) {
client = app.redisClient
} else {
client = redis.createClient(app.get('redis'));
}
}
var redisDeletePattern = require('redis-delete-pattern');
client.on("error", function (err) {
console.log(err);
// try to connect again with server config
if(err.toString().indexOf("invalid password") !== -1){
console.log("Invalid password... reconnecting with server config...");
var app = require('../../server/server');
var clientSettings = app.get('redis');
client = redis.createClient(clientSettings);
}
});
Model.beforeRemote('**', function(ctx, res, next) {
// get all find methods and search first in cache
if((ctx.method.name.indexOf("find") !== -1 || ctx.method.name.indexOf("__get") !== -1 || ctx.method.name.indexOf("get") !== -1) && client.connected){
if(typeof ctx.req.query.cache != 'undefined'){
var modelName = ctx.method.sharedClass.name + "_" + ctx.method.name;
var cachExpire = ctx.req.query.cache;
// set key name
var cache_key = modelName+'_'+new Buffer(JSON.stringify(ctx.req.params) + JSON.stringify(ctx.req.query)).toString('base64');
// search for cache
client.get(cache_key, function(err, val) {
if(err){
console.log(err);
}
if(val !== null){
ctx.result = JSON.parse(val);
ctx.res.setHeader('Cache-Hit', true)
client.ttl(cache_key, function(err, data) {
if (!err) {
ctx.res.setHeader('Cache-TTL', data)
var expires = new Date()
expires.setSeconds(expires.getSeconds() + parseInt(data))
ctx.res.setHeader('Expires', expires)
}
ctx.done(function (err) {
if (err) return next(err);
});
})
}else{
//return data
ctx.res.setHeader('Cache-Hit', false)
ctx.res.setHeader('Cache-TTL', cachExpire)
var expires = new Date()
expires.setSeconds(expires.getSeconds() + parseInt(cachExpire))
ctx.res.setHeader('Expires', expires)
next();
}
});
}else{
next();
}
}else{
next();
}
});
Model.afterRemote('**', function(ctx, res, next) {
// get all find methods and search first in cache - if not exist save in cache
if((ctx.method.name.indexOf("find") !== -1 || ctx.method.name.indexOf("__get") !== -1 || ctx.method.name.indexOf("get") !== -1) && client.connected){
if(typeof ctx.req.query.cache != 'undefined'){
var modelName = ctx.method.sharedClass.name + "_" + ctx.method.name;
var cachExpire = ctx.req.query.cache;
// set key name
var cache_key = modelName+'_'+new Buffer(JSON.stringify(ctx.req.params) + JSON.stringify(ctx.req.query)).toString('base64');
// search for cache
client.get(cache_key, function(err, val) {
if(err){
console.log(err);
}
if(val == null){
// set cache key
client.set(cache_key, JSON.stringify(res));
client.expire(cache_key, cachExpire);
next();
}else{
next();
}
});
}else{
next();
}
}else{
next();
}
});
Model.afterRemote('**', function(ctx, res, next) {
// delete cache on patchOrCreate, create, delete, update, destroy, upsert
if((ctx.method.name.indexOf("find") == -1 && ctx.method.name.indexOf("__get") == -1) && client.connected){
var modelName = ctx.method.sharedClass.name + "_" + ctx.method.name;
var cachExpire = ctx.req.query.cache;
// set key name
var cache_key = modelName+'_*';
// delete cache
redisDeletePattern({
redis: client,
pattern: cache_key
}, function handleError (err) {
if(err){
console.log(err);
}
next();
});
}else{
next();
}
});
}