-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (80 loc) · 2.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
var RedisClient = require('redis').RedisClient
, events = require('events')
, util = require('util')
, fs = require('fs')
;
function Lua(client){
if(!(client instanceof RedisClient))
throw new Error('client should be a instance of RedisClient');
var scripts = {}
, self = this
;
/**
*
* @param key
* @param filename
* @param [next]
*/
function load(key, filename, next){
function checkFile(next){
fs.exists(filename, function(exists){
if(!exists) return next(null, filename); // use filename as a raw script
return fs.readFile(filename, function(err, script){
if(err && next) return next(err);
return next(null, script);
});
})
}
checkFile(function(err, script){
return client.script('load', script, function(err, hash){
if(err && next) return next(err);
scripts[key] = hash;
self.emit(key + ' script loaded', hash);
self.emit('script loaded', key, hash);
return next && next(null, hash);
});
})
}
/**
*
* @param key - key used when script loaded
* @param params - {keys:[], argv:[]}
* @param next
* @returns {*}
*/
function run(key, params, next){
if(!next && typeof params == 'function'){
next = params;
params = {};
}
var hash = scripts[key];
if(!hash) return next(new Error('Unknown script'));
var args = [hash];
var keys_num = params.keys&¶ms.keys.length || 0;
var argv_num = params.argv&¶ms.argv.length || 0;
args.push( keys_num );
if(keys_num) params.keys.forEach(function(key){ args.push(key); });
if(argv_num) params.argv.forEach(function(arg){ args.push(arg); });
if(next) args.push(next);
return client.evalsha.apply(client, args);
}
function waitRun(key, params, next){
var hash = scripts[key];
if(hash) return self.run(key, params, next);
return self.on(key+' script loaded', function(){
self.run(key, params, next);
})
}
function flush(next){
client.script('flush', function(){
self.emit('scripts flushed');
next&&next.apply(self, arguments);
});
}
self.load = load;
self.run = run;
self.waitRun = waitRun;
self.flush = flush;
}
util.inherits(Lua, events.EventEmitter);
module.exports = Lua;