Skip to content
Redis

Strings & Counters

Set, get, increment, and expire string values.

#string#counter#cache

Code

redis
# Basic set/get with expiration
SET session:abc "user-data" EX 3600
GET session:abc
TTL session:abc
PERSIST session:abc

# Conditional set
SET cache:lock "1" NX EX 10

# Counters
INCR page:home:views
INCRBY page:home:views 5
DECR stock:item:42
SET stock:item:42 100

# Append and substring
APPEND greeting "World"
STRLEN greeting
GETRANGE greeting 0 4

# MGET for batch reads
MGET k1 k2 k3