Database

Redis CLI

Redis CLI commands for in-memory data store management.

41 commands
Windows MacOS Linux
#redis #caching

Connection

Connect to local Redis on default port
redis-cli
Connect to a remote Redis host
redis-cli -h 192.168.1.10 -p 6379
Connect with password authentication
redis-cli -a mypassword
Connect and select database number 2
redis-cli -n 2
Connect with TLS encryption
redis-cli --tls --cert ./client.crt --key ./client.key
Test connectivity and get PONG response
redis-cli PING

String Operations

Set a string value for a key
SET mykey "hello"
Get the value of a key
GET mykey
Set multiple keys at once
MSET key1 "val1" key2 "val2"
Get multiple key values at once
MGET key1 key2
Increment an integer value by one
INCR counter
Set a key with expiration in seconds
SETEX session 3600 "data"
Append a string to existing value
APPEND mykey " world"

List Operations

Push an element to the left of a list
LPUSH queue "task1"
Push an element to the right of a list
RPUSH queue "task2"
Remove and return the leftmost element
LPOP queue
Remove and return the rightmost element
RPOP queue
Get all elements in a list
LRANGE queue 0 -1
Get the number of elements in a list
LLEN queue

Hash Operations

Set a field in a hash
HSET user:1 name "Alice"
Get a field value from a hash
HGET user:1 name
Get all fields and values of a hash
HGETALL user:1
Set multiple hash fields at once
HMSET user:1 name "Alice" age "30"
Delete a field from a hash
HDEL user:1 age
List all field names of a hash
HKEYS user:1

Set Operations

Add members to a set
SADD tags "redis" "cli"
List all members of a set
SMEMBERS tags
Check if a value is in a set
SISMEMBER tags "redis"
Get the number of members in a set
SCARD tags
Get the intersection of two sets
SINTER set1 set2
Get the union of two sets
SUNION set1 set2

Key Management

Find all keys matching a pattern
KEYS 'user:*'
Iteratively scan keys matching a pattern
SCAN 0 MATCH 'user:*' COUNT 100
Delete a key
DEL mykey
Set key expiration to 300 seconds
EXPIRE mykey 300
Check remaining time-to-live of a key
TTL mykey
Get the data type of a key
TYPE mykey
Return the number of keys in the database
DBSIZE

Quick Commands

Connect to the local Redis instance
redis-cli
Store a string value with a key in Redis
SET mykey "hello"
Find all keys matching a wildcard pattern
KEYS 'user:*'