nodejs eval script?
micateam opened this issue · 2 comments
micateam commented
Description
//
const getPixelLuaScript = `
local key = KEYS[1]
local position = tonumber(ARGV[1])
local binaryColor = ""
return '233' .. tostring(key)
`;
console.log(`getPixelLuaScript:${ position} ${chunkKey}`)
const binaryColor = await this.redis.eval(getPixelLuaScript, 1, chunkKey, position);
getPixelLuaScript:32 pixel004
key is nil
Node.js Version
"redis": "^4.7.0",
Redis Server Version
No response
Node Redis Version
No response
Platform
No response
Logs
No response
micateam commented
I used this method. May I ask if the first method was cancelled?
const binaryColor = await this.redis.eval(getPixelLuaScript,{
keys:[chunkKey],
arguments:['100']
});
leibale commented
I'm not too sure I understand your question, but anyway, the best way to use scripts with node-redis is to register them on the client rather than using the eval command:
import { createClient, defineScript, BlobStringReply } from 'redis';
const client = createClient({
scripts: {
getPixel: defineScript({
SCRIPT: `
local key = KEYS[1]
local position = tonumber(ARGV[1])
local binaryColor = ""
return '233' .. tostring(key)
`,
NUMBER_OF_KEYS: 1,
transformArguments(key: string, position: number) {
return [key, position.toString()];
},
transformReply: undefined as unknown as () => BlobStringReply
})
}
});
client.on('error', err => console.error('Redis Client Error', err));
await client.connect();
// you can use the script just like a normal command, the types are inferred from `transformArguments` and `transformReply`
const result = await client.getPixel('key', 100);see https://github.com/redis/node-redis/blob/master/docs/programmability.md#lua-scripts for more details