Redis data type String operation command
1. append append string
append name 2222
Copy after login
2. strlen gets the key string length
strlen name
Copy after login
3. Self-increment, self-decrement
Article views, likes can be used This realization.
incr agedecr age
Copy after login
Note that this must be a number to proceed, so a key is reset.
The String type can store not only strings but also numbers.
If you want to bring the step size:
incrby age 5decrby age 8
Copy after login
##4. String range
getrange name 1 3
Copy after login
getrange name 0 -1
Copy after login
View all, similar to the string interception operation in python.
5. Replace the string
Start replacing the string at the specified position
setrange name 0 test
Copy after login
6. Set the value and its expiration time
setex
setex mykey 60 redis
Copy after login
Set the value and its expiration time for the specified key. If key already exists, the SETEX command will replace the old value.
setnx
The Setnx(SET if Not eXists) command sets the specified value for the key when the specified key does not exist. This is often used in distributed locks.
setnx mykey redis333
Copy after login
#key exists, and the setting fails.
7. Batch operations
1. mset, mget
mset, set multiple at one time.
mset k1 v1 k2 v2 k3 v3
Copy after login
mget, get multiple at one time.
mget k1 k2 k3
Copy after login
2. msetnx
Note that when setting multiple values here, as long as one of them fails, none of them will succeed.
msetnx k1 v1 k4 v4
Copy after login
8. Set a json object
In actual applications, you may often need to save an object, so you can use colon: in redis Make some clever designs.
For example, if you want it now
{name: pingguo, age:22}set it to a
user1, you can do this:
mset user:1:name pingguo user:1:age 22mget user:1:name user:1:agemset user:1:name pingguo user:1:age 22
mget user:1:name user:1:age
Copy after login
9. Getset first gets and then sets
Just like the literal meaning, the value will be obtained first and then set.
If the value does not exist, return
nil. If it exists, get the original value and set the new value.
getset db mongodb
Copy after login
The above is the detailed content of What are the common operation commands for Redis's basic data type String?. For more information, please follow other related articles on the PHP Chinese website!