redis-js icon indicating copy to clipboard operation
redis-js copied to clipboard

ioredis' defineCommand similar implementation

Open donaldrevel opened this issue 2 years ago • 2 comments

Hey,

We're looking into this library to replace ioredis in our code for our firebase functions and gain some speed.

I'm wondering if we could have a similar kind of function like defineCommand in ioredis (https://github.com/luin/ioredis#lua-scripting) which would take care of the script caching logic and execution?

This would save us a lot of time on writing our own implementation and error handling for scripting in redis.

donaldrevel avatar Aug 04 '22 15:08 donaldrevel

Hey @donaldrevel , I've created a release candidate with this feature npm install @upstash/[email protected]

You can now create a new script instance and then execute it.

import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv()

const script = "return ARGV[1];"
const echo = redis.createScript(script)

const res = await echo.exec([/* Keys go here */], ["value1"])
// res -> "value1"

Under the hood it will calculate the sha1 of the script and try calling EVALSHA first and if the script is not yet loaded to the server it falls back to EVAL. Afterwards the script will be on the server and only the sha1 will be sent over the wire.

Does this solve your problem?

chronark avatar Aug 08 '22 13:08 chronark

Yes that's great @chronark Thanks for this I haven't had the time to look into this yet, I'll get to this next week. I'll provide some feedback then

donaldrevel avatar Aug 11 '22 11:08 donaldrevel

It's in v1.12.0 Let me know once you have time to check and have some feedback :)

chronark avatar Aug 23 '22 10:08 chronark