Redis data basic operation

Posted by gotornot on Sat, 04 Jan 2020 04:12:20 +0100

@ overview

  • Some common operations of key value pairs and other data types are listed here;
  • For more operation commands and definitions, please refer to: http://redis.cn/commands.html

@String operation example

set name bill //set key value
setex name 20 bill //Key expires in 20 seconds
mset age 60 gender male //Store multiple key values at once
get name //Return null if it does not exist
mget name age //Take multiple key values at one time
incr/decr age //Add and subtract 1 from age
incrby/decrby age 20 //Add and subtract age by 20
append name gates //Append content to original value
strlen key //Get value length

@key operation example

keys * //Display all keys, use with caution, cause stuck when data volume is large
keys a* // Show all keys starting with a
exists name age //Judge the existence of name and age
type name //View the type of value
del name age //Delete key
expire name 30 //name key expires in 30 seconds
ttl name //View expiration time of key value
persist name // Expiration time of cancel key
rename name nickname // Rename key (this is not safe and easy to overwrite existing data)
renamenx name nickname // Rename without overwriting other keys (nx=not exists,x suffix = exists)

@Example of hash operation

  • hash is to store multiple fields and values in a key, similar to the object;
hset p1 name bill // Set the name of p1 object to bill
hset p1 age 18 // Set the age of p1 object to 18
hmset p2 name jobs age -1 //Set the name of p2 object to jobs and age to - 1
hget p1 name // Get the name attribute value of p1
hmget p1 name age // Get the name and age of p1 at the same time
hgetall p1 // Get all fields and values of p1
hkeys p1 // Get all fields of p1
hvals p1 // Get all field values of p1
hlen p1 // Get the number of all fields in p1
hexists p1 name // Determine whether there is a name field in p1
hdel p1 name // Delete the name field in p1
hdel p2 name age // Delete the name and age fields in p2
hstrlen p1 age // Length of age in p1

@Example of list operation

  • In redis, also known as queue, you can get elements by subscript, or pop or put them from the beginning to the end;
lpush mlist 2 // Append element 2 from left to list mlist
rpush mlist 3 // Append element 3 to list mlist from the right
linsert mlist after 3 4 // Append element 4 after 3 in list mlist
lset mlist 1 200 // Set the first element in the list mlist to 200 (subscript starts from 0)
lrange mlist 0 3 // View elements of subscript [0 to 3] in list mlist
lrange mlist 0 -1 //From head to tail
lpop mlist // Pop a value from the left side of the list
rpop mlist // Pop up a value from the right side of the list
ltrim mlist 0 1 // Prune elements other than mlist subscript [0,1]
llen mlist // Number of list elements
lindex mlist 0 // Get element of subscript 0 in mlist

@set operation example

  • Unordered element set, often used to find intersection, union, difference set, etc;
sadd mset 1 2 3 4 // Add elements 1,2,3,4 to set mset
smembers mset // View elements in collection mset
scard mset // Number of query collection elements
sinter mset mset2 //Finding the intersection of Mset and mset2
sdiff mset mset2 //Find the difference set of Mset compared with mset2 (the part of Mset with mset2 without mset2)
sunion mset mset2 // Finding the union of Mset and mset2
sismember mset 3 //Judge whether 3 is an element in mset

@Ordered set zset

  • Elements can be arranged by weight;
zadd mzset 10 bill 9 jobs 6 jackma // Add multiple values and corresponding weights
zrange mzset 0 -1 // Returns all elements from the first to the last, in ascending order of weight
zcard mzset // Element number
zcount mzset 6 10 // Number of elements between weights 6-10
zscore mzset jackma // Returns the weight of jackma in mzset

Topics: Redis Attribute