It would be very helpful to have NX option (similar to one of expanded SET syntax) added to all EXPIRE Redis commands: EXPIRE key seconds NX or PEXPIREAT key milliseconds-timestamp NX.

For example, in our project we have class of keys that need to be updated constantly (usually via INCRBY/HINCRY/ZINCRBY) but also need to have 'concrete' deadline after which old key gets deleted and new one is created. Right now that job is done via Redis script like:

redis.call('INCRBY', KEYS[1], ARGV[1])

if redis.call('TTL', KEYS[1]) < 0 then
  redis.call('EXPIRE', KEYS[1], ARGV[2])
end

With this new option last three lines could be safely replaced with

redis.call('EXPIRE', KEYS[1], ARGV[2], 'NX')

Also with NX option EXPIRE could return not just simple 1 or 0 but a current TTL of a given key:

redis> SET test value
OK
redis> EXPIRE test 60
(integer) 1
redis> EXPIRE test 60 NX
(integer) 59

Unfortunately, my own C skills are not enough to provide a pull request, but I've found some old one (#1350) that provides 4 additional commands: EXPIRENX, EXPIREATNX etc.

Comment From: sunng87

+1 for this. This is frequently needed.

Comment From: ahfeel

+1 !

Comment From: shaylevi2

+1 !!