Redis study notes, redis Getting Started Guide
I. Data structures:1. String: key-> value(1) Add: set key value/setnx key value (0 is returned if no value exists)/setex key time value (ADD and set the effective time) /setrange key index value (replace the value corresponding to the key with the value starting from the index position)/mset key1 value1 key2 value2 (if multiple values are set at a time, 0 is returned for failure) /msetnx key1 value1 key2 value2 (same as the previous one, but does not overwrite the existing one); (2) get: get key/getset key value (reset value, and return the old value) /getrange key index1 index2 (return the substring of the value corresponding to the key: index1 to index2)/mget key1 key2 key3 (obtain multiple values at a time); (3) addition and subtraction: incr key (if the key value is of the int type, add one; if the key value is not of the int type Type, return error; if the key does not exist, set the key value to 1, that is, the original value is considered as 0)/incrby key number (the key value is added with number, the other is the same as above) /decr key/decrby key number (this is subtraction, with the same specification as incr/incrby); (4) string operation: append key val (append val to the value corresponding to the key) /strlen key (returns the length of the value corresponding to the key) Note: The subscript on the left of the string starts from 0, and the subscript on the right starts from-1.2. hash: map. Each hash table has one or more key-value pairs.(1) Add: hset myhash key value (add key-value pairs to key-> value in hash table myhash)/hsetnx myhash key value (same as hset, but not overwrite) /hmset myhash key1 value1 key2 value2 (add multiple key-value pairs to myhash in the hash table); (2) Get: hget myhash key (get the value of key in myhash in the hash table) /hmet myhash key1 key2 key3 (obtain the values of multiple keys in the hash table myhash)/hincrby myhash key number (add the value corresponding to the key in the hash table myhash); (3) other operations: hexists myhash key (to determine whether a key exists in myhash is a key, 0 indicates none, and 1 indicates yes)/hlen myhash (number of myhash keys returned) /hdel myhash key (delete key-value pairs whose key is the key in myhash)/hkeys myhash (return all keys in myhash)/hvals myhash (return all values in myhash) /hgetall myhash (return all keys and values in myhash)3. list:List string by value(1) Add: lpush mylist value (add value to mylist header)/rpush mylist value (add value to end of mylist) /linsert mylist before value1 value (insert value to the front of value1 in mylist)/lset mylist index value (reset the value at the index position in mylist to value) (2) obtain: lrange mylist index1 index2 (obtain index1 to index2 in list)/lindex mylist index (returns the value of mylist subscript index)/llen mylist (returns the length of mylist) (3) Delete: lrem mylist count value (delete | count | elements whose values are value in mylist, count> 0: delete from start to end; count <0: delete from end to end; count = 0: delete all)/ltrim mylist index1 index2 (only keep the value from index1 to index2)/lpop mylist (delete the mylist header and return it) /rpop mylist (delete the end of mylist and return)/rpoplpush mylist1 mylist2 (move the end of mylist1 to the header of mylist2)4. set: set composed of Values(1) Add: sadd myset value (2) Get: smembers myset (returns all values of myset)/sdiff myset1 myset2 (returns the difference set of two sets) /sdiffstore myset myset1 myset2 (store the difference set of myset1 and myset2 in myset)/sinter myset1 myset2 (return the intersection of the two sets) /sinterstore myset myset1 myset2 (store the intersection of myset1 and myset2 in myset)/sunion myset1 myset2 (return the union of two sets) /sunionstore myset myset1 myset2 (store the Union set of myset1 and myset2 in myset)/scard myset (return the number of elements in myset)/sismember myset value (test whether the value is an element in myset, 1 is yes, 0 is no)/srandmember myset (random return of an element in myset, but not delete) (3) Delete: srem myset value (delete the element with the value) /spop myset (randomly Delete and return an element in myset)/smove myset1 myset2 value (remove value from myset1 and add it to myset2)5. sorted set (zset): set with sorting(1) Add: zadd myzset score value (add value to myzset, and score is used for sorting. If value already exists, update score) (2) get: zrange myzset index1 index2 withscores (returns the Element and Its score value with the subscript range of index1 to index2 in myzset, in ascending order of score)/zrank myzset value (returns the ranking of value in myzset, count from 0)/zrevrank myzset value (returns the reverse ranking of value in myzset, counting from 0)/zrevrange myzset index1 index2 withscores (descending order of zrange) /zrangebyscore myzset a B withscores (returns the score from myzset in the range from a to B and the score) /zcount myzset a B (returns the number of values from a to B in myzset)/zcard myzset (returns the number of elements) /zscore myzset value (returns the score value corresponding to the value in myzset) (3) Delete: zrem myzset value (deletes the element whose value is the value in myzset) /zremrangebyrank myzset index1 index2 (delete the element in myzset that ranks between index1 and index2)/zremrangebyscore myzset a B (delete the Element Between score a and score B in myzset) (4) add or subtract score: zincrby myzset number value (add score with value to number) 2. Common redis commands: 1. keys pattern: return all keys of a given pattern. pattern is a regular expression.
2. exists key: determine whether there is a key3, del key: delete key4, expire key time: Set the expiration time of a key 5. move key db: move a key to the database 6. ttl key: how long does the returned key expire (-1 indicates that there is no expiration time) 7. peisist key: remove key expiration time 8. randomkey: return a random key9, rename oldkey newkey: rename oldkey to newkey10, type value: Return value type 11, dbsize: return the number of keys of the current database 12, flushdb: delete all key13 and flushall of the current database: delete all key14 and info of all databases: get Server Information and Statistics 15. config get: get server configuration information ps: the redis Database Number is 0-15, and the default value is 0.