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

how to set expire time #1000

Closed
brucejcw opened this issue Mar 7, 2016 · 53 comments
Closed

how to set expire time #1000

brucejcw opened this issue Mar 7, 2016 · 53 comments
Labels

Comments

@brucejcw
Copy link

brucejcw commented Mar 7, 2016

client.set(key, value)

how to set a expire time

@BridgeAR
Copy link
Contributor

BridgeAR commented Mar 7, 2016

Please check the commands dcoumentation. Every part has it's own parameter so you would write something like:

client.set(key, value, 'EX', 60 * 60 * 24, callback);

@brucejcw
Copy link
Author

brucejcw commented Mar 8, 2016

Thanks

@brucejcw
Copy link
Author

brucejcw commented Mar 8, 2016

@BridgeAR the documentation link not working

@sxyjijiji
Copy link

it doesn't work.. redis version 3.0

client.set(key, value, 'EX', 60 * 60 * 24, callback);

@BridgeAR
Copy link
Contributor

BridgeAR commented Mar 30, 2016

@sxyjijiji it works perfectly fine for me. If you run into an error, please post your stack trace and describe what is not working.

@brucejcw I fixed the link

@bruceCzK
Copy link

I think it is not included in node_redis documentation, even the set function.
Maybe it'll confuse some people.

@BridgeAR
Copy link
Contributor

@bruceCzK There is no explicit commands documentation. And this would not be a good idea in general as the commands might change in Redis and new parameters might be added over time.

But there is more than one reference to the main documentation that you should always look at to see how the commands work. If you see any possibility to improve the current documentation, please feel free to open a pull request for it. It might be a good idea to add the link on all "commands" occurrences in the README.md.

@kpx-dev
Copy link
Contributor

kpx-dev commented Oct 14, 2016

hey guys, is there a way to set default expire at the client level?

Ex: something like

  const redisClient = redis.createClient({
    host: process.env.REDIS_ENDPOINT,
    port: process.env.REDIS_PORT,
    expire: 60 
})

@ryanvanderpol
Copy link

Just echoing that this is a super confusing syntax, especially to newcomers. There is nothing in node_redis documentation about key expirations/TTLs. I would imagine most people use Redis as part of a caching layer and expiration time for keys would be a really important part of that workflow. IMO, this should either be baked into the basic API (perhaps the set function can take in an optional parameter) or the documentation should at least talk about the command API as it relates to key expiration.

@kpx-dev
Copy link
Contributor

kpx-dev commented Oct 20, 2016

looks like the answer is no that we can't set global expire at the client level. It's kind of suck to have to set expire on every single key.

@BridgeAR
Copy link
Contributor

BridgeAR commented Oct 30, 2016

@ryanvanderpol the set function already takes a optional parameter as described. Just look at the documentation and at my comment above.

@kienpham2000 There is no global expire at the client level and this would be difficult to implement. Each command would have to be checked and manipulated. Instead, a new feature to add hooks could solve that.
This is not a caching library and likely you're looking for that instead.

@ryanvanderpol
Copy link

@BridgeAR: Understood, but you're kind of missing the point. Perhaps this is just me being lazy, but I refuse to learn the entire native Redis API just so I can stick things in and pull things out. Maybe I shouldn't be using the node_redis library then. Maybe I should be using something else that sits on top of it and abstracts this away from me. However, like I said before, I think most people use Redis as a cache and there is nothing in the node_redis documentation that even mentions the word "expire" or "expiration" (or even the "EX" command or TTLs) so without digging into the official Redis documentation it's hard to understand how to actually set a TTL for a key, an action that I expect the majority of people will need to do.

@CherryDT
Copy link

@ryanvanderpol It's a Redis client library, not a cache library. If you use a Redis library, you are kind of expected to read the Redis documentation, especially because node_redis does not document how the commands work, for this very reason. After all, node_redis just forwards the parameters to Redis most of the time.

If you want a cache library, you can use one of the many npm packages out there, such as cacheman-redis or node-redis-cache, in their documentation you will find explicit mentioning of "expire" 😄

I understand your point, but I think it shouldn't be the job of node_redis to duplicate the documentation of Redis or abstract things away more than necessary...

@ryanvanderpol
Copy link

@CherryDT your point is fair, which is also why I prefaced my previous comment with "maybe i should be using something else that sits on top of node_redis", but given how many +1's my comment has received so far, I have no doubt that I am not alone in this frustration.

If all I wanted to do was communicate directly with Redis in a raw format, then this wrapper doesn't really provide much value for me. I think it's completely fair to assume that the vast majority of people using Redis are doing so for caching purposes and will care about expirations.

This whole conversation could have just been negated with a "good point, we should add a bit to the documentation about how to set key expirations, along with some other commonly used commands". I suppose I could also submit a PR for that as well, but I'm not familiar with Redis outside of using it as a cache.

That said I'll look at the other libraries you mentioned and I might move to one of those instead if they suit my use case better.

@CherryDT
Copy link

CherryDT commented Apr 26, 2017

My point is that there is a lot more functionality than TTLs. There are different data types (lists, sets, sorted sets), a publisher & subscriber system and more, and basically duplicating the documention of Redis (and keeping it updated), or adding more sugar on the parameters (and keeping them updated) are things which I believe are outside the scope of this client. I wouldn't expect from a MySQL client the features of an ORM (or a MySQL reference) either, would you?

I believe in modular design, and hence I also believe that the individual modules shouldn't be larger than they need to be. And in this case, this client does a great job in abstracting the networking part and providing the commands as methods for a first layer of convenience, but I think that things on top of that should be the job of more specialized libraries for the specific goals (such as those I linked before for cache, or others for pubsub, etc.). Those often use node_redis interally, by the way, So even if it may not be of much use for you directly, it can be indirectly, because the more abstracted libraries you may use maybe use node_redis as dependency.

So, I agree that for your use case you would be better off with a more "high-level" library on top of something like node_redis.

@ryanvanderpol
Copy link

Yeah, I completely disagree with you here. I would expect the documentation for a MySQL client to show me how to do some of the common stuff everyone does in MySQL, like how do I do a select with an aggregate function or how do I do a subquery. I don't expect it to explain how to build an ORM or be a reproduction of the entire MySQL documentation, but show me how I do common stuff using the library.

Anyway, clearly you're not going to see my point of view and this is a waste of time. So far, 17 people agree with me and only you and one other person disagree. So, maybe you should step back and think about how you can see things from other people's perspective once in a while.

@CherryDT
Copy link

CherryDT commented Apr 26, 2017

Why do you make this personal? I totally understand your point, I just think that node_redis is not the right tool for the job then. (As you agreed yourself before I even started making my point.) Think about it, keeping things small and self-contained simplifies a lot of things. node_redis worries about networking, things like node-cache-redis (or your own implementation) worry about using the Redis API correctly. This way, there are not so many dependencies. Libraries "on top" will always exist in one way or another, and this way node_redis doesn't need to be updated (or even support different API versions) every time Redis adds or changes some feature (only when it adds a whole command).

I offered you alternative solutions, and I hope these will fulfill your needs, and if not, take a moment to search for "redis" on npm and you will finds tons of other libraries as well.

Let's just agree that we disagree on this. (And it doesn't matter if 17 or 1700 people agree with you or me. Every opinion should be valued, I think.)

@kpx-dev
Copy link
Contributor

kpx-dev commented Apr 26, 2017

If we look at the original question and the answer, I didn't know I have to use .set() with 'EX' and time options like that in this library.

Looking at the current README.md, it explains how to use client.hgetall (https://github.com/NodeRedis/node_redis#clienthgetallhash-callback) , perhaps we can add in few more common case for SET and GET in similar fashion? I'm also assuming people use at lot of GET and SET with redis.

@BridgeAR
Copy link
Contributor

I was not aware that so many people had issues with that as the likes do not show up in my inbox.

I would be up for a PR that documents the set command as an example while also mentioning "expire".

I would also like to have a PR that documents that this is not a cache library and that it is not intended to be one. I'm open for any input on how to make the documentation better for new people.

@stockholmux
Copy link
Contributor

@kienpham2000 Well, it's also a separate command - setex :)

Just to chime in here about explaining redis in the node_redis module readme. I'm not entirely sold on the idea. As the illustrated by this example - using set and expire in a multi/exec, using setex or using set with 'ex' - there are a tons of ways to do many things. None of which has anything to do with node. We explain hgetall because it departs from how other commands return values.

Not to say that people don't have questions, but it seems to me that writing a blog or medium post or something is a better way to inform people rather than concentrating it here. If we get the question, it can just be linked in any issues that come up. Turning the node_redis module readme into the source all for knowledge is maybe out of scope.

@kpx-dev
Copy link
Contributor

kpx-dev commented Apr 26, 2017

@stockholmux I'm just curious how do we use setex in this lib? Is it just client.setex()?

If this lib is 1-1 mapping with redis commands then I think we don't need doc. But if this lib has special api or diff way to pass in options, I think having some type of API doc would be very helpful for devs.

@BridgeAR
Copy link
Contributor

@kienpham2000 it is a 1-to-1 mapping. It was always meant to be one. A simple client to use the API. This is low level, not high level.

And @stockholmux is right that hgetall is only documented because you may use it in more ways than any other command. But it is still a 1-to-1 mapping.

Anyhow: I'm in favor of adding a section about this so this does not come up again.

@kpx-dev
Copy link
Contributor

kpx-dev commented Apr 27, 2017

@BridgeAR cool thanks for willing to accept this doc PR, here is my 1st draft: https://github.com/NodeRedis/node_redis/pull/1229/files

@rainabba
Copy link

Updated: #1380

@rainabba
Copy link

Now I found this and it looks like another method: https://dzone.com/articles/tutorial-working-nodejs-and

@rainabba
Copy link

I couldn't get that method working either (no error, just no effect so I'm abandoning this project and switching to https://www.npmjs.com/package/node-cache which can use Redis anyway.

@inane
Copy link

inane commented Oct 19, 2018

Under windows is not possible set the expiration within "set" method with 'EX', 20 because only accepts 2 parameters instead o 3.

@CherryDT
Copy link

CherryDT commented Oct 19, 2018

Maybe you have an old Redis version? The docs say it was added in Redis 2.6.12: https://redis.io/commands/set

You can use SETEX instead, which exists since 2.0.0...

@inane
Copy link

inane commented Oct 19, 2018

Maybe you have an old Redis version? The docs say it was added in Redis 2.6.12: https://redis.io/commands/set

I am using redis in a node-express project, with JS is not possible to add three parameters (or I am not being able.. :-( )

redis.set("cache:" + req.originalUrl, JSON.stringify(result), 'EX', 25); --> this is my code with error in the expiration area.

@CherryDT
Copy link

Which Redis version?

@inane
Copy link

inane commented Oct 19, 2018

Redis 2.4.5 for windows redis-windows and lastest version of node module.

@CherryDT
Copy link

CherryDT commented Oct 19, 2018

So then it's clear, please read my message above again and check the Redis documentation! The "EX" parameter in SET was only added in 2.6.12, so your Redis version 2.4.5 doesn't have it, but you can instead use SETEX which exists since 2.0.0.

redis.setex(key, seconds, value)

@inane
Copy link

inane commented Oct 19, 2018

redis.setex(key, value, expiration) --> that is not working

...but I have found one correct way in this link : redis windows

@CherryDT
Copy link

If you want to change your Redis version, yes, your way is right of course.
If not, mine works too, I just got the parameters wrong (but if you had checked the SETEX docs, you would have noticed): it's key, seconds, value and not key, values, seconds.

Anyhow, great to hear that it works for you now.

@kdthanvi
Copy link

The below doesn't work.

client.set('key', 'value', 'EX', 10, (error, replay)=>{
...
}

Any updated on this ?
I am using client.expire(id,10) for now.

PS : I am using redis version : Redis server v=5.0.0 and node verison : v8.10.0

@stockholmux
Copy link
Contributor

@kdthanvi That should work. Double check what's actually happening using MONITOR from redis-cli when running it.

@martinlevesque
Copy link

martinlevesque commented May 8, 2020

@kdthanvi is right. It does not work with redis 5:

client.set(key, value, 'EX', 60 * 60 * 24, callback);

You need:

`
client.set(key, value, callback);

client.expire(key, TTL, callback);
`

to make it actually work.

@stockholmux
Copy link
Contributor

@martinlevesque I believe you are mistaken. EX was added in 2.6.12 and node_redis correctly passes 'EX' no matter the version. Check with MONITOR and you'll see.

SET + EXPIRE can be dangerously different than SET .. EX due to atomic differences.

@daveteu
Copy link

daveteu commented Jul 8, 2020

Because this thread is number 1 when I search "Redis node set expire", I would like to make it a point to conclude the follow set expire command works with nodeJS redis as of today.

setex(key, 60, value)

Where 60 is the expire time in seconds.

@dalalRohit
Copy link

Because this thread is number 1 when I search "Redis node set expire", I would like to make it a point to conclude the follow set expire command works with nodeJS redis as of today.

setex(key, 60, value)

Where 60 is the expire time in seconds.

Found this after 2hrs of debugging and error solving. Thank you so much ;)

@tiagosalema
Copy link

Because this thread is number 1 when I search "Redis node set expire", I would like to make it a point to conclude the follow set expire command works with nodeJS redis as of today.

setex(key, 60, value)

Where 60 is the expire time in seconds.

This is indeed working atm. Note that SETEX might stop working in the future. Redis have a note in their SET documentation that SETEX, amongside other commands, may be deprecated, as the SET command can accomplish the same by just the use of options.

Note: Since the SET command options can replace SETNX, SETEX, PSETEX, GETSET, it is possible that in future versions of Redis these commands will be deprecated and finally removed.

For this reason I would recommend using client.set('key', 'value', 'EX', 60) instead.

@narendradaffodilsw
Copy link

it doesn't work.. redis version 3.0

client.set(key, value, 'EX', 60 * 60 * 24, callback);

Try using client.setex(key, seconds, value);

@ChristophP
Copy link

it is a 1-to-1 mapping. It was always meant to be one. A simple client to use the API. This is low level, not high level.
I appreciate this library and I am using it quite a bit.

I just think the overall confusion in this threads shows that it would help greatly if the README went into a bit more detail on the API. It wasn't obvious to me how to pass more than two arguments to a .set() call and it further confused me that there is an API section which does not include .get() and .set(). Would be nice if the paragraph stating that all redis commands are exposed as (lower case) methods on the client would be made more exlicit and promiment. I think that would could help avoid much of the confusion.

@trainoasis
Copy link

trainoasis commented Jun 22, 2021

How would I use client.set() with .incr() ? Doesn't incr() already set, but no expiry available with it then or what?

The below does not seem to work:

const multi = client.multi();

  multi.incr(key);
  multi.expire(key, 10);

@leibale
Copy link
Collaborator

leibale commented Jun 22, 2021

@trainoasis You have to execute the multi chain:

client.multi()
  .incr(key)
  .expire(key, 10)
  .exec((err, replies) => {
    console.log(err, replies);
  });

@jaschaio
Copy link

jaschaio commented Nov 24, 2021

For anybody using redis@next you need to set it like this:

await client.set( 'key', 'value', {
    EX: 60,
} );

See https://github.com/redis/node-redis#redis-commands

@martianmartian
Copy link

martianmartian commented Feb 21, 2022

this is madness, i installed node-redis 4.0.3, finally figured out following commands works

  let res;
  await client.hSet('frameworks_hash', {
    'javascript': 'ReactJS',
    'css': 'TailwindCSS',
    'node': 'Express'
  });
  await client.pExpireAt("frameworks_hash",1);
  setTimeout(async () => {
    res = await client.hGetAll('frameworks_hash');
    console.log(res);
    debugger
  }, 100);

@alphainvaders
Copy link

For anybody using redis@next you need to set it like this:

await client.set( 'key', 'value', {
    EX: 60,
} );

See https://github.com/redis/node-redis#redis-commands

This is the right answer today, at least for me.

@pianomansam
Copy link

For anybody using redis@next you need to set it like this:

await client.set( 'key', 'value', {
    EX: 60,
} );

See https://github.com/redis/node-redis#redis-commands

This is the right answer today, at least for me.

When I run this on Redis 6, I get ErrorReply: ERR syntax error

@jaschaio
Copy link

jaschaio commented Jun 2, 2022

@pianomansam I believe in Nov 2021 redis@next was for 4.0.0

@vuduynhiennn
Copy link

thanks a lot

@Arcanorum
Copy link

what a mess of an API lol

@h0tgril
Copy link

h0tgril commented Jan 26, 2024

I was using client.setex(k, ttl, v), not sure where I read to use that it was working. Latest version broke that, even after I enabled legacy mode. Setting "redis": "=3.0.2", in my package.json fixed it.

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

No branches or pull requests