-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
115 lines (101 loc) · 3.39 KB
/
backend.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
var stow = require('stow')
, _ = require('underscore');
module.exports = function (app) {
function CantinaBackend (options) {
var self = this
, redisOptions = {}
, memoryOptions = {};
this.prefix = options.prefix = app.redisKey(options.prefix) + ':';
this.amino = options.amino;
// Check for backend-specific options.
if (options.backends && options.backends.redis) {
redisOptions = options.backends.redis;
}
if (options.backends && options.backends.memory) {
memoryOptions = options.backends.memory;
}
delete options.backends;
// Check for redis client.
if (options.client) {
redisOptions.client = options.client;
}
// Create backends.
this.backends = {
redis: new stow.backends.Redis(_.extend({}, options, redisOptions)),
memory: new stow.backends.Memory(_.extend({}, options, memoryOptions))
};
// Subscriptions
this.amino.subscribe(this.key('memory', 'clear'), function (key, specId) {
if (specId !== self.amino.id) {
self.backends.memory.clear(key, self.handleError.bind(self));
}
});
this.amino.subscribe(this.key('memory', 'invalidate'), function (tags, specId) {
if (specId !== self.amino.id) {
self.backends.memory.invalidate(tags, self.handleError.bind(self));
}
});
}
CantinaBackend.prototype.key = function () {
return [this.prefix] + Array.prototype.slice.call(arguments, 0).join(':');
};
CantinaBackend.prototype.set = function (options, cb) {
var self = this;
self.backends.memory.set(options, function (err) {
if (err) return cb(err);
self.backends.redis.set(options, function (err) {
if (err) return cb(err);
self.amino.publish(self.key('memory', 'clear'), options.key, self.amino.id);
cb();
});
});
};
CantinaBackend.prototype.get = function (key, cb) {
var self = this;
self.backends.memory.get(key, function (err, result) {
if (err) return cb(err);
if (result) {
app.emit('cache', 'memory', key, 'hit');
return cb(null, result);
}
app.emit('cache', 'memory', key, 'miss');
self.backends.redis.get(key, function (err, result) {
if (err) return cb(err);
if (!result) {
app.emit('cache', 'redis', key, 'miss');
return cb(null, null);
}
app.emit('cache', 'redis', key, 'hit');
self.backends.memory.set(result, function (err) {
cb(err, result);
});
});
});
};
CantinaBackend.prototype.invalidate = function (tags, cb) {
var self = this;
self.backends.memory.invalidate(tags, function (err) {
if (err) return cb(err);
self.backends.redis.invalidate(tags, function (err) {
if (err) return cb(err);
self.amino.publish(self.key('memory', 'invalidate'), tags, self.amino.id);
cb();
});
});
};
CantinaBackend.prototype.clear = function (pattern, cb) {
var self = this;
self.backends.memory.clear(pattern, function (err) {
if (err) return cb(err);
self.backends.redis.clear(pattern, function (err) {
if (err) return cb(err);
self.amino.publish(self.key('memory', 'clear'), pattern, self.amino.id);
cb();
});
});
};
CantinaBackend.prototype.handleError = function (err) {
if (err) this.amino.emit('error', err);
};
return CantinaBackend;
};