Upstash Documentation

PSETEX

Set value with ms expiration.
2 min read
Warning

Prefer SET with the PX argument in new code: SET <key> <value> PX <milliseconds>.

Use PSETEX to set a value together with a lifetime in milliseconds.

The value and the expiration are applied in the same atomic step, so the key never exists without its expiration, which is the point of the command: setting the value and then expiring it separately leaves a window where a crash makes the key permanent. A lifetime of zero or less returns an error.

It is the millisecond form of SETEX, and SET with the PX option is the more flexible way to write the same thing.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
<milliseconds>YesNoLifetime in milliseconds.
<value>YesNoString value to store.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Simple string OK
RESP3Simple string OK
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs