-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from streamich/eval
Adds EVAL commands support
- Loading branch information
Showing
9 changed files
with
248 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {createHash} from 'crypto'; | ||
|
||
export class Script { | ||
public readonly sha1: Uint8Array; | ||
|
||
constructor(public readonly script: string) { | ||
const hash = createHash('sha1').update(script).digest('hex'); | ||
this.sha1 = Buffer.from(hash); | ||
} | ||
} | ||
|
||
export class ScriptRegistry { | ||
protected map: Map<string, Script> = new Map(); | ||
|
||
public set(id: string, script: string) { | ||
const s = new Script(script); | ||
this.map.set(id, s); | ||
} | ||
|
||
public get(id: string): Script | undefined { | ||
return this.map.get(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {RedisCluster} from '../RedisCluster'; | ||
|
||
const setup = () => { | ||
const cluster = new RedisCluster({ | ||
seeds: [{host: '127.0.0.1', port: 7000}], | ||
}); | ||
return {cluster}; | ||
}; | ||
|
||
let keyCnt = 1; | ||
const getKey = () => '{redis-cluster-script}-test-' + keyCnt++; | ||
|
||
if (process.env.TEST_LOCAL_CLUSTER) { | ||
test('can execute a script', async () => { | ||
const {cluster} = setup(); | ||
cluster.start(); | ||
const scriptId = 'hello-scripting-cluster-test-' + Date.now(); | ||
cluster.scripts.set(scriptId, "return 'Hello, scripting!'"); | ||
const res = await cluster.eval(scriptId, 0, [], [], {utf8Res: true}); | ||
expect(res).toBe('Hello, scripting!'); | ||
cluster.stop(); | ||
}); | ||
|
||
test('can run a with keys and arguments', async () => { | ||
const {cluster} = setup(); | ||
cluster.start(); | ||
const scriptName = '{add-script-cluster}-' + Date.now(); | ||
cluster.scripts.set(scriptName, `return tonumber(redis.call("get",KEYS[1])) + tonumber(ARGV[1])`); | ||
const key = getKey(); | ||
await cluster.cmd(['SET', key, '1']); | ||
const res = await cluster.eval(scriptName, 1, [key], ['2'], {utf8Res: true}); | ||
expect(res).toBe(3); | ||
cluster.stop(); | ||
}); | ||
} else { | ||
test.todo('To enable cluster tests, set TEST_LOCAL_CLUSTER=1 in your environment variables.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.