Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Brief introduction and Analysis of Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the brief introduction and analysis of Redis, which is very detailed and has certain reference value. Friends who are interested must finish it!

1 introduction to Redis

What is Redis?

Redis is completely open source and free, complies with the BSD protocol, and is a NOSQL key-value database. Redis is an open source blog type that enables ANSI C language to be written, persisted, memory-based and persistent, Key-Value database, and provides API in multiple languages.

BSD is an acronym for "Berkeley Software Distribution", which means "Berkeley Software release". The BSD open source protocol is a protocol that gives people a lot of reason. The source code can be modified, or the modified code can be rereleased as open source or proprietary software. BSD is a friendly protocol for commercial integration because it allows the buyer to modify or redistribute the code, as well as the release and sale of commercial software to be developed on the BSD code. Linux:Ubuntu Redhat Centos

Nosql:

Nosql, generally refers to relational database, Nosql is Not-only SQL, which serves as a good supplement to relational database. With the rise of the Internet, relational database has now become an extremely hot new field, and the development of relational database products is often rapid.

2 Redis installation

2.1 preparation before installation

Redis official

Official website: http://redis.io

Chinese official: http://redis.cn

Download from the official website: http://redis.io/download

Redis installation

Linux

Redis is developed in C language. To install Redis, you need to first compile the source code downloaded by the official website. The compilation depends on the GCC environment. If there is no GCC environment, you need to install GCC.

$wget http://download.redis.io/releases/redis-5.0.5.tar.gz$ tar xzf redis-5.0.5.tar.gz$ cd redis-5.0.5$ make

Windows

Just decompress it directly.

It is recommended that the directory for redis installation be added to the environment variable

2.2 start of Redis

Linux starts the Redis server

Enter the corresponding installation record

Cd / usr/local/redis

Carry out orders

. / bin/redis-server

Linux starts the Redis client

. / bin/redis-cli

Windows starts the Redis server

Enter the corresponding installation record and open the command window.

Carry out orders

Redis-server redis.window.conf

Windows starts the Redis client

Enter the corresponding installation record and open the command window.

Carry out orders

Redis-cli

The figure after the client starts successfully:

3 Redis core "configuration" piece Redis.conf

* 1. Redis does not operate as a daemon by default. You can modify the configuration item to make yes start the daemon daemonize no2. Close the connection (in seconds) after the client has been idle for a long time (in seconds) timeout 300 seconds 3. Specify the Redis listener MERZ, which defaults to 6379. In the blog, the author explains why 6379 is chosen as the default terminal, because 6379 MERZ the corresponding number on the phone button, and the name "MERZ" takes the name of port 6379u4. The bound host address bind 127.0.0.1 5. Specifies the log record level, Redis holds a total of four levels: debug, verbose, notice, warning loglevel verbose6. The number of databases (in stand-alone environment), and the default database is 0, which enables the "select command to specify database id databases 16 save on the connection / always important * 7. RDB persistence policy, which specifies how many update operations there are during the multi-slave time, the data will be synchronized to the data replica. You can use multiple conditions to cooperate with the save Redis default configuration package to provide three conditions save 900 1 save 300 10 save 60 100008. The persistent component name is dbfilename dump.rdb*9. Specifies whether to compress the data when storing the local database. The default is yes,Redis mining LZF (compression algorithm) compression. If you want to save CPU time, you can turn this option off, but it will cause the database component to become huge rdbcompression yes*10. Set the Redis connection password. If the connection password is configured, the client needs to provide the password through the AUTH command when connecting to the Redis. By default, turn off the requirepass foobared// AOF configuration * 11. Specifies whether to log records after each operation. By default, Redis is the name of the closed appendonly no*12. AOF component appendfilename "appendonly.aof" * 13. Aof policies are divided into three types. Always means that records are recorded for each operation, everysec means records are recorded per second, and no means that records are not recorded # appendfsync always appendfsync everysec # appendfsync no.

Summary of Redis persistence:

RDB: is the default persistence mechanism for Redis. RDB is the equivalent of a snapshot, which saves a variety of states. Data from GB-> KB

Snapshot of

Snapshot is the default persistence style, which is to write the data in memory to the binary part in the form of snapshot, and the default component is called dump.rdb.

Advantages: snapshots save data very fast, restore data very fast

Suitable for disaster recovery backup

Disadvantages: "the memory machine is not suitable for making". If the RDB mechanism meets the requirements, the snapshot will be taken and data may be lost.

Snapshot conditions:

1. When the server shuts down normally, / bin/redis-cli shutdown

2. If the key is fully qualified, it will take a snapshot.

