forked from conancat/mongoose-redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (102 loc) · 3.29 KB
/
index.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
// Generated by CoffeeScript 1.7.1
var crypto, mongooseRedisCache, redis, _;
redis = require("redis");
crypto = require("crypto");
_ = require("underscore");
RegExp.prototype.toJSON = function() {
var ind, json, opts, str;
json = {
$regexp: this.source
};
str = this.toString();
ind = str.lastIndexOf('/');
opts = str.slice(ind + 1);
if (opts.length > 0) {
json.$options = opts;
}
return json;
};
mongooseRedisCache = function(mongoose, options, callback) {
var client, host, pass, port, prefix, redisOptions;
if (options == null) {
options = {};
}
host = options.host || "";
port = options.port || "";
pass = options.pass || "";
redisOptions = options.options || {};
prefix = options.prefix || "cache";
var client;
if(options.cluster_mode) {
var haredis = require('haredis');
var client = haredis.createClient(host)
client.on("error", function (err) {
console.log('Cannot connect to Redis cache using haredis:' + err);
});
client.on('connect', function () {
console.log('Connected to Redis using haredis.');
});
} else {
client = redis.createClient(port, host, redisOptions);
}
mongoose.redisClient = client;
// if (pass.length > 0) {
// client.auth(pass, function(err) {
// if (callback) {
// return callback(err);
// }
// });
// }
mongoose.Query.prototype._exec = mongoose.Query.prototype.exec;
mongoose.Query.prototype.exec = function(callback) {
var cb, collectionName, expires, fields, hash, key, model, populate, query, schemaOptions, self;
self = this;
model = this.model;
query = this._conditions || {};
options = this._optionsForExec(model) || {};
fields = _.clone(this._fields) || {};
populate = this.options.populate || {};
schemaOptions = model.schema.options;
collectionName = model.collection.name;
expires = this._mongooseOptions.redisExpires || schemaOptions.expires || 60;
if (!(schemaOptions.redisCache && this._mongooseOptions.lean)) {
delete this._mongooseOptions.redisCache;
delete this._mongooseOptions.redisExpires;
return mongoose.Query.prototype._exec.apply(self, arguments);
}
delete this._mongooseOptions.redisCache;
delete this._mongooseOptions.redixExpires;
hash = crypto.createHash('md5').update(JSON.stringify(query)).update(JSON.stringify(options)).update(JSON.stringify(fields)).update(JSON.stringify(populate)).digest('hex');
key = [prefix, collectionName, hash].join(':');
cb = function(err, result) {
var docs, k, path;
if (err) {
return callback(err);
}
if (!result) {
for (k in populate) {
path = populate[k];
path.options || (path.options = {});
_.defaults(path.options, {
cache: false
});
}
return mongoose.Query.prototype._exec.call(self, function(err, docs) {
var str;
if (err) {
return callback(err);
}
str = JSON.stringify(docs);
client.setex(key, expires, str);
return callback(null, docs);
});
} else {
docs = JSON.parse(result);
return callback(null, docs);
}
};
client.get(key, cb);
return this;
};
};
module.exports = mongooseRedisCache;