What is Redis
- Redis (Remote Dictionary Server): remote dictionary service
- It is an open source log-type, key-value database that is written in C language, supports the network, can be based on memory and can be persisted, and provides APIs in multiple languages
- One of the most popular NoSQL technologies at the moment, also known as structured database
What can Redis do
- Memory storage and persistence (memory is lost when power is off, so persistence is very important)
- High efficiency, can be used for cache
- Publish and Subscribe System
- Map information analysis
- Timers, counters, page views
Features of Redis
- Various data types
- Can be persistent
- Transaction function
- Can do cluster
Install under Windows
- download
- Unzip to your own environment directory
- Run in the environment directory: redis-server.exe redis.windows.conf
- Enter redis-cli.exe, enter ping, test the connection
Install under Linux
- Download the compressed package: wget http://download.redis.io/releases/redis-5.0.8.tar.gz
- Unzip to the opt path: tar -zxvf redis-5.0.8.tar.gz
- Install gcc: yum install gcc-c++
- cd /redis-5.0.8 —> make
- make install
- Linux will be installed to /usr/local/bin by default
Redis configuration under Linux
- Create a directory to save your own configuration files under /usr/local/bin: mkdir myconfig
- Copy the Redis configuration file: cp /opt/redis-5.0.8/redis.conf myconfig
- File monize no change monize yes , so after Taiwan way to start
Start Redis under Linux
- Execute under /usr/local/bin: redis-server myconfig/redis.conf start Redis
- Execute under /usr/local/bin: redis-cli -h host -p port -a password to enter Redis client
- The client executes shutdown and then exit closes Redis and exits the client
Redis benchmark
Official performance test tool
-
Test: 100 concurrent connections 100000 requests
Execute under /usr/local/bin: redis-benchmark -h localhost -p 6379 -c 100 -n 100000
Redis basic commands
The following commands are executed under the Redis client
-
Redis has 16 databases by default, number 0-15, and number 0 is used by default
select n (select 3) command: switch the database
-
dbsize command: view the current database size
-
keys *Command: View all keys of the current database
-
set key_name key_value: add a key to the current database
-
flushdb command: clear the current database
-
flushall: flush all databases
-
exists key_name: If the key exists, return 1, otherwise return 0
-
mave key_name: remove the target key
-
expire key_name num (Arabic numerals): Set the expiration time for the target key
-
ttl key_name: View the remaining time of the target key
-
type key_name: View the type of target key
Redis data type
Five basic data types
-
String
Usage scenario : The value of the String type can be a number in addition to a string; therefore, it can be used to implement counters, count the number of units, object cache, and web page cache
Commonly used commands:
- append key_name value: append content to the target key, if the key does not exist, it is equivalent to adding a key
- strlen key_name: View the length of the value corresponding to the target key
- incr key_name: target key value +1, which can be used to achieve real-time data of the website, such as reading volume (when the key value is a number, the Redis background will automatically convert it to Integer before operating)
- decr key_name: target key value -1
- incrby/decrby key_name num (Arabic numerals): increase/decrease the specified value
- getrange key_name num1 num2: Get the string range
- setrange key_name num xx: modify the string value from num to xx
- mset k1 v1 k2 v2 k3 v3: set multiple keys at once
- mget k1 k2 k3: get multiple values at once
- Mset and mget are atomic operations, either all succeed or fail
- setex key_name num key_value: Create the target key and set the expiration time
- setnx key_name key_value: If this key does not exist, create the target key
- set user:1 {name:waston,age:3}: set a user:1 object value as a json string
- set item:{id}:value: such as set book:3:views, the number of page views of Book 3
- getset key_name value: get the target key first, and then set the value to the target key
- lset list_name index value: put a value to the specified subscript of the target list, the list and subscript must exist!
-
List
In Redis, the underlying data structure of the list is a linked list, which can be used as a stack, queue, or blocking queue
**Use scenario: **Message queue
Commonly used commands:
list_name is still stored as a key in Redis
- lpush list_name value: add value to the left (head) of the target list
- rpush list_name value: add value to the right (tail) of the target list
- lrange list_name num1 num2: view the values in the target list; lrange list_name 0 -1: view all
- lpop list_name: The target list pops a value on the left (head)
- rpop list_name: The target list pops a value on the right (tail)
- lindex list_name num (starting from 0): Get the num+1 value from the left of the target list
- llen list_name: Get the length of the list
- lrem list_name value: remove all values in the target list
- lrem list_name num value: remove num values from the target list, from left to right
- ltrim list_name num1 num2: intercept the target list; retain the value of num1 to num2
- rpoplpush a_list b_list: Pop a value from the right side of a_list and put it in b_list
- linsert list_name before/after value1 value2: add value2 to the left/right of value1 in the target list
-
Set
Values in Set cannot be repeated
**Use scenario: **Display of common attributes such as common concern and common hobbies among users; recommended by friends
Commonly used commands:
- sadd set_name value: add an element to the target set
- smembers set_name: View the elements in the target set
- sismember set_name value: Determine whether value is in the target set (1: exists 0: does not exist)
- scard set_name: the number of elements in the target set
- srem set_name value: remove the value element in the target set
- srandmember set_name (num): randomly take out one (num) element from the target set
- spop set_name: randomly pop an element from the target set
- smove set1 set2 value: Move the value in set1 to set2
- sdiff set1 set2: return elements in set1 but not in set2
- sinter set1 set2: Returns the elements jointly owned by set1 and set2
- sunion set1 set2: returns all elements in set1 and set2, only one of the same element is returned
-
Hash
Map collection, key-map; the essence is not much different from the String type, the value in the string becomes the key-value
Hash is more suitable for object storage, string is more suitable for general string storage
**Use scenario: **Use a hash to store a series of information of a user
Commonly used commands:
- hset hash_name key_name value: create a hash
- hget hash_name key_name: Get the value corresponding to the target key in the target hash
- hmset hash_name k1 v1 k2 v2: Create multiple values
- hmget hash_name k1 k2: Get multiple values
- hdel hash_name key_name: delete the target key and corresponding value in the target hash
- hlen hash_name: View how many keys are in the target hash
- hexists hash_name key_name: Determine whether the target key exists in the target hash
- hkeys hash_name: View all keys in the target hash
- hvals hash_name: View all values in the target hash
- hincrby hash_name key_name 1/-1: Make the value of the target key +1 or -1
- hsetnx hash_name key_name value: create when the target key does not exist
-
Zset
Ordered set
A value is added to the set for sorting
**Use scenario: **Leaderboard, data weighting
Commonly used commands:
- zadd set_name score value: create a target zset, and add a value score
- zadd set_name n1 v1 n2 v2: create multiple, eg: zadd salary 100 sam 200 kim
- zrangebyscore set_name -inf +inf: target zset is output in ascending order according to score, the range is -inf to +inf, which means positive or negative infinity is the full range
- zrevrange set_name +inf -inf: output in descending order
- zrangebyscore set_name -inf +inf withscores: sort the output with score
- zrem set_name value: remove the specified element in the target zset
- zcard set_name: Get the number of elements in zset
- zcount set_name num1 num2: Get the number of elements between num1 and num2
Three special types
-
geospatial
Geographical location; the bottom layer is implemented by zset
**Use scenario: **Location, nearby people, distance calculation
Commonly used commands:
-
Add geographic location: geoadd key_name longitude (longitude) latitude (latitude) place_name (eg: geoadd china:city 116.40 39.90 beijing)
Generally download city geographic data and import it in one time using java program
-
Get the latitude and longitude data of the target bottom point: geopos key_name place_name1 place_name2 (one or more)
-
Return the distance between two places: geodist key_name place1 place2 unit
unit is the unit, it can be km m mi ft
-
With the given latitude and longitude as the center, find the elements within a certain radius: georadius key_name longitude (center longitude) latitude (center latitude) radius (radius) unit (unit) xxx (additional parameters)
The additional parameter is withdist, the straight line distance is added to the result; withcoord, the latitude and longitude is added; count num, the number of results is limited
-
The query is centered on the specified location and the elements within the specified range: georadiusbymember key_name place_name radius unit
-
Return the latitude and longitude of the specified place in string format: geohash china:city place_name
-
Remove target location: zrem key_name place_name
-
All zset related commands can be used directly...
-
-
Hyperloglog
A data structure for cardinal statistics
Cardinality , that is, the number of remaining elements in an array after removing duplicate elements; such as {1,3,5,5,7} after deduplication {1,3,5,7}, the base is 4
**Use scenario: **Record the UV of the webpage (a person visits multiple times, press once), counting statistics
Commonly used commands:
- Create a Hyperloglog: pfadd key_name value1 value2…
- View the number of elements in the specified Hyperloglog: pfcount key_name
- Merge Hyperloglog (union): pfmerge key_name key1 key2
-
Bitmap
Bitmap, can be regarded as an array
**Application scenario: **There are only two statistics, 0 means one situation, 1 means another situation. Such as: login, check in
Commonly used commands:
- Set the value in the target bitmap target index: setbit map_name index 0/1
- Get the value of the target bitmap target index: getbit map_name index
- Count the number of elements with a value of 1 in the target bitmap: bitcount map_name