AOF: since snapshots are performed several times within a fixed time interval, if redis accidentally down, all modifications since the last snapshot will be lost. If you are required not to lose any changes, you can use the aof persistence format.

Append-only file:aof Snapshot has better persistence because when making aof persistent, redis appends every write command received to the component through the write function (the default appendonly.aof). When redis restarts, it rebuilds the contents of the entire database in memory by executing the write command saved in the script.

There are three styles as follows: (the default is times per second)

Appendonly yes initiates aof persistence formula

Appendsync always writes the disk as soon as it receives the write command, which is the slowest, but guarantees full persistence.

Appendsync writes "disk" times per second, making a good compromise between performance and persistence.

Appendsync no is completely dependent on os and has the best performance. Persistence is not guaranteed.

4 Redis regular data types and response scenarios

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

4.1 String

String is the most basic type of Redis. "key" corresponds to "value", and "key" can store 512MB.

The string type is binary-safe. It means that the string of Redis can contain any data. Such as jpg diagrams or serialized objects.

Binary security means to ensure the information security of binary data when transmitting data, that is, it will not be tampered with, deciphered, etc., if it is attacked.

Hit, it can be detected in time.

It is often similar to the previous map stories. Value is a string.

SET key valueGET keyINCR can add the value of the corresponding key (integer value) to the value (original operation) INCRBY to the value and then assign the value when the SETEX expire expired SETNX not exist key does not exist.

Should "scenario": very common scenarios are based on the statistics of the number of visits to pv (Page view), the number of online accounts, and so on. Incr command (+ operation)

4.2 List

Redis's list allows users to push or pop elements from both ends of the sequence. The list is an ordered and repeatable sequence of multiple string values, which is a linked list structure, so the time complexity of adding elements to both ends of the list is o (1). The closer the elements are, the faster it is to get them. 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. The number of minimum elements that can be contained in a List is 4294967295.

Operation command: put the element after LPUSH on the top of the stack LPOP returns the first element, and delete the element on the list (top of the stack) LLEN returns the degree LINDEX of the current list list returns the specified index subscript of the current list. If nil,0 is not returned, it means that the position of LINSERT inserts at the top of the stack is in the order of index. If Before, you should pay attention to the value of index. If list exists, you can easily check the value of list in a certain index range by going to pushLRANGE. The input index starts at 0, and the label displayed starts at 1. LREM deletes the first element of the specified list (specified value) element deletes the element at the specified location: the value of the element at the specified location is not set by LSET (modified) the input index starts at 0, and the displayed label starts at 1.

Scene should be: 1. The list of the latest news. two。 Message queuing to complete message exchange between multiple programs. You can save tasks by using the push operation

In the list (producer), and then the thread fetches the task into the slave during the pop operation. (consumer)

4.3 Hash (dimension table)

Hashes in Redis can be thought of as map containers with String key and String value, and multiple key-value can be stored in several key. Each Hash can store 4294967295 key-value pairs.

HSETHSET key field value sets the value of the field field in the hash table key to value. If key does not exist, a new hash table is created and entered into the HSET operation. If the domain field already exists in the hash table, the old value will be overwritten. HGET returns the value of the given field field in the hash table key. If it does not exist, return to nilHEXISTSHEXISTS key field to check whether the given domain field exists in the hash table key. HGETALLHGETALL key returns all the fields and values in the hash table key. The returned value field name is followed by the value of each domain name (domain), so the degree of the returned value is twice that of the hash table. HKEYSHKEYS key returns all the values in the hash table key. HLEN returns the number of values in the hash table key. HVALSHVALS key returns the value of all fields in the hash table key. HINCRBYHINCRBY key field increment adds incremental increment to the value of the field field in the hash table key. The increment can also be negative, which is equivalent to a subtraction operation for a given domain. If key does not exist, a new hash table is created and the HINCRBY command is executed. If the domain field does not exist, the value of the domain is initialized to 0 before the execute command. Executing the HINCRBY command on a field field that stores string values will result in an error. The value of this operation is limited to a 64-bit (bit) signed number representation. HMGETHMGET key field [field...] Returns the values of one or more given fields in the hash table key. If the given field does not exist in the hash table, a nil value is returned. Because a non-existent key is treated as an empty hash table, the HMGET operation for a non-existent key will return a table with only nil values. HMSETHMSET key field value [field value...] Multiple field-value (field-value) pairs are set to the hash table key at the same time. This command overwrites fields that already exist in the hash table. If key does not exist, an empty hash table is created and executes the HMSET operation. HSETNXHSETNX key field value sets the value of the field field in the hash table key to value if and only if the domain field does not exist. This operation is effective if the domain field already exists. If key does not exist, a new hash table is created and the HSETNX command is executed.

