SETEX(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– SETEX β€” the "set it and ✨time it out✨" command (3valkey)

okay bestie, imagine you wanna set a key AND have it self-destruct after a bit. that's this. no cap.


✨ NAME ✨

setex β€” sets the string value and the expiration time of a key. creates the key if it doesn't exist. like, it's giving "here's your value, and also a countdown timer, you're welcome" πŸ’…


🌸 SYNOPSIS 🌸

SETEX key seconds value

πŸ¦‹ DESCRIPTION πŸ¦‹

so the whole vibe is: set key to hold the string value, and set key to time out after a given number of seconds. this command is literally equivalent to:

SET key value EX seconds

an error gets returned when seconds is invalid. like, don't try anything funny with that number, the server WILL clock it. πŸ’€

πŸŽ€ Alternative

just use SET with the EX argument. same energy, different outfit.


πŸ’Œ REPLY πŸ’Œ

valkey-protocol(7) Simple string reply: OK.


⭐ COMPLEXITY ⭐

O(1) β€” constant time, we love a low-effort queen.


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@slow @string @write


🌈 HISTORY 🌈

  • available since: 2.0.0 β€” a true legacy icon, been here since forever fr fr

πŸ“ EXAMPLES πŸ“

127.0.0.1:6379> SETEX mykey 10 "Hello"
OK
127.0.0.1:6379> TTL mykey
(integer) 10
127.0.0.1:6379> GET mykey
"Hello"

see how the TTL is sitting at 10? and then GET still hands you back "Hello"? that's the whole magic trick, bestie. ate. πŸ”₯


πŸ’œ SEE ALSO πŸ’œ

TTL


🧚 SEE ALSO 🧚

append(3valkey), decr(3valkey), decrby(3valkey), delifeq(3valkey), get(3valkey), getdel(3valkey), getex(3valkey), getrange(3valkey), getset(3valkey), incr(3valkey), incrby(3valkey), incrbyfloat(3valkey), lcs(3valkey), mget(3valkey), mset(3valkey), msetnx(3valkey), psetex(3valkey), set(3valkey), setnx(3valkey), setrange(3valkey), strlen(3valkey), substr(3valkey)


lowkey a whole friend group of string commands up there. go say hi. 🫢

and that's the tea, bestie πŸ’… this page is from the Valkey 9.0.0 manual, dated 2025-10-21, and honestly? iconic. periodt. βœ¨πŸŽ€πŸ’–

πŸ“¦ where this page comes from