# Set a single field with expirationresult = redis.hsetex("myhash", "field1", "Hello", ex=60)assert result == 1
# Set fields with 1 hour expirationresult = redis.hsetex( "user:123", values={"name": "John", "email": "john@example.com"}, ex=3600)assert result == 2
# Set fields only if the hash doesn't existresult = redis.hsetex( "user:456", values={"name": "Jane", "age": "25"}, fnx=True)assert result == 2# Try again - will return 0 since hash now existsresult = redis.hsetex( "user:456", values={"email": "jane@example.com"}, fnx=True)assert result == 0
# First create the hashredis.hset("session:abc", "token", "xyz")# Update only if hash existsresult = redis.hsetex( "session:abc", values={"user": "john"}, fxx=True)assert result == 1 # Hash exists, field added# Try on non-existent hashresult = redis.hsetex( "session:nonexistent", values={"user": "jane"}, fxx=True)assert result == 0 # Hash doesn't exist
import time# Set fields with 30 second expirationresult = redis.hsetex( "cache:data", values={"value": "cached data", "timestamp": str(int(time.time()))}, px=30000)assert result == 2
import time# Set expiration to specific timestampfuture_time = int(time.time()) + 7200 # 2 hours from nowresult = redis.hsetex( "temp:data", values={"info": "temporary information"}, exat=future_time)assert result == 1
import time# Set expiration to specific timestamp in millisecondsfuture_time = int(time.time() * 1000) + 300000 # 5 minutes from nowresult = redis.hsetex( "session:xyz", values={"token": "abc123", "user": "john"}, pxat=future_time)assert result == 2
import time# Set fields only if hash doesn't exist, with 1 hour expirationresult = redis.hsetex( "user:789", values={ "name": "Alice", "email": "alice@example.com", "created": str(int(time.time())) }, fnx=True, ex=3600)assert result == 3
# First set fields with expirationredis.hsetex("cache:data", values={"value": "cached"}, ex=300)# Later update fields while retaining the existing TTLresult = redis.hsetex("cache:data", values={"updated": "yes"}, keepttl=True)assert result == 1# Verify TTL is still 300 seconds (or less if time passed)ttl = redis.ttl("cache:data")assert ttl > 0 and ttl <= 300 # TTL was retained
# Just set fields without expiration or conditionsresult = redis.hsetex( "data:simple", values={"field1": "value1", "field2": "value2"})assert result == 2
Hash
HSETEX
Set hash fields with expiration support.
The HSETEX command sets the specified fields with their values and optionally sets their expiration time or TTL. It supports conditional operations to control when fields should be set.
0 if no fields were set, 1 if all the fields were set.
# Set a single field with expirationresult = redis.hsetex("myhash", "field1", "Hello", ex=60)assert result == 1
# Set fields with 1 hour expirationresult = redis.hsetex( "user:123", values={"name": "John", "email": "john@example.com"}, ex=3600)assert result == 2
# Set fields only if the hash doesn't existresult = redis.hsetex( "user:456", values={"name": "Jane", "age": "25"}, fnx=True)assert result == 2# Try again - will return 0 since hash now existsresult = redis.hsetex( "user:456", values={"email": "jane@example.com"}, fnx=True)assert result == 0
# First create the hashredis.hset("session:abc", "token", "xyz")# Update only if hash existsresult = redis.hsetex( "session:abc", values={"user": "john"}, fxx=True)assert result == 1 # Hash exists, field added# Try on non-existent hashresult = redis.hsetex( "session:nonexistent", values={"user": "jane"}, fxx=True)assert result == 0 # Hash doesn't exist
import time# Set fields with 30 second expirationresult = redis.hsetex( "cache:data", values={"value": "cached data", "timestamp": str(int(time.time()))}, px=30000)assert result == 2
import time# Set expiration to specific timestampfuture_time = int(time.time()) + 7200 # 2 hours from nowresult = redis.hsetex( "temp:data", values={"info": "temporary information"}, exat=future_time)assert result == 1
import time# Set expiration to specific timestamp in millisecondsfuture_time = int(time.time() * 1000) + 300000 # 5 minutes from nowresult = redis.hsetex( "session:xyz", values={"token": "abc123", "user": "john"}, pxat=future_time)assert result == 2
import time# Set fields only if hash doesn't exist, with 1 hour expirationresult = redis.hsetex( "user:789", values={ "name": "Alice", "email": "alice@example.com", "created": str(int(time.time())) }, fnx=True, ex=3600)assert result == 3
# First set fields with expirationredis.hsetex("cache:data", values={"value": "cached"}, ex=300)# Later update fields while retaining the existing TTLresult = redis.hsetex("cache:data", values={"updated": "yes"}, keepttl=True)assert result == 1# Verify TTL is still 300 seconds (or less if time passed)ttl = redis.ttl("cache:data")assert ttl > 0 and ttl <= 300 # TTL was retained
# Just set fields without expiration or conditionsresult = redis.hsetex( "data:simple", values={"field1": "value1", "field2": "value2"})assert result == 2