PEXPIRE(3valkey)

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

✨ PEXPIRE ✨

okay so bestie, quick vibe check: this is pexpire, a page straight out of the Valkey Command Manual (yes, the 3valkey girlies know πŸ’…). She sets the expiration time of a key β€” but in milliseconds, not seconds. tiny units, big power. periodt.


🌸 NAME 🌸

pexpire β€” sets the expiration time of a key in milliseconds. ⏱️✨


πŸŽ€ SYNOPSIS πŸŽ€

PEXPIRE key milliseconds [NX | XX | GT | LT]

look at her. so simple. so iconic. 🫢


πŸ’– DESCRIPTION πŸ’–

no cap, this command works exactly like valkey-expire(7) EXPIRE β€” same energy, same everything β€” except the time to live of the key is given in milliseconds instead of seconds. that's literally the whole plot twist. the way that one unit change carries the entire command… obsessed. 😭


✨ OPTIONS ✨

the PEXPIRE command supports a set of options, and yes she has range:

  • NX β€” set expiry only when the key has no expiry. like, only if nobody's claimed the TTL yet.
  • XX β€” set expiry only when the key has an existing expiry. she only updates if there's already a timer running.
  • GT β€” set expiry only when the new expiry is greater than the current one. bigger only, bestie πŸ“ˆ
  • LT β€” set expiry only when the new expiry is less than the current one. smaller only πŸ“‰
important tea 🍡: a non-volatile key is treated as an infinite TTL for the purpose of GT and LT. and heads up β€” GT, LT and NX are mutually exclusive. pick one, you can't have all three. she's not polyamorous about this.

πŸ’Œ REPLY πŸ’Œ

one of the following, and it's giving valkey-protocol(7) energy:

  • valkey-protocol(7) Integer reply: 0 if the timeout was not set. for example, if the key doesn't exist, or the operation got skipped because of the arguments you provided. 😭
  • valkey-protocol(7) Integer reply: 1 if the timeout was set. slay. πŸ‘‘

πŸ”₯ COMPLEXITY πŸ”₯

O(1)

constant time, no dragging, she's fast fr fr. ⚑


🌈 ACL CATEGORIES 🌈

@fast @keyspace @write


πŸ¦‹ HISTORY πŸ¦‹

  • available since: 2.6.0
  • changed in 7.0.0: added options: NX, XX, GT and LT.

she's been around a minute, but she keeps reinventing herself. main character energy only. ✨


πŸ’… EXAMPLES πŸ’…

127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> PEXPIRE mykey 1500
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) 1
127.0.0.1:6379> PTTL mykey
(integer) 1480
127.0.0.1:6379> PEXPIRE mykey 1000 XX
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) 1
127.0.0.1:6379> PEXPIRE mykey 1000 NX
(integer) 0
127.0.0.1:6379> TTL mykey
(integer) 1

see how NX came back with (integer) 0? because the key already had an expiry and she said "not today." 🚫✨


πŸ‘‘ SEE ALSO πŸ‘‘

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), pexpireat(3valkey), pexpiretime(3valkey), pttl(3valkey), randomkey(3valkey), rename(3valkey), renamenx(3valkey), restore(3valkey), scan(3valkey), sort(3valkey), sort_ro(3valkey), touch(3valkey), ttl(3valkey), type(3valkey), unlink(3valkey), wait(3valkey), waitaof(3valkey)

the whole friend group. go say hi. 🫢


and that's the page, bestie!! πŸ’– this version is from valkey 9.0.0, dated 2025-10-21 β€” so go forth, set those millisecond expiries, and remember: TTLs are temporary but the knowledge is forever. mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from