Should be used in scenarios such as storing, reading, modifying subscriber attributes (name,age,pwd, etc.)

4.4 Set (sequence set)

SADDSADD key member [member...] Add one or more member elements to the collection key, and member elements that already exist in the collection will be ignored. If key does not exist, create a collection that contains only the member element as a member. An error is returned when key is not a collection type. SMEMBERSSMEMBERS key returns all members of the collection key. A key that does not exist is treated as an empty collection. SISMEMBERSISMEMBER key member determines whether the member element is a member of the collection key. SCARDSCARD key returns the cardinality of the collection key (the number of elements in the collection). SPOP (pop-up and remove from the collection) SPOP key removes and returns several random elements in the collection. If you only want to get a random element, but do not want that element to be removed from the collection, you can use the retrieve SRANDMEMBER command. SRANDMEMBERSRANDMEMBER key [count] if only the key parameter is supplied when the command is executed, then two random elements in the collection are returned. Randomly take out count elements (do not delete) SINTERSINTER key [key.] Returns all members of a collection that is the intersection of all given collections. A key that does not exist is considered an empty set. SINTERSTORESINTERSTORE destination key [key...] This command is similar to the SINTER command, but it saves the results to the destination collection, instead of simply returning the result set. If the destination collection already exists, it is overwritten. SUNIONSUNION key [key...] Returns all members of a collection that is the union of all given collections. A key that does not exist is considered an empty set. SUNIONSTORESUNIONSTORE destination key [key...] This command is similar to the SUNION command, but it saves the results to the destination collection, instead of simply returning the result set. If destination already exists, overwrite it. SDIFFSDIFF key [key...] Returns all members of a collection, which is the difference between all given collections. A key that does not exist is considered an empty set. SDIFFSTORESDIFFSTORE destination key [key...] The action of this command is similar to that of SDIFF, but it saves the results to the destination collection, instead of simply returning the result set. If the destination collection already exists, it is overwritten. Destination can be key itself. SMOVESMOVE source destination member moves the member element from the source collection to the destination collection. SMOVE is an original operation. SREM deletes SREM key member [member...] Remove several or more member elements from the collection key, and member elements that do not exist will be ignored. When key is not a collection type, an error is returned.

The scenario should be:

1. Benefit from each other and seek common friends.

two。 You can count all the unique IP visited by the website because of its unique nature.

3. When friends are recommended, they can intersect according to tag, and if they choose a certain threshold (critical value), they can recommend it.

4.5 SortSet (ordered set)

ZADDZADD key score member [[score member] [score member]...] Add several or more member elements and their score values to the ordered set key. ZCARDZCARD key returns the cardinality of the ordered set key. ZSCOREZSCORE key member returns the score value of the member member in the ordered set key. Returns nil if the member element is not a member of the ordered set key, or if key does not exist. ZCOUNT (closed interval) ZCOUNT key min max returns the number of members in the ordered set key whose score value is between min and max (the default includes that the score value is equal to min or max). For more information about the parameters min and max, please refer to the ZRANGEBYSCORE command. ZINCRBYZINCRBY key increment member is the score value of member, a member of the ordered set key, plus the incremental increment. ZRANGEZRANGE key start stop [WITHSCORES] returns the members of the specified interval in the ordered set key. The positions of the members are sorted by increasing the score value (from "to"). Members with the same score value are arranged in dictionary order (lexicographical order). ZRANGEBYSCOREZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] returns all members in the ordered set key whose score values are between min and max (including those equal to min or max). The members of the ordered set are arranged in the order of increasing score values (from "to"). Find the ZRANK according to the specified score range (ranking starts at 0) ZRANK key member returns the ranking of the member member in the ordered set key. The members of the ordered set are arranged in the order of increasing score values (from "to"). ZREVRANGEZREVRANGE key start stop [WITHSCORES] returns the members of the specified interval in the ordered set key. The positions of members are arranged by decreasing score values (from "to"). Members with the same score value are arranged in reverse (reverse lexicographical order) lexicographic order. ZREVRANGEBYSCOREZREVRANGE key start stop [WITHSCORES] returns the members of the specified interval in the ordered set key. ZREVRANKZREVRANK key member returns the ranking of member member in the ordered set key. Where ordered set members are sorted by decreasing score values (from to). The ranking is based on 0, that is, the member with the lowest score value ranks 0. Enables the ranking ZRANK command to rank members by increasing the score value (from to). ZREMZREM key member [member...] Remove one or more members from the ordered set key, and members that do not exist will be ignored. Returns an error when key exists but is not an ordered set type. ZREMRANGEBYRANKZREMRANGEBYRANK key start stop removes all members in the specified rank range from the ordered set key. The interval parameters start and stop respectively indicate that start and stop are included. ZREMRANGEBYSCOREZREMRANGEBYSCORE key min max removes all members in the ordered set key whose score values are between min and max (including those equal to min or max).

Response scenario: you can play in the ranking of points in an online game, and you can update zadd whenever the player's score changes.

Player score (score), and then obtain the account information of the sub-top ten through zrange.

5 Redis Integration (Jedis) Java for Redis

5.1 Guide package

Redis.clients jedis 2.9.0

5.2 configuration

@ Configurationpublic class RedisConfig {@ Bean public Jedis jedis () {Jedis jedis = new Jedis ("localhost", 6379); return jedis;}}

5.3 make use of

/ / directly quote @ Autowiredprivate Jedis jedis;// to make jedis.set (); jedis.get (); jedis.hset (); jedis.hget (); jedis.sadd ();...

6 Redis access connection (RedisTemplate) in Springboot2.x

A brief introduction to RedisTemplate. SpringBoot has implemented a layer template encapsulation of Redis, which makes it easy for us to operate on objects. Bottom layer

It is Jedis that makes SpringBoot1.x, and it is lettuce after Springboot2.x.

6.1 Guide package

Org.springframework.boot spring-boot-starter-data-redis com.fasterxml.jackson.core jackson-core 2.10.0 com.fasterxml.jackson.core jackson-databind 2.10.0

6.2 configuration

@ Configurationpublic class RedisConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (redisConnectionFactory); / / customized template StringRedisSerializer stringRedisSerializer = new StringRedisSerializer (); redisTemplate.setKeySerializer (stringRedisSerializer); / / set value JackSon serialization Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper objectMapper = new ObjectMapper (); / / display the full class name objectMapper.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL) for variables that are not basic types / / set objectMapper.setVisibility (PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper (objectMapper); redisTemplate.setValueSerializer (jsonRedisSerializer); return redisTemplate;}}

6.3 make a living

RedisTemplate.opsForValue (). Set (); redisTemplate.opsForHash (). Put (); redisTemplate.opsForList (). LeftPush (); redisTemplate.opsForSet (). Add (); redisTemplate.opsForZSet (). Add ()

Connect "Redisson in" in 7 Springboot2.x

Redisson:Redisson is a Java resident memory data grid (In-Memory Data Grid) implemented on the basis of Redis. It not only provides a series of distributed Java constant objects, but also provides many distributed services. These include (BitSet, Set)

Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service) Redisson provides the easiest and most convenient way to make Redis. The purpose of Redisson is to facilitate the separation of concerns (Separation of Concern) from Redis by enabling developers to focus more on dealing with business logic.

Open source address: https://github.com/redisson/redisson

7.1 Guide Pack

Org.redisson redisson 3.5.7

7.2 configuration

@ Configurationpublic class RedissonConfig {@ Bean public RedissonClient getRedisson () {Config config = new Config (); config.useSingleServer () .setAddress ("redis://localhost:6379"); return Redisson.create (config);}}

7.3 make use of

Reference command match list

8 Redis memory obsolescence strategy

According to the warning given by the Redis officer, when the memory is out of memory, Redis will eliminate some of the Keys according to the configured cache policy to ensure the success of the write. When there is a phase-out strategy or no Key suitable for phase-out is found, Redis directly returns an out of memory error.

Most efficient cache configuration

In Redis, the most allowed user settings make the memory available.

Maxmemory 512G

Redis provides 8 (after 5.0) data elimination strategies:

Volatile-lru: select the most recent obsolete data from datasets with an expiration time set

Volatile-lfu: delete the key that minimizes the number of times within a period of time from the Keys that has been set to expire

Volatile-ttl: select the data that is about to expire recently from the dataset with the expiration time set for elimination.

Volatile-random: randomly select data elimination from datasets with an expiration time set

Allkeys-lru: select the data from the dataset that has eliminated the most recent data.

Allkeys-lfu: delete the key with the least number of "make" in a period of time from all keys

Allkeys-random: randomly select data from a dataset for elimination

No-enviction (expulsion): ban on expulsion data (no phase-out strategy is adopted. (the default is this configuration). When memory is out of order, an error message is returned for write operations.

Suggestion: after understanding the elimination strategy of Redis, you should take the initiative to set / update the expire time of key and actively eliminate the inactive old data, which is helpful to improve the query performance.

The above is all the contents of this article "introduction and Analysis of Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report