Redis official website: https://redis.io
Redis Chinese official website: http://www.redis.cn
The following is an overview of Redis data types, which can be read intuitively by using code blocks.
1, Base point
1. There are 16 Redis databases by default, and the first one is used by default. Use select to switch databases.
2. The default port number of Redis is 6379.
3. Use flushdb and flushall to clear the data in the database.
2, Redis key
Master common redis key s.
3, Five data types
The following are some common command operations of String type, but not limited to these.
1,String
# General operation of string content # set key value # get key # exists key #View expiration time # append key value #Additional value # keys * #View all key s 127.0.0.1:6379> set name xiong OK 127.0.0.1:6379> get name "xiong" 127.0.0.1:6379> exists name (integer) 1 127.0.0.1:6379> append name bo (integer) 7 127.0.0.1:6379> get name "xiongbo" 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> append age 18 (integer) 2 127.0.0.1:6379> keys * 1) "age" 2) "name" # incr key #Self plus one operation # decr key #Self subtraction one operation # incrby key num,#Assignment increase # decrby key num,#Assignment reduction 127.0.0.1:6379> set views 0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views (integer) 1 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> get views "2" 127.0.0.1:6379> decr views (integer) 1 127.0.0.1:6379> decr views (integer) 0 127.0.0.1:6379> decr views (integer) -1 127.0.0.1:6379> get views "-1" 127.0.0.1:6379> incrby step 10 (integer) 10 127.0.0.1:6379> incrby step 5 (integer) 15 127.0.0.1:6379> decrby step 8 (integer) 7 127.0.0.1:6379> # getrange gets a range of values # setrange replaces a range of values 127.0.0.1:6379> set key1 "hello,world" OK 127.0.0.1:6379> get key1 "hello,world" 127.0.0.1:6379> GETRANGE key1 0 3 "hell" 127.0.0.1:6379> GETRANGE key1 1 -1 "ello,world" 127.0.0.1:6379> set key2 abcdefg OK 127.0.0.1:6379> SETRANGE key2 1 xx (integer) 7 127.0.0.1:6379> get key2 "axxdefg" 127.0.0.1:6379> # setex key num value #Set expiration time # setnx key value #If the key does not exist, the creation succeeds; otherwise, it fails 127.0.0.1:6379> setex key3 30 hello OK 127.0.0.1:6379> ttl key3 (integer) 24 127.0.0.1:6379> get key3 "hello" 127.0.0.1:6379> setnx mykey tom (integer) 1 127.0.0.1:6379> keys * 1) "key2" 2) "key3" 3) "myke" 127.0.0.1:6379> setnx mykey jack (integer) 0 127.0.0.1:6379> get mykey "tom" 127.0.0.1:6379> # mset set multiple values # mget get multiple values # Msetnx sets multiple values, but msetnx is an atomic operation that either succeeds or fails together. mset age1 10 age2 12 age3 14 mget age1 age2 msetnx age1 123 age6 21 # getset get first, then set If no value exists, return nil If the original value exists, get the new value
2,List
It can be used as stack, queue and blocking queue.
# lpush add element to the left # Add elements to the right of rpush 127.0.0.1:6379> lpush list one (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379> rpush list four (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "four" # lpop removes the first element of the list # rpop removes the last element of the list 127.0.0.1:6379> LPOP list 1 1) "three" 127.0.0.1:6379> rpop list "four" # lindex obtains a value in the list by subscript 127.0.0.1:6379> lindex list 1 "one" # lrem removes the specified number of value s in the list set and matches them exactly 1) "three" 2) "three" 3) "two" 4) "one" 127.0.0.1:6379> lrem list 2 three (integer) 2 127.0.0.1:6379> lrange list 0 -1 1) "two" 2) "one" 127.0.0.1:6379> # ltrim truncates elements and retains elements in the specified range 127.0.0.1:6379> LTRIM list 1 2 # Rpop lpush combines the command to remove the last element and add it to the new list 127.0.0.1:6379> rpoplpush list newlist # lset replaces the value of the specified subscript in the list with another value, and updates the operation. If it does not exist, an error is reported 127.0.0.1:6379> lpush mylist tom age (integer) 2 127.0.0.1:6379> LRANGE mylist 0 -1 1) "age" 2) "tom" 127.0.0.1:6379> lset mylist 0 jack OK 127.0.0.1:6379> LRANGE mylist 0 -1 1) "jack" 2) "tom" # linsert inserts a specific value before or after an element in the list 127.0.0.1:6379> linsert mylist before tom king (integer) 3 127.0.0.1:6379> LRANGE mylist 0 -1 1) "jack" 2) "king" 3) "tom"
Summary:
1. List is actually a linked list, and values can be inserted on both sides
2. If the key does not exist, create a new linked list
3. If the key exists, new content will be added
If 4 in the linked list does not exist, it means that all values are removed
5. Insert or change values on both sides, the efficiency is the highest, and the relative efficiency of intermediate elements will be lower
It can be used as stack, queue and blocking queue.
3,Set
# sadd adds elements to the set # smembers view all elements in the set collection # sismember determines whether a value exists in the set set # scard gets the number of elements in the set set 127.0.0.1:6379> sadd myset hello world (integer) 2 127.0.0.1:6379> smembers myset 1) "world" 2) "hello" 127.0.0.1:6379> sismember myset hello (integer) 1 127.0.0.1:6379> sismember myset hello2 (integer) 0 1127.0.0.1:6379> scard myset (integer) 2 #srem removes an element from the set set 127.0.0.1:6379> srem myset hello (integer) 1 127.0.0.1:6379> smembers myset 1) "world" 127.0.0.1:6379> # srandmember arbitrarily gets an element in the set set 127.0.0.1:6379> srandmember myset "world" # spop randomly deletes some elements in the set set 127.0.0.1:6379> spop myset "hello" # smove moves a specified value from one set to another 127.0.0.1:6379> smove myset myset2 xiong (integer) 1 # For example, Weibo, the number of followers of station B and common friends. # sdiff difference set # sinter intersection # sunion Union 127.0.0.1:6379> sdiff myset myset2 127.0.0.1:6379> sinter myset myset2 127.0.0.1:6379> sunion myset muset2
4. Hash
The Map set is essentially no different from the String type. It is also a simple key value.
# New Hash data in hset # hget get an element # hgetall get all elements # hdel delete an element 127.0.0.1:6379> hset myhash name tom age 18 (integer) 2 127.0.0.1:6379> hgetall muyhash (empty array) 127.0.0.1:6379> hgetall myhash 1) "name" 2) "tom" 3) "age" 4) "18" 127.0.0.1:6379> hdel myhash age (integer) 1 127.0.0.1:6379> hgetall myhash 1) "name" 2) "tom" # hlen gets the number of map elements 127.0.0.1:6379> hlen myhash (integer) 1 # hex gets whether an element in the map exists 127.0.0.1:6379> hexists myhash name (integer) 1 # hkeys gets all the keys in the map # hvals gets all the value s in the map 127.0.0.1:6379> hkeys myhash 1) "name" 127.0.0.1:6379> hvals myhash 1) "tom" # hincrby specifies that a variable is incremented automatically # hsetnx can only be set if it does not exist, and cannot be set if it exists. It is the same as the setnx command in String (integer) 1 127.0.0.1:6379> hincrby myhash age 1 (integer) 21 127.0.0.1:6379> hget myhash age "21" 127.0.0.1:6379> hsetnx myhash age 12 (integer) 0 127.0.0.1:6379> hsetnx myhash email 192@qq.com (integer) 1
The data changed by hash is user name and age, especially the frequently changed information such as user information. Hash is more suitable for object storage, and String is more suitable for String storage.
5. zset ordered set
# zadd add zset set element # ZRANGEBYSCORE sort # ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] 127.0.0.1:6379> zadd salary 500 zhangsan (integer) 1 127.0.0.1:6379> zadd salary 800 lisi (integer) 1 127.0.0.1:6379> zadd salary 1000 wangwu (integer) 1 127.0.0.1:6379> zrangebyscore salary 0 20000 1) "zhangsan" 2) "lisi" 3) "wangwu" # zrem removes an element from the zset set # zcard gets the number of elements in the zset set 127.0.0.1:6379> zrem salary zhangsan (integer) 1 127.0.0.1:6379> zcard salary (integer) 2 127.0.0.1:6379> # zcount returns the ordered set key, and the score value is between min and max # (the default includes members whose score value is equal to min or max) 127.0.0.1:6379> zcount salary 100 20000 (integer) 2
Case idea: set sorting, storing class grade table and salary sorting table
Ordinary message, 1, important message, 2, judgment with weight
Leaderboard application implementation, taking Top N, etc.
4, Three special data types
1. Geographical location
Six commands.
# geoadd add add geographic location 127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing (integer) 1 127.0.0.1:6379> geoadd china:city 121.47 31.32 shanghai (integer) 1 127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqin 114.05 22.52 shenzhen (integer) 2 # geopos view the longitude and dimension of a location 127.0.0.1:6379> geopos china:city shenzhen 1) 1) "114.04999762773513794" 2) "22.5200000879503861" # geodist looks at the straight-line records of two positions, with units 127.0.0.1:6379> geodist china:city chongqin beijing km "1464.0708" # georadius is a city with a radius of 500km, centered on the longitude and latitude of 110 and 30 # The premise is that geographic data needs to exist in redis data 127.0.0.1:6379> georadius china:city 110 30 500 km 1) "chongqin" # Displays the distance to the center 127.0.0.1:6379> georadius china:city 110 30 500 km withdist 1) 1) "chongqin" 2) "341.9374" # Display other people's location information 127.0.0.1:6379> georadius china:city 110 30 500 km withcoord 1) 1) "chongqin" 2) 1) "106.49999767541885376" 2) "29.52999957900659211" # GEORADIUSBYMEMBER finds other elements around the specified element 127.0.0.1:6379> GEORADIUSBYMEMBER china:city chongqin 1000 km 1) "chongqin" 127.0.0.1:6379> GEORADIUSBYMEMBER china:city chongqin 2000 km 1) "chongqin" 2) "shenzhen" 3) "shanghai" 4) "beijing" # GeoHash returns a GeoHash string of one or more location elements (rarely used) # The closer two strings are, the closer they are 127.0.0.1:6379> geohash china:city beijing shenzhen 1) "wx4fbxxfke0" 2) "ws10578st80"
The underlying implementation of geospatial is Zset. We can use Zset commands to operate geo.
127.0.0.1:6379> zrange china:city 0 -1 1) "chongqin" 2) "shenzhen" 3) "shanghai" 4) "beijing" 127.0.0.1:6379> zrem china:city chongqin (integer) 1 127.0.0.1:6379> zrange china:city 0 -1 1) "shenzhen" 2) "shanghai" 3) "beijing" 127.0.0.1:6379>
2,Hyperloglog
What is cardinality?
For non repeating elements, errors can be accepted.
Redis Hyperloglog cardinality statistics algorithm!
Advantages: the memory occupied is fixed. The cardinality of 2 ^ 64 different elements only needs 12KB of memory. If you want to compare from a memory perspective, Hyperloglog is the first choice.
Application scenario: for example, a website counts the number of visitors, and a person visits multiple times only once.
At first, set may be used to save the user's id and count the number of elements in set as the standard judgment.
However, this will store a large number of user IDs and occupy more space.
At this time, you can use the Hyperloglog of redis.
But Hyperloglog has an error rate of 0.81%. Statistical UV tasks can be ignored.
# pfadd add element 127.0.0.1:6379> pfadd mykey a b c d e f g h i j (integer) 1 127.0.0.1:6379> pfadd mykey2 i j k n m (integer) 1 127.0.0.1:6379> pfcount mykey2 (integer) 5 127.0.0.1:6379> pfadd mykey3 i j i j k n m (integer) 1 127.0.0.1:6379> pfcount mykey3 (integer) 5 127.0.0.1:6379> pfmerge mykey4 mykey mykey2 OK 127.0.0.1:6379> pfcount mykey4 (integer) 13 127.0.0.1:6379>
If fault tolerance is allowed, you can use Hyperloglog.
If fault tolerance is not allowed, use your own set or data type.
3,Bitmaps
Bit storage.
Bitmaps bitmap data structures are recorded by operating binary bits. There are only two states: 0 and 1.
The occupied space is very small, such as clock in, 365 days = 365 bit, 1 byte = 8 bit, about 46 bytes.
Application scenarios, such as clock in.
# setbit key offset value sets the value. Offset is the subscript and value is 0 or 1 # getbit key offset # bitcount key counts the number of bit s whose string is set to 1 127.0.0.1:6379> setbit sign 0 1 (integer) 0 127.0.0.1:6379> setbit sign 1 1 (integer) 0 127.0.0.1:6379> setbit sign 2 0 (integer) 0 127.0.0.1:6379> setbit sign 3 1 (integer) 0 127.0.0.1:6379> setbit sign 4 0 (integer) 0 127.0.0.1:6379> getbit sign 3 (integer) 1 127.0.0.1:6379> bitcount sign (integer) 3 127.0.0.1:6379>