/
redis-cached-script.ts
50 lines (47 loc) · 1.25 KB
/
redis-cached-script.ts
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
import * as crypto from 'crypto';
import { RedisMiddleware } from '../redis-middleware';
/**
* Redis cached script manager. Inspirated in the code of the ioredis library:
*
* https://github.com/luin/ioredis/blob/master/lib/script.ts
*/
export class RedisCachedScript {
/**
* Lua script.
*/
protected _lua: string;
/**
* Redis manager.
*/
protected _redis: RedisMiddleware;
/**
* SHA1 string of the lua script in order to use EVALSHA.
*/
protected _sha: string;
/**
* Creates a new Redis cached script.
* @param lua Lua script.
* @param redis Redis manager.
*/
public constructor(lua: string, redis: RedisMiddleware) {
this._redis = redis;
this._lua = lua;
this._sha = crypto
.createHash('sha1')
.update(lua)
.digest('hex');
}
/**
* Evaluates the script in the server.
* @param args Script args ([number of keys, ...KEYS, ...ARGV]).
*/
public eval(argsGen: (scriptArg: string) => any[]): Promise<any> {
return (this._redis.evalsha(argsGen(this._sha)) as Promise<any>).catch((err) => {
// See https://redis.io/commands/eval
if (err.toString().indexOf('NOSCRIPT') === -1) {
throw err;
}
return this._redis.eval(argsGen(this._lua));
});
}
}