This repository was archived by the owner on Nov 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
54 lines (48 loc) · 1.33 KB
/
cache.js
File metadata and controls
54 lines (48 loc) · 1.33 KB
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
var Medea = require('medea');
var rules = require('./caching_rules');
var Cache = function(db) {
this.open = Medea.prototype.open.bind(db);
this.get = Medea.prototype.get.bind(db);
this.put = Medea.prototype.put.bind(db);
this.remove = Medea.prototype.remove.bind(db);
};
var options;
var db;
var isOpen = false;
module.exports = function(opts) {
options = opts || {};
dirname = options.dirname || process.cwd() + '/data';
db = new Medea(options);
return { package: setupPackage };
};
var setupPackage = function(argo) {
return {
name: 'Argo HTTP Caching',
install: function() {
argo
.use(function(handle) {
handle('request', function(env, next) {
env.cache = new Cache(db);
env.cache.ttl = options.ttl;
if (!isOpen) {
db.open(dirname, options, function(err) {
isOpen = true;
db.compact(function(err) {
next(env);
});
});
} else {
next(env);
}
});
})
.use(function(handle) {
handle('request', rules.generateKey);
})
.use(function(handle) {
handle('request', rules.checkRequest);
handle('response', { affinity: 'sink' }, rules.checkResponse);
});
}
};
};