Location>code7788 >text

Redis common instructions (details)

Popularity:501 ℃/2025-01-17 11:11:41
# Redis common commands

## Basic commands
### Startup and connection
```bash
# Start the Redis service
redis-server

# Connect to Redis client
redis-cli
```

### Basic operations
```bash
# Check whether the Redis service is running
ping
#Return result: PONG

# Get Redis version information
info

# View all keys
keys *

# delete key
del <key>

# Check if the key exists
exists <key>
#Return result: 1 means exists, 0 means does not exist

#Set the expiration time of the key
expire <key> <seconds>

# Check the remaining survival time of the key
ttl <key>
# -1 means that the expiration time is not set, -2 means that the key does not exist

# Persist a key and remove its expiration time
persist <key>
```

## String
```bash
# Set key value
set <key> <value>

# Get key value
get <key>

# Set the key value and set the expiration time (seconds)
setex <key> <seconds> <value>

# Set value only if key does not exist
setnx <key> <value>

# Get substring
getrange <key> <start> <end>

#Append value to existing key
append <key> <value>

# Get and update values
getset <key> <value>

# self-increment/self-decrement
tincr <key>
decr <key>
incrby <key> <number>
decrby <key> <number>
```

## Hash
```bash
# Set the hash field value
hset <key> <field> <value>

# Get the hash field value
hget <key> <field>

#Set multiple field values
hmset <key> <field1> <value1> <field2> <value2> ...

# Get multiple field values
hmget <key> <field1> <field2> ...

# Get all fields and values
hgetall <key>

# Delete fields
hdel <key> <field>

# Check if the field exists
hexists <key> <field>

# Get the number of fields
hlen <key>

# Get all fields
hkeys <key>

# Get all values
hvals <key>
```

## List
```bash
#Insert elements from the left
lpush <key> <value1> <value2> ...

#Insert elements from the right
rpush <key> <value1> <value2> ...

# Pop elements from the left
lpop <key>

# Pop elements from the right
rpop <key>

# Get the length of the list
llen <key>

# Get the elements in the specified range
lrange <key> <start> <stop>

# Set value by index
lset <key> <index> <value>

# Remove specified value
lrem <key> <count> <value>
```

## Set
```bash
# Add elements
sadd <key> <member1> <member2> ...

# Delete element
srem <key> <member>

# Check if the element exists
sismember <key> <member>

# Get all elements of the collection
smembers <key>

# Get the size of the collection
scard <key>

# Get one or more elements randomly
srandmember <key> [count]
```

## Sorted Set
```bash
# Add elements
zadd <key> <score1> <member1> <score2> <member2> ...

# Get elements in the specified range (sorted by score)
zrange <key> <start> <stop> [WITHSCORES]

# Get the elements of the specified range (in reverse order by score)
zrevrange <key> <start> <stop> [WITHSCORES]

# Delete element
zrem <key> <member>

# Get the size of the collection
zcard <key>

# Get member's score
zscore <key> <member>

# Get elements within the specified score range
zrangebyscore <key> <min> <max> [WITHSCORES]
```

## Publish and Subscribe (Pub/Sub)
```bash
# Subscribe to channel
subscribe <channel>

# Post a message
publish <channel> <message>
```

## Transaction
```bash
# Start transaction
multi

#Execute transaction
exec

# Abort transaction
discard
```

## Script (Lua)
```bash
# Execute Lua script
eval <script> <numkeys> <key1> <key2> ... <arg1> <arg2> ...

#Load script into cache
script load <script>

# Check if the script is in cache
script exists <sha1>

# Kill the currently running script
script kill
```

## other
```bash
#Clear the current database
flushdb

#Clear all databases
flushall

#Switch database
select <db-index>

# Save data to disk
save

# Asynchronously save data to disk
bgsave

# View slow query logs
slowlog get [count]