Ioredis: Create keys with expires

Created on 25 Sep 2015  Â·  20Comments  Â·  Source: luin/ioredis

Hi,

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

set key 100 ex 10
question

Most helpful comment

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

All 20 comments

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

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

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

Done! #160

:)

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)

@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.

@luin Please add this to the API documentation.

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

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

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.

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.

@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.

@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.

+1 on document for the instance methods signature

@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)

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

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

@dirkbonhomme ok thank you.

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

@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

@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'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jehy picture jehy  Â·  4Comments

kimmobrunfeldt picture kimmobrunfeldt  Â·  5Comments

ORESoftware picture ORESoftware  Â·  5Comments

iamjochem picture iamjochem  Â·  5Comments

saschaishikawa picture saschaishikawa  Â·  4Comments