In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is "introduction and installation of Redis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Redis introduction and installation method"!
Redis introduces what Redis is.
Redis is an open source (BSD licensed) high-performance key-value pair (key-value) in-memory database developed in C language, which can be used as database, cache and message middleware. It is a kind of NoSQL (NOT-Only Sql) database.
Excellent performance, data in memory, read and write very fast, support concurrent 10W QPS
Single process, single thread, thread safe, using IO multiplexing
Rich data types: string (strings), hash (hashes), list (lists), collection (sets), ordered set (sorted sets), etc.
Data persistence is supported. Data in memory can be saved on disk and loaded on restart.
Master-slave replication
Redis application scenario
In-memory database (login information, shopping cart information, user browsing history, etc.)
Cache server (merchandise data, advertising data, etc.) (most used)
Solve the problem of session separation (session sharing) in distributed cluster architecture
Task queue (second kill, rush purchase, 12306, etc.)
Implementation of distributed Lock
Message mode that supports publish and subscribe
Application ranking (orderly collection)
Website visit statistics
Data expiration processing (can be accurate to milliseconds)
Redis installation startup (stand-alone)
There is no official Windows version of Redis. It is recommended to install and run it on Linux system.
Install and start installation under Linux
The official website of Redis is redis.io. The latest version is available on the official website.
Download and decompress
$wget http://download.redis.io/releases/redis-5.0.5.tar.gz$ tar xzf redis-5.0.5.tar.gz
Enter the directory, compile
$cd redis-5.0.5$ make
The compiled binaries are in the src directory, where redis-server is the server program and redis-cli is the client program.
Installation
You can also install to a specified directory through the make install command, and specify an installation directory through PREFIX
$make install PREFIX=/usr/local/redis command description
Redis-server: start the redis service
Redis-cli: enter the redis command client
Redis-benchmark: a tool for performance testing
Redis-check-aof: a tool for checking aof files
Redis-check-dump: a tool for checking rdb files
Redis-sentinel: start the Sentinel Monitoring Service
Start
The command to start Redis is redis-server, and run redis-server in the installation directory
$/ usr/local/redis/bin/redis-server
Close ctrl+c, start in this way, our connection window closes, Redis will also close, we will want it to run in the background all the time, we need the backend to start.
Backend startup (daemon startup)'
1) copy the redis.conf configuration file from the unzipped file to the bin directory in the installation directory
$cp / root/redis-5.0.5/redis.conf / usr/local/redis/bin/
2) modify redis.conf configuration file
$vim redis.conf# changes `daemonize` from `no` to `yes`daemonize yes#. By default, it is bound to the loopback address, and cannot be accessed by other machines by default. Whether protection mode is enabled for # bind 127.0.0. should be noprotected-mode no by yes.
3) start the redis-server service
. / redis-server redis.conf
4) shut down the service
. / redis-cli shutdown command line client
The command to start the client is redis-cli, which takes two parameters
-ip address of the h:redis server (default is 127.0.0.1)
-Port number of p:redis instance (default is 6379)
. / redis-cli-h 127.0.0.1-p 6379Windows installation
Although there is no official Windows version, Microsoft has a 64-bit Redis for development and maintenance, downloaded from https://github.com/microsoftarchive/redis/releases
Download Redis-x64-xxx.zip here and decompress it, enter the directory after decompression, and the following command is to start redis
Redis-server redis.windows.conf
The client connection is
Redis-cli.exe-h 127.0.0.1-p 6379Redis data structure and operation command
Redis stores data in key-value format. Key is a binary secure string. Value supports multiple data types. We need to understand and master the following five data types
String (String): a binary-safe string that can be a string, integer, or floating-point number
Hash: a mapping table of String type field and value, suitable for storing objects
List (List): list of strings, sorted in insertion order
Set: unordered collection of strings, deduplicated
Ordered set (Sorted Set,zset): same as Set, except that it is sorted by score
String (String) command
Set/get assignment / selection
Grammar
# assign SET key value [EX seconds] [PX milliseconds] [NX | XX] # set a new value to GET key# and return the old value GETSET key value
EX seconds: key expiration time
PX milliseconds: sets millisecond expiration time for keys
NX: the key must not exist before it can be set successfully, which is used to add
XX: the key must exist before it can be successfully set for updating.
Example:
127.0.1 6379 > set test 123OK127.0.0.1:6379 > get test "123"
Incr/incrby/decr/decrby numerical value increase or decrease
Only when the value value is an integer can you use the command to add or subtract the value. Incr/decr is to increase / decrease 1, and Decrby is to increase / decrease the specific value. The increase or decrease of the value is an atomic operation.
127.0.0.1 incr counter 6379 > set counter 100OK127.0.0.1:6379 > incr counter (integer) 101127.0.0.1 integer 6379 > incrby counter 20 (integer) 121127.0.0.1 integer 6379 > decr counter (integer) 120127.0.1 purl 6379 > decrby counter 10 (integer) 110
Mset/mget sets / gets multiple keys at the same time
Setting / getting multiple key values in a single command can reduce latency
127.0.0.1 30OK127.0.0.1:6379 6379 > mset K1 10 K2 20 K3 30OK127.0.0.1:6379 > mget K1 K2 K31) "10" 2) "20" 3) "30" usage scenarios
Regular key-value caching
Count: Weibo, fans, etc.
Using INCR atomic counting to produce globally unique id or order number
Hash (Hash) command
Hset/hget/hmset/hmget setting / value
# set a field value HSET key field value# set multiple field values HMSET key field value [field value...] # assign HMSETNX key field value# to get a field value HGET key field # get multiple field values HMGET key field [field...] # get all field values HGETALL key127.0.0.1:6379 > hmset user name zou age 23OK127.0.0.1:6379 > hset user location Guangzhou (integer) 1127. Hgetall user1) "name" 2) "zou" 3) "age" 4) "23" 5) "location" 6) "Guangzhou"
Hincrby increment number
# add the number HINCRBY key field increment127.0.0.1:6379 > hincrby user age 1 (integer) 24
Other commands
# determine whether a field exists HEXISTS key field# only gets the field name HKEYS key# only gets the field value HVALS key# gets the number of fields HLEN key# gets the difference between all fields and values HGETALL keyString and Hash
The Hash type is suitable for storing those object data, especially those where object properties often have additions, deletions and modifications. The String type can also store object data, converting Java objects into json strings or serializing for storage, which is suitable for query operations.
Working with scen
Store structured data objects, such as user information, commodity information
List (List)
The list of Redis is linked list structure and can store an ordered list of strings. The common operation is to add elements to both ends of the list, or to get a fragment of the list.
The interior of the list type is implemented using a bidirectional linked list (double linked list), so the time complexity of adding elements to both ends of the list is 0 (1), and the closer you get to the two ends, the faster you get. This means that even if it is a list of tens of millions of elements, it is extremely fast to get 10 records from the head or tail.
Command
Lpush/rpush adds a new element to the list
# add element LPUSH key value [value...] # at the right end of the list add element RPUSH key value [value...] 127.0.0.1 RPUSH key value 6379 > lpush list:1 123 (integer) 3 127.0.1 RPUSH key value 6379 > rpush list:1 4 56 (integer) 3
Lrange get list snippet
Get a fragment of the list and return all the elements between start and stop (including the elements at both ends). The index starts at 0, and the index can be negative, indicating that the count starts at the end: so-1 is the last element and-2 is the penultimate element of the list.
LRANGE key start stop127.0.0.1:6379 > lrange list:1 0 21) "2" 2) "1" 3) "4"
Lpop/rpop pops elements from both ends of the list
When an element pops up from both ends of the list, it is done in two steps:
The first step is to remove the element on the left of the list from the list
The second step is to return the value of the removed element
LPOP keyRPOP key127.0.0.1:6379 > lpop list:1 "3" 127.0.0.1 rpop list:1 "6"
Other commands
# get the number of elements in the list LLEN key# delete the value of the specified number in the list #-when count > 0, LREM will be deleted from the left side of the list. #-when count sadd set a b c (integer) 3 127.0.0.1 sadd set a 6379 > sadd set a (integer) 0 127.0.1 sadd set a 6379 > srem set c d (integer) 1
Smembers gets all elements
SMEMBERS key127.0.0.1:6379 > smembers set 1) "b" 2) "a"
Sismember determines whether the element is in the collection
# determine whether the element is in the collection. Return 1: in, 0: not in SISMEMBER key member127.0.0.1:6379 > sismember set a (integer) 1 127.0.0.1 integer > sismember set h (integer) 0
Sdiff/sinter/sunion subtraction / intersection / union operation command
Difference Operation of SDIFF-set Amurb: a set of elements that belong to An and do not belong to B.
SDIFF key [key...]
127.0.0.1 sadd setA1 6379 > sadd setB 2 3 (integer) 3127.0.0.1 sadd setB 6379 > sadd setB 2 34 (integer) 3127.0.0.1 sadd setB 6379 > sdiff setA setB1) "1" 127.0.1 sadd setB)) "4"
Intersection operation of SINTER-set A ∩ B: a set of elements belonging to An and B.
SINTER key [key...]
127.0.0.1 6379 > sinter setA setB1) "2" 2) "3"
SUNION-Union operation of a set A ∪ B: a set of elements belonging to An or B.
SUNION key [key...]
127.0.0.1 sunion setA setB1) "1" 2) "2" 3) "3" 4) "4"
Other commands
# get the number of elements in the collection SCARD key# pops up an element randomly from the collection SPOP key usage scenario
A list that needs to be deduplicated
It provides the operation of finding intersection, union, difference and other operations, which can easily realize the functions such as common concern, common preference, second-degree friend and so on.
Ordered set (Sorted Set,zset)
Ordered set (Sorted Set,zset) on the basis of the Set set type, the ordered set type associates a score for each element in the set, which enables us not only to insert, delete and judge whether the element exists in the set, but also to get the first N elements with the highest or lowest score, elements within the specified score range, and other score-related operations.
The difference between SortedSet and List
Similarity
Both are ordered, List is in the order of data insertion, and SortedSet is sorted naturally according to score
Both can get a certain range of elements.
Difference
The list type is implemented through a linked list, and the speed of obtaining data near both ends is extremely fast, but when the number of elements increases, the speed of accessing intermediate data becomes slower.
The ordered collection type is implemented using a hash table, and all data in the middle part can be read quickly.
You can't simply reposition an element in a list, but ordered collections can (by changing scores)
Ordered collections consume more memory than list types
Command
Zadd/zrem add / remove elements
ZADD key score member [score member...] ZREM key member [member...] 127.0.0.1 zadd scoreboard 6379 > zadd scoreboard 80 zhangsan 89 lisi 94 wangwu (integer) 3127.0.0.1 lisi (integer) 0127.0.1 lisi (integer) 0127.0.0.1 lisi 6379 > zrem scoreboard lisi (integer) 1
Zrange/zrevrange gets the list of range elements
Get a list of elements that rank in a certain range.
ZRANGE: returns all elements indexed from start to stop (including elements at both ends) in the order of element scores.
ZREVRANGE: returns all elements indexed from start to stop (including elements at both ends) in the order of element scores from highest to lowest.
# if you need to get the score of the element, you can add the WITHSCORES parameter ZRANGE key start stop [WITHSCORES] ZREVRANGE key start stop [WITHSCORES] 127.0.0.1 WITHSCORES 6379 > zrange scoreboard 021) "zhangsan" 2) "wangwu" 3) "lisi" 127.0.0.1 WITHSCORES 6379 > zrevrange scoreboard 021) "lisi" 2) "wangwu" 3) "zhangsan"
Zscore gets the score of the element
ZSCORE key member127.0.0.1:6379 > zscore scoreboard lisi "97"
Other commands
# get the element in the specified score range ZRANGEBYSCORE key min max [WITHSCORES] # increase the score of an element ZINCRBY key increment member# get the number of elements in the set ZCARD key# get the number of elements in the specified score range ZCOUNT key min max# delete the element in the ranking range ZREMRANGEBYRANK key start stop# delete the element in the score range ZREMRANGEBYSCORE key min max# get the ranking of the element #-ZRANK: from small to Big #-ZREVRANK: ZRANK key memberZREVRANK key member usage scenarios from large to small
Store an ordered and non-repeating list of collections
Related to the ranking, take TOP N operation
General command
Keys returns the key value that satisfies the rule
# return key-valueKEYS pattern127.0.0.1:6379 > keys Know1) "K1" 2) "K3" 3) "K2" 4) "key" that satisfy the rule
Del deletion
DEL key [key...] 127.0.0.1 del K1 K2 K3 (integer) 3
Does exists exist?
EXISTS key [key...] 127.0.0.1 exists test 6379 > exists test (integer) 1127.0.1 exists test > exists mytest (integer) 0
Expires time to Live Settings (important)
In the actual use of Redis, more of it is used as a cache, and the cached data generally needs to set the survival time, and the data is automatically destroyed when it expires.
# set expiration time (in seconds) EXPIRES key seconds # set expiration time (in milliseconds) PEXPIRES key milliseconds # View the remaining survival time of key TTL keyPTTL key# clear survival time PERSIST key127.0.0.1:6379 > expire test 5 (integer) 1127.0.0.1 TTL keyPTTL key# 6379 > ttl test (integer) 2127.0.0.1 TTL keyPTTL key# 6379 > ttl test (integer)-2127.0.0.1 TTL keyPTTL key# 6379 > get test (nil)
Rename renaming
RENAME key newkey127.0.0.1:6379 > keys * 1) "counter" 127.0.0.1 rename counter coounter_newOK127.0.0.1:6379 > keys * 1) "coounter_new"
Type View data types
TYPE keyredis 127.0.0.1 type myzset2zsetredis 6379 > type myzset2zsetredis 127.0.0.1 type myzset2zsetredis 6379 > type mylistlist. I believe you have a deeper understanding of "introduction and installation method of Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.