Redis common commands, everything you need is here, come and collect

data type command

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).

1. String

String is the most basic type of redis, which can store up to 512MB of data. The String type is binary safe, that is, it can store any data, such as numbers, pictures, serialized objects, etc. INCR key: the key value is incremented by 1 (the key value must be an integer), DECR key: the key value is incremented and decremented by 1 (the key value must be an integer), GETSET key value: get the key value and return it, and set a new value for the key at the same time.

  • set(key, value): assign the value value to the String named key in the database
  • get(key): Returns the value of the String named key in the database
  • getset(key, value): Assign the last value to the String named key
  • mget(key1, key2,…, key N): Returns the value of multiple Strings in the library
  • setnx(key, value): Add String, name is key, value is value
  • setex(key, time, value): Add String to the library and set the expiration time time
  • mset(key N, value N): Set the values ​​of multiple Strings in batches
  • msetnx(key N, value N): if all Strings named key do not exist
  • incr(key): the operation of incrementing the String named key by 1
  • incrby(key, Integer): Increase N(Integer) for String named key
  • decr(key): the operation of subtracting 1 from the String named key
  • decrby(key, Integer): String named key reduces integer
  • append(key, value): append value to the value of String named key
  • substr(key, start, end): returns the substring of the value of the String named key

2.Hash

A Redis hash is a collection of key-value (key:value) pairs, especially suitable for storing objects.

  • hset(key, field, value): add the element field to the hash named key
  • hget(key, field): returns the value corresponding to the field in the hash named key
  • hmget(key, (fields)): Returns the value corresponding to the field in the hash named key
  • hmset(key, (fields)): add the element field to the hash named key 
  • hincrby(key, field, Integer): Add Integer to the value of the field in the hash named key
  • hexists(key, field): Whether there is a field with the key field in the hash named key
  • hdel(key, field): delete the field whose key is field in the hash named key
  • hlen(key): returns the number of elements in the hash named key
  • hkeys(key): Returns all keys in the hash named key
  • hvals(key): returns the value corresponding to all keys in the hash named key
  • hgetall(key): Returns all keys (fields) and their corresponding values ​​in the hash named key
     

3. List (list)

Redis lists are simple lists of String strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of a list, and you can implement asynchronous queues using lists.

  • rpush(key, value): Add an element whose value is value to the end of the list named key
  • lpush(key, value): Add an element whose value is value to the head of the list named key
  • llen(key): Returns the length of the list named key
  • lrange(key, start, end): Returns the elements between start and end in the list named key
  • ltrim(key, start, end): intercept the list named key
  • lindex(key, index): Returns the element at the index position in the list named key
  • lset(key, index, value): Assign value to the element at the index position in the list named key
  • lrem(key, count, value): delete the element whose value is value in the list of count keys
  • lpop(key): Returns and deletes the first element in the list named key
  • rpop(key): Returns and removes the tail element in the list named key
  • blpop(key1, key2,… key N, timeout): the block version of the lpop command
  • brpop(key1, key2,… key N, timeout): block version of rpop
  • rpoplpush(srckey, dstkey): Returns and removes the tail element of the list named srckey and adds the element to the head of the list named dstkey

4.Set (collection)

Redis's Set is an unordered collection of string type. Sets are implemented through hash tables, so adding, deleting, and searching are all O(1).

  • sadd(key, member): Add the element member to the set named key
  • srem(key, member) : delete the element member in the set named key
  • spop(key) : Randomly returns and deletes an element in the set named key
  • smove(srckey, dstkey, member) : move to set element
  • scard(key) : returns the cardinality of the set named key
  • sismember(key, member) : Whether member is an element of the set named key
  • sinter(key1, key2,…key N) : find the intersection
  • sinterstore(dstkey, (keys)) : find the intersection and save the intersection to the set of dstkeys
  • sunion(key1, (keys)) : union set
  • sunionstore(dstkey, (keys)) : Takes the union and saves the union to the collection of dstkeys
  • sdiff(key1, (keys)) : find the difference
  • sdiffstore(dstkey, (keys)) : find the difference and save the difference to the set of dstkey
  • smembers(key) : returns all elements of the set named key
  • srandmember(key) : randomly returns an element of the set named key

5.zset (sorted set: ordered set)

Redis zset, like set, is also a collection of elements of type string, and does not allow duplicate members.

The difference is that each element is associated with a double-type score (weight), and redis can use this score to sort the members of the set from small to large. The members of a zset are unique, but scores can be repeated.

  • zadd(key, score, member): Add one or more members to an ordered set, or update the scores of existing members
  • zcard(key) : Get the number of members of the sorted set
  • zcount(key min max): Counts the number of members with the specified interval score in the sorted set
  • zincrby(key, increment, member): add increment to the score of the specified member in the sorted set
  • zinterstore(destination, numkeys, key): Computes the intersection of one or more given sorted sets and stores the result set in a new sorted set destination
  • zlexcount(key, min,max): Calculate the number of members in the specified dictionary interval in an ordered set
  • zrange(key, start ,stop): Returns the members in the specified range of the ordered set through the index range
  • zrangeByLex(key, min ,max): Returns the members of the sorted set through the dictionary interval
  • zrangeByScore(key, min ,max): Returns the members in the specified range of the sorted set by score
  • zrank(key, member): returns the index of the specified member in the sorted set
  • zrem(key, member[member...]): remove one or more members from a sorted set
  • zremRangeByLex(key, min,max): removes all members of the given dictionary range in the sorted set
  • zremRangeByRank(key, start, stop): removes all members of the given ranking interval in the sorted set
  • zremRangeByScore(key, min,max): removes all members of the given score interval in the sorted set
  • zrevRange(key, start, stop): Returns the members in the specified range in the sorted set, through the index, the scores are from high to low
  • zrevRangeByScore(key, min ,max): Returns the members in the specified score range in the sorted set, with scores sorted from high to low
  • zrevRange(key, member): Returns the rank of the specified member in the sorted set. The members of the sorted set are sorted in descending order (from large to small)
  • zscore(key, member): Returns the score value of the member in the sorted set
  • zunionStore(destination numkeys, key[key ...]): Computes the union of one or more ordered sets given and stores in a new key

Redis operation command

1. Redis connection

Usually, the official client redis-cli is used to connect to redis. The most commonly used parameters of redis-cli are as follows:

  • redis-cli  -h  127.0.0.1  -p  6379  -a  12345(password)

If there is no redis-cli, you can also use telnet , the connection method is:

  • telnet  127.0.0.1  6379

After the connection is successful, communication with redis has been established. If redis has set a password, password authentication is also required. Use the redis command auth to authenticate:

  • auth  12345(password)

2. Operations on keys

  • keys * : view all keys in the directory
  • keys key_*: View all keys prefixed with "name_" (* means fuzzy search)
  • exists key: confirm whether a key exists
  • del key: delete a key
  • expire key time: Set the expiration time of a key to time (unit: seconds)
  • move key area: transfer the key in the current database to the area (other) database
  • persist key : remove the expiration time for the given key
  • ttl key: the remaining expiration time of the query key
  • rename key: rename the specified key 
  • type key : returns the type of the key, which is convenient for the corresponding get operation of the key

3. Clear all keys

  • flushdb: clear all keys of the current database
  • flushall: clear all keys of all databases

Guess you like

Origin blog.csdn.net/weixin_44259720/article/details/123071357