Skip to content

Commit cbde950

Browse files
committed
Added eval and script command supports
1 parent 30c9352 commit cbde950

File tree

6 files changed

+179
-21
lines changed

6 files changed

+179
-21
lines changed

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changes Logs
22

3+
## v1.2.0
4+
5+
- Added following command methods:
6+
7+
- `mExists`
8+
- `eval`
9+
- `evalSHA`
10+
- `scriptLoad`
11+
- `scriptFlush`
12+
- `scriptKill`
13+
- `scriptExists`
14+
- `scriptDebug`
15+
316
## v1.1.0
417

518
- Fixed the watch mode with a new `WatchClient` class.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/redis",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "A redis protocol implement for Node.js.",
55
"main": "./lib/index.js",
66
"scripts": {

src/examples/debug.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,38 @@ import * as L from '@litert/core';
3737
// await cli.auth("hello");
3838
await cli.flushAll();
3939

40-
console.log(await cli.ping(''));
41-
console.log(await cli.set('a', '123'));
42-
console.log(await cli.incr('a', 23));
43-
console.log(await cli.incrByFloat('a', 23.4));
44-
console.log(await cli.get('a'));
45-
console.log(await cli.mGet(['a', 'sada']));
40+
console.log('PING ->', await cli.ping(''));
41+
console.log('GET ->', await cli.set('a', '123'));
42+
console.log('INCR ->', await cli.incr('a', 23));
43+
console.log('INCRBYFLOAT ->', await cli.incrByFloat('a', 23.4));
44+
console.log('GET ->', await cli.get('a'));
45+
console.log('MGET ->', await cli.mGet(['a', 'sada']));
46+
console.log('MGET ->', await cli.mGet(['a', 'sada']));
47+
48+
const SHA1 = await cli.scriptLoad('return redis.call("GET", "a")');
49+
50+
console.log('EVAL ->', (await cli.evalSHA(SHA1, [], [])).toString());
4651

4752
await L.Async.sleep(2000);
4853

4954
let x = cli.set('a', '333');
5055

51-
console.log(await x);
52-
await cli.lPush('lll', ['a', 'b']);
53-
console.log(await cli.lIndex('lll', 1));
54-
console.log(await cli.lRange('lll', 0, -1));
55-
console.log(await cli.hMSet('ggg', {
56+
console.log('SET ->', await x);
57+
console.log('LPUSH ->', await cli.lPush('lll', ['a', 'b']));
58+
console.log('LINDEX ->', await cli.lIndex('lll', 1));
59+
console.log('LRANGE ->', await cli.lRange('lll', 0, -1));
60+
console.log('HMSET->', await cli.hMSet('ggg', {
5661
'a': '3333',
5762
'bb': '1231232131'
5863
}));
59-
console.log(await cli.hMGet('ggg', ['bb', 'a']));
60-
console.log(await cli.incr('a'));
61-
console.log(await cli.pubSubChannels());
62-
console.log(await cli.pubSubNumPat());
63-
console.log(await cli.pubSubNumSub(['hello']));
64-
console.log(await cli.publish('hello', 'test'));
64+
console.log('HMGET->', await cli.hMGet('ggg', ['bb', 'a']));
65+
console.log('INCR->', await cli.incr('a'));
66+
console.log('PUBSUBCHANNELS->', await cli.pubSubChannels());
67+
console.log('PUBSUBNUMPAT->', await cli.pubSubNumPat());
68+
console.log('PUBSUBNUMSUB->', await cli.pubSubNumSub(['hello']));
69+
console.log('PUBLISH->', await cli.publish('hello', 'test'));
70+
console.log('EXISTS->', await cli.exists('a'));
71+
console.log('MEXISTS->', await cli.mExists(['a', 'b', 'lll', 'x', 'y']));
6572

6673
await L.Async.sleep(2000);
6774

src/lib/Commands.ts

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,20 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
399399
process: isIntegerOne
400400
},
401401

402+
/**
403+
* Command: exists
404+
* @see https://redis.io/commands/exists
405+
*/
406+
'mExists': {
407+
prepare(keys: string[]): IPrepareResult {
408+
409+
return {
410+
cmd: 'EXISTS',
411+
args: keys
412+
};
413+
}
414+
},
415+
402416
/**
403417
* Command: type
404418
* @see https://redis.io/commands/type
@@ -1860,5 +1874,80 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
18601874
cmd: 'PUBSUB'
18611875
};
18621876
}
1863-
}
1877+
},
1878+
1879+
'scriptLoad': {
1880+
prepare(script: string): IPrepareResult {
1881+
1882+
return {
1883+
cmd: 'SCRIPT',
1884+
args: ['LOAD', script]
1885+
};
1886+
},
1887+
process: U.buffer2String
1888+
},
1889+
1890+
'scriptKill': {
1891+
prepare(): IPrepareResult {
1892+
1893+
return {
1894+
cmd: 'SCRIPT',
1895+
args: ['KILL']
1896+
};
1897+
},
1898+
process: null
1899+
},
1900+
1901+
'scriptFlush': {
1902+
prepare(): IPrepareResult {
1903+
1904+
return {
1905+
cmd: 'SCRIPT',
1906+
args: ['FLUSH']
1907+
};
1908+
},
1909+
process: null
1910+
},
1911+
1912+
'scriptExists': {
1913+
prepare(shaList: string[]): IPrepareResult {
1914+
1915+
return {
1916+
cmd: 'SCRIPT',
1917+
args: ['EXISTS', ...shaList]
1918+
};
1919+
}
1920+
},
1921+
1922+
'scriptDebug': {
1923+
prepare(enabled: boolean | 'sync'): IPrepareResult {
1924+
1925+
return {
1926+
cmd: 'SCRIPT',
1927+
args: ['DEBUG', enabled === false ? 'NO' : enabled === true ? 'YES' : 'SYNC']
1928+
};
1929+
},
1930+
process: null
1931+
},
1932+
1933+
'evalSHA': {
1934+
prepare(sha: string, keys: string[], args: Array<string | Buffer>): IPrepareResult {
1935+
1936+
return {
1937+
cmd: 'EVALSHA',
1938+
args: [sha, keys.length, ...keys, ...args]
1939+
};
1940+
}
1941+
},
1942+
1943+
'eval': {
1944+
prepare(script: string, keys: string[], args: Array<string | Buffer>): IPrepareResult {
1945+
1946+
return {
1947+
cmd: 'EVAL',
1948+
args: [script, keys.length, ...keys, ...args]
1949+
};
1950+
}
1951+
},
1952+
18641953
};

src/lib/Common.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ export interface ICommandAPIs {
360360
* Command: exists
361361
* @see https://redis.io/commands/exists
362362
*/
363-
exists(key: string): Promise<boolean>;
363+
exists(key: string | string[]): Promise<number>;
364+
365+
/**
366+
* Command: exists
367+
* @see https://redis.io/commands/exists
368+
*/
369+
mExists(keys: string[]): Promise<number>;
364370

365371
/**
366372
* Command: type
@@ -933,6 +939,49 @@ export interface ICommandAPIs {
933939
* @see https://redis.io/commands/pubsub
934940
*/
935941
pubSubNumPat(): Promise<number>;
942+
943+
/**
944+
* Command: eval
945+
* @see https://redis.io/commands/eval
946+
*/
947+
eval(luaScript: string, keys: string[], args: Array<string | Buffer>): Promise<any>;
948+
949+
/**
950+
* Command: evalsha
951+
* @see https://redis.io/commands/evalsha
952+
*/
953+
evalSHA(luaScriptSHA: string, keys: string[], args: Array<string | Buffer>): Promise<any>;
954+
955+
/**
956+
* Command: script debug
957+
* @see https://redis.io/commands/script-debug
958+
*/
959+
scriptDebug(enabled: boolean | 'sync'): Promise<void>;
960+
961+
/**
962+
* Command: script exists
963+
* @see https://redis.io/commands/script-exists
964+
*/
965+
scriptExists(shaList: string[]): Promise<number>;
966+
967+
/**
968+
* Command: script kill
969+
* @see https://redis.io/commands/script-kill
970+
*/
971+
scriptKill(): Promise<void>;
972+
973+
/**
974+
* Command: script flush
975+
* @see https://redis.io/commands/script-flush
976+
*/
977+
scriptFlush(): Promise<void>;
978+
979+
/**
980+
* Command: script load
981+
* @see https://redis.io/commands/script-load
982+
*/
983+
scriptLoad(script: string): Promise<string>;
984+
936985
}
937986

938987
export interface ICommandClientBase {

0 commit comments

Comments
 (0)