Kirzilla
Kirzilla

Reputation: 16596

How to know if key set to be expired or not in Redis?

As far as Redis do not allow to reSet expire date to key (because of nans with replication) I'd like to know is there any method to check if key set to be expired or not?

Thank you

Upvotes: 22

Views: 16953

Answers (2)

rpetrich
rpetrich

Reputation: 32326

Use the TTL command. If an expiration is set, it returns the number of seconds until the key expires; otherwise it returns -1.

Upvotes: 44

chakrit
chakrit

Reputation: 61518

I don't think checking for expiration date make much sense in Redis, though. I'd like to first suggest that you model it so you don't need to check for expiration date.

If you really need it, though, you can just use another key to store the expiration date for later retrieval via normal GET/SET.

Note that you can also check for EXPIRES manually in your client code, which might be a better solution.

Upvotes: -2

Related Questions