TTL(3valkey)
π TTL β the "how long do you have, bestie" command π
okay so this is the TTL page from the valkey command manual (that's the 3valkey section, iykyk) and honestly? it's a short one, but it's so useful. let's get into it π¦
β¨ NAME β¨
ttl β Returns the expiration time in seconds of a key.
like. literally just "how many seconds does this key have left to live." that's the whole gig. π
πΈ SYNOPSIS πΈ
TTL key
one arg, no drama. key is your key. periodt.
π DESCRIPTION π
okay so TTL gives you back the remaining time to live of a key that has a timeout. it's giving introspection, and that's iconic β it means a Valkey client can just vibe check how many seconds a given key is still gonna be part of the dataset before it β¨evaporatesβ¨.
now, the error-ish replies. bestie, these are important, don't skip:
- The command returns
-2if the key does not exist. π (it's just not there, sorry) - The command returns
-1if the key exists but has no associated expire. (she's here and she's staying forever, no cap)
also!! go look at the valkey-pttl(7) PTTL command β it's the same info but with milliseconds resolution, for when you need to be extra precise. obsessed with the precision tbh.
π REPLY π
you get one of the following, and they're all valkey-protocol(7) Integer replies:
valkey-protocol(7)Integer reply: TTL in seconds. β±οΈvalkey-protocol(7)Integer reply:-1if the key exists but has no associated expiration. πvalkey-protocol(7)Integer reply:-2if the key does not exist. π
π₯ COMPLEXITY π₯
constant time, baby. she's fast. no scrolling through the whole keyspace, she just knows. π¨
π ACL CATEGORIES π
@fast @keyspace @read
β HISTORY β
the glow-up timeline:
- Available since:
1.0.0π - Changed in
2.8.0: Added the-2reply. (before that, no-2. the more you know π«)
π«Ά EXAMPLES π«Ά
watch her work:
127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> EXPIRE mykey 10
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) 10
set it, gave it 10 seconds, asked how long it had left, got 10 back. ate. π
π SEE ALSO π
go be nosy, here's the whole squad:
copy(3valkey), del(3valkey), dump(3valkey), exists(3valkey), expire(3valkey), expireat(3valkey), expiretime(3valkey), keys(3valkey), migrate(3valkey), move(3valkey), object(3valkey), object-encoding(3valkey), object-freq(3valkey), object-help(3valkey), object-idletime(3valkey), object-refcount(3valkey), persist(3valkey), pexpire(3valkey), pexpireat(3valkey), pexpiretime(3valkey), pttl(3valkey), randomkey(3valkey), rename(3valkey), renamenx(3valkey), restore(3valkey), scan(3valkey), sort(3valkey), sort_ro(3valkey), touch(3valkey), type(3valkey), unlink(3valkey), wait(3valkey), waitaof(3valkey)
k that's the whole page!! remember:-1means "no expiration, she's immortal",-2means "girl, that key does not exist". don't mix them up or it's over for you π§
and that's a wrap on TTL(3valkey), from the 9.0.0 valkey command manual, dated 2025-10-21. ty for reading, mwah π
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms