Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SET with KEEPTTL #1517

Closed
martinzuern opened this issue May 2, 2020 · 16 comments
Closed

Add support for SET with KEEPTTL #1517

martinzuern opened this issue May 2, 2020 · 16 comments

Comments

@martinzuern
Copy link

Issue

Redis 6 introduced support for the KEEPTTL flag when using SET.
https://redis.io/commands/set

@stockholmux
Copy link
Contributor

@martinzuern Works without changes - KEEPTTL would just be an argument after your value.

@gionapaolini
Copy link

gionapaolini commented May 20, 2021

@martinzuern Works without changes - KEEPTTL would just be an argument after your value.

@stockholmux How do you provide the options to the set command? I could not find this information in the documentation

@leibale
Copy link
Contributor

leibale commented May 20, 2021

@gionapaolini

client.set('key', 'value', 'KEEPTTL', (err, reply) => {
  // ...
});

@gionapaolini
Copy link

gionapaolini commented May 20, 2021

@gionapaolini

client.set('key', 'value', 'KEEPTTL', (err, reply) => {
  // ...
});

Thank you @leibale , how do you apply multiple option instead?
I was trying to do SET key value EX 10 NX GET
(set value with expiration date only if not exists, and if exists return it)

@leibale
Copy link
Contributor

leibale commented May 20, 2021

@gionapaolini

client.set('key', 'value', 'EX', '10', 'NX', 'GET', (err, reply) => {
  // ...
});

edit: NX & GET are not supported by Redis (try to run it in redis-cli, you'll get "syntax error")

@gionapaolini
Copy link

gionapaolini commented May 20, 2021

@gionapaolini

client.set('key', 'value', 'EX', '10', 'NX', 'GET', (err, reply) => {
  // ...
});

I already tried that, I still get

ReplyError: ERR syntax error
    at parseError (/app/node_modules/.pnpm/redis-parser@3.0.0/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/app/node_modules/.pnpm/redis-parser@3.0.0/node_modules/redis-parser/lib/parser.js:302:14)

I'm using redis 3.0.2 btw. Is that supported on this version?


Ah thanks for the clarification, it does not seem to be documented on the website https://redis.io/commands/set

@leibale
Copy link
Contributor

leibale commented May 20, 2021

@gionapaolini redis/redis-doc#1565

@LazyCompiler
Copy link

@gionapaolini

client.set('key', 'value', 'KEEPTTL', (err, reply) => {
  // ...
});

Works like a charm 👌

@iimanii
Copy link

iimanii commented Jul 21, 2021

Why is this closed ? you still can't do something like this

SET key value KEEPTTL XX

@isidoro98
Copy link

Hey @iimanii Have you found a work around to pass both options KEEPTTL and XX with node-redis?

@leibale
Copy link
Contributor

leibale commented Sep 18, 2021

@iimanii @isidoro98

client.set('key', 'value', 'KEEPTTL', 'XX', (err, reply) => {
  // ...
});

@isidoro98
Copy link

Thanks that's awesome @leibale.

Are you aware of this type missing in the @types/redis package?

@leibale
Copy link
Contributor

leibale commented Sep 18, 2021

@isidoro98 @types/redis is not part of node-redis and is not maintained by us. We're currently working on v4, which will have TS declarations built-in. Until then, feel free to open a PR in @types/redis.

@isidoro98
Copy link

Awesome, thanks for letting me know! Wasn't aware of that :)

@vanduc1102
Copy link

vanduc1102 commented Nov 4, 2021

my code with ioredis typedscript

export async function updateObject<T>(key: string, item: T) {
  return redis.set(key, JSON.stringify(item), 'KEEPTTL', 'XX');
}

export async function setObject<T>(key: string, item: T) {
  return redis.set(key, JSON.stringify(item), 'EX', 3600);
}

@wulfsolter
Copy link

For the next person who ends up here. I'm on redis@4.2.0 and this is what I needed to use

await redis.set(key, JSON.stringify(val), { KEEPTTL: true });

as per https://github.com/redis/node-redis/blame/a0de7967f98068790cd09c909bbbb7e7d6deefe8/packages/client/lib/commands/SET.ts#L38-L48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants