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

Create keys with expires #159

Closed
pensierinmusica opened this issue Sep 25, 2015 · 21 comments
Closed

Create keys with expires #159

pensierinmusica opened this issue Sep 25, 2015 · 21 comments
Labels

Comments

@pensierinmusica
Copy link
Contributor

Hi,

Is it poissible to send this Redis command with ioredis in just one function call?

set key 100 ex 10
@luin
Copy link
Collaborator

luin commented Sep 25, 2015

Yep! redis.set('key', 100, 'ex', 10)

@luin luin added the question label Sep 25, 2015
@pensierinmusica
Copy link
Contributor Author

Thanks @luin! Would it make sense to add this example to README? If you like I can create a PR :)

@luin
Copy link
Collaborator

luin commented Sep 25, 2015

That would be great :-). ioredis just passes all arguments directly to the redis server

@pensierinmusica
Copy link
Contributor Author

Done! #160

:)

@luin luin closed this as completed in #160 Sep 25, 2015
@wesleymilan
Copy link

I'm receiving an error trying to set key with expires

redis.set('key', 100, 'ex', 10);

Unhandled rejection ReplyError: ERR wrong number of arguments for 'set' command
at ReplyParser._parseResult (/project/node_modules/ioredis/lib/parsers/javascript.js:39:14)
at ReplyParser.execute (/project/node_modules/ioredis/lib/parsers/javascript.js:139:20)
at Socket. (/project/node_modules/ioredis/lib/redis/event_handler.js:94:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:540:20)

@luin
Copy link
Collaborator

luin commented Mar 5, 2016

@wesleymilan You should run a redis server > 2.6.12 in order to support SET command with EX option. Refer to http://redis.io/commands/set for more information.

@aalexgabi
Copy link

@luin Please add this to the API documentation.

@luin
Copy link
Collaborator

luin commented Aug 5, 2016

@aalexgabi do you mean adding a note about set command with ex option is only available in redis > 2.8?

@dirkbonhomme
Copy link

You can also use this dedicated command http://redis.io/commands/SETEX ;)

@aalexgabi
Copy link

TL;DR Method signatures should be documented.

Disclaimer: Maybe I didn't see the part of documentation that talks about this when I "scanned" it. I didn't read the entire page. I mainly used mouse scroll and Ctrl + F (searched for "set", "ex", "command", "commands").

@luin @dirkbonhomme I would like to see explicit documentation on the existence of the set method on Redis prototype. Also I would like to see explicit documentation on the arguments each command can take even if that means just writing something like ioredis.set(String arg0,String arg1,String arg2...) where arg<x> is the positional argument documented in the Redis API documentation for your version available here: <link>

There is no reference of the set method (or any other Redis command) here: https://github.com/luin/ioredis/blob/master/API.md#new_Redis I knew the set method existed from the README.

It wasn't obvious to me what arguments the set command can take: I was wondering if it would be like ioredis.set(key, value, px, nx, xx) but then what about ex or ioredis.set(key, value, {px: new Date().getTime()}) but then where do I find the options names in the documentation. Then I searched online and I stumbled upon this issue where @luin gave an example. It would be nice to have that example visible somewhere in the documentation.

This would also reduce probability of issues like #159.

@aalexgabi
Copy link

I just saw the commit in #160. that added:

+// All arguments are passed directly to the redis server:
+redis.set('key', 100, 'EX', 10);

I didn't see that in README.

@luin
Copy link
Collaborator

luin commented Aug 6, 2016

@aalexgabi I agree with you that the api documentation should be more informative. As noted in README, all arguments are passed directly to the redis server, so that ioredis supports every command / option that your redis server supports. Given people may run different versions of redis servers, the commands / options may differ, so it's a little hard to document them clearly.

However, I think some examples do help, and it's also better to note how to pass options like EX so that people won't confused.

@aalexgabi
Copy link

aalexgabi commented Aug 8, 2016

@luin I agree with you on having generic documentation for all versions of Redis. I suggest adding something like:

  • instance
    • .<redis command>(<first command argument>, <second command argument>...) ⇒ any

In the table of contents and as a section in the API documentation.

@magicdawn
Copy link

+1 on document for the instance methods signature

@arankhem
Copy link

arankhem commented Mar 19, 2018

@luin , Just wanted to know is there a way to expire hash similar to set. For example

let sample = {
'field1': 1,
'field2': 2,
'field3': 3,
}
redis.hmset('test', sample, 'EX', 1000)

@dirkbonhomme
Copy link

@arankhem no, you'll have to do it with 2 commands:

  • hmset(key, data)
  • expire(key, ttl)

@arankhem
Copy link

@dirkbonhomme ok thank you.

@xtianus79
Copy link

please can we get these awesome examples in the readme @dirkbonhomme and the op have two wildly different examples.

@katesroad
Copy link

@luin , Just wanted to know is there a way to expire hash similar to set. For example

let sample = {
'field1': 1,
'field2': 2,
'field3': 3,
}
redis.hmset('test', sample, 'EX', 1000)

This does not work at 2020 Feb

@bizzfs
Copy link

bizzfs commented Nov 28, 2020

@luin , Just wanted to know is there a way to expire hash similar to set. For example
let sample = {
'field1': 1,
'field2': 2,
'field3': 3,
}
redis.hmset('test', sample, 'EX', 1000)

This does not work at 2020 Feb

works in lowercase 'ex'

@locopump
Copy link

@arankhem no, you'll have to do it with 2 commands:

  • hmset(key, data)
  • expire(key, ttl)

But what about hmset when you put the data inside of hash table, for example when you use redis.hmset('myHashTable', 'myKey', 'myData)

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