# TTL: set, read, lose, keep and clear it


--- No TTL, and a key that does not exist ---
set("plain", "v")
getExpire("plain")   = -1
getExpire("missing") = -2

--- Set a value with a Duration ---
set("session", "v1", Duration.ofMinutes(10))
getExpire("session")                       = 600
getExpire("session", TimeUnit.MINUTES)     = 9
$ redis-cli -p 6390 --no-raw TTL session
(integer) 600

--- A plain set() afterwards throws the TTL away ---
set("session", "v2")
getExpire("session") = -1
$ redis-cli -p 6390 --no-raw TTL session
(integer) -1

--- Put a TTL back, then overwrite while keeping it ---
expire("session", Duration.ofMinutes(10))
set("session", "v3", Expiration.keepTtl())
getExpire("session") = 600, value = v3
$ redis-cli -p 6390 --no-raw TTL session
(integer) 600

--- Sub-second precision ---
set("short", "v", Duration.ofMillis(1500))
getExpire("short", TimeUnit.MILLISECONDS) <= 1500: true
getExpire("short") in seconds = 1

--- persist() removes a TTL ---
persist("session")
getExpire("session") = -1

--- Expiry actually happens ---
set("blink", "v", Duration.ofMillis(300))
get("blink") straight away = v
get("blink") later          = null
$ redis-cli -p 6390 --no-raw EXISTS blink
(integer) 0

--- setIfAbsent with a TTL is the smallest lock ---
setIfAbsent("lock:job", "worker-1", 30s) = true
setIfAbsent("lock:job", "worker-2", 30s) = false
$ redis-cli -p 6390 --no-raw GET lock:job
"worker-1"
$ redis-cli -p 6390 --no-raw TTL lock:job
(integer) 30
