3

I am using JedisCluster, and I need to set a key with 1-hour expiry, now I am doing something like

    getJedisCluster().set(key,value);
    getJedisCluster().expire(key, 60 * 60);

But I hope I can using a single command to reduce the round trip to send commands

With Redis cli , I can write this:

set key value ex 3600

But in JedisCluster, I can only find a interface:

 public String set(final String key, final String value, final String nxxx, final String expx, final long time) {

which means I either should use setex or setnx.

But I hope my set command applies both to update or insert.

How can I do this?

Ps: Jedis-Client's version is 2.9.0

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • Im not sure about this. But can you tried `set(final String key, final String value, final SetParams params)` ? As `SetParams` can hold `secondsToExpire ` and `millisecondsToExpire `. – Zico Mar 20 '18 at 06:49
  • @Zico Which version do you use, I can't find this method in class JedisCluster – JaskeyLam Mar 20 '18 at 07:22
  • Just explored the `JedisCluster` [here](https://github.com/xetorthio/jedis/blob/master/src/main/java/redis/clients/jedis/JedisCluster.java) is the source file. – Zico Mar 20 '18 at 12:10

2 Answers2

2

If you are using jedis client version 2.9.1

jedis.setex(sid, 86400,String.valueOf(version));

In the latest versions, we have something like this

enter image description here

Rams
  • 2,141
  • 5
  • 33
  • 59
1

You can use setex method directly. It does exactly this

 set key value ex 3600
Karthikeyan Gopall
  • 5,469
  • 2
  • 19
  • 35