Introduction to NoSQL (4)
Introduction to NoSQL (4)
Common operations of Redis
Common Redis operations-string
Set key3 zsset key3 lisi / / the second assignment will override setnx key3 wangwu / / return 0, then key3 exists setnx key4 zhaoliu / / returns 1, then key4 does not exist to create key4setex key4 100s tianqi / / set the expiration time of key4 to 100s, the value is tianqi, if key4 exists, override
Common Redis operations-list
Lpush listb a / / add an element from the left lpop listb a / / take an element from the left rpush listb b / / add an element from the right rpop listb / / take the first element from the right linsert lista before a 1 / / insert (value) 1lset lista 3 3 / / change the value corresponding to subscript 3 to 3lindex lista 3 / / check the numerical value corresponding to subscript 3 / / check how many elements there are in the linked list lista
Common Redis operations-set
Sadd seta aaa / / put elements into the collection seta aaasmembers seta / / View all elements of the seta collection srem seta aaa / / Delete aaaspop seta / / randomly fetch an element and delete the sdiff seta setb / / difference set Take seta as the standard sdiffstore setc seta setb / / to find the difference between seta and setb, store the difference result in setc sinter seta setb / / intersect sinterstore setd seta setb / / store the intersection in setdsunion seta setb / / union sunionstore sete seta setb / / and store it in setesismember seta aaa / / to determine whether an element belongs to a set srandmember seta / / randomly take an element, but do not delete it.
Common Redis operations-zset
Zadd zsetx 100 zhangsan / / add an element zadd zsetx 85 lisi 90 wangwu 58 zhaoliu / / add multiple elements zrange zsetx 0-1 / / View all elements zrange zsetx 0-1 withscores / / View all elements with scores View zrem zsetx zhangsan / / Delete specified element zrank zsetx zhaoliu / / View subscript zrevrank zsetx zhaoliu / / check subscript zrevrange zsetx 0-1 / reverse of specified element Ask to view all elements zcard zsetx / / View the number of elements in the specified zset zcount zsetx 60 / 100 / View the number of elements between 60-100 / / View the elements between 60-100 zremrangebyrank zsetx 0 1 / delete the element corresponding to the subscript 0-1 / delete the element with the score 80-100 / delete the element with the score 80-100
Common Redis operations-hash
Hset user1 name apeng / / establish hashhset user1 age 20hset user1 job ithgetall user1hmset user2 name aling age 25 job it / / batch create key value pair hmget user2 name age jobhdel user2 job / / Delete specified key value pair hkeys user2 / / View all keyhvals user2 / / View all value values hlen user2 / / View there are several key value pairs
Redis common operations (key values)
Keys * / check all keykeys my* / / Fuzzy matching exists name / / return 1 if there is name, otherwise return 0del key1 / / delete a keyexpire key1 100s / / set key1 100s to expire ttl key / / check how long the key expires in s, return-2 when key does not exist, return-1 when key exists but do not set the remaining survival time. Otherwise, return the remaining lifetime of key select 0 / / switch to 0 database move age 1 / / move the age in the current library to the database 1persist key1 / / cancel the expiration time of key1 randdomkey / / return the type of random keyrename oldname newname / / rename keytype key1 / / return key
Redis common operations (services)
Dbsize / / returns the number of key in the current database info / / returns the redis database status information flushdb / / clears all keys in the current database flushall / / clears all keybgsave / / saves data in all databases to the rdb file, and runs save / / in the background. But run config get * / / in the foreground to get all configuration parameters config get dir / / get configuration parameters config set dir / / change configuration parameters data recovery: first define or determine the dir directory and dbfilename, then put the backed up rdb files under the dir directory, restart the redis service to restore the data