How Redis Implements SETNX, SETEX, and PSETEX

Overview
After understanding how the Redis SET command works internally, the behavior of SETNX, SETEX, and PSETEX becomes fairly straightforward. These three commands follow the same general path: Redis first calls tryObjectEncoding to optimize the value object, and then uses setGenericCommand to write the key-value pair into the database.
The main difference is that these commands do not need to parse additional optional arguments in the way the more general SET command does. Their behavior is already determined by the command form itself.
SETNX
Command format:
setnx key value
SETNX writes a key-value pair to the database only when the specified key does not already exist.
At the source-code level, Redis calls setGenericCommand with the flags parameter set to OBJ_SET_NX. This flag tells the generic set logic that the operation should succeed only if the key is absent from the database.
SETEX
Command format:
setex key seconds value
SETEX stores the key-value pair and also assigns an expiration time to the key, measured in seconds.
When Redis invokes setGenericCommand for this command, the flags value is set to OBJ_SET_NO_FLAGS, meaning the command does not care whether the key already exists. The expiration unit is set to UNIT_SECONDS, so the timeout value is interpreted as seconds.
PSETEX
Command format:
psetex key milliseconds value
PSETEX is similar to SETEX, but its expiration time is specified in milliseconds rather than seconds.
Internally, Redis also calls setGenericCommand with flags set to OBJ_SET_NO_FLAGS, so the existence of the key is not a condition for execution. The expiration unit is set to UNIT_MILLISECONDS, which causes Redis to treat the timeout argument as a millisecond value.