In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "Redis data structure and simple operation instructions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Redis data structure and simple operation instructions".
1.Redis data structure and simple operation instruction
String, list, set, hash, zset (ordered set)
Generally speaking, redis stores data in the form of Key-Value. It's just that the form of the data type Value is different.
String: the simplest data structure, for example, we convert an object into a json string for storage
Set key value stores data
Get key acquires data
Exists key to check whether the data exists. If it exists, return 1 or 0.
The number of successful operations returned by del key to delete data
Mset key1 value1 key2 value2 key3 value3... Store multiple sets of data
Mget key1 ke2y key3... Get data from multiple key and return a collection, similar to Map's values method
Expire key second sets key elapsed time (in second)
Setex key second value sets key elapsed time in seconds (equivalent to set first, then expire)
Setnx key value if key does not exist, set returns 1. Return 0 if there is (you can implement a distributed lock based on this)
List: the list of list,redis in java is more like a linked list or queue / stack structure. This means that it is fast to delete and insert, but slow to locate through the index. When the last element pops up in the list, the data structure is automatically deleted and memory is reclaimed.
The list structure of Redis is often used as an asynchronous queue. The tasks that need to be deferred are crammed into the Redis list from which another thread polls the data for processing.
Rpush key value1 value2 value3... Insert list data
Llen key View length
Lpop key is obtained in join order (first in, first out, similar queue)
Rpop key LIFO, which is similar to stack.
The list takes data, and after fetching, the whole list is recycled, that is, the data can only be fetched once.
Hash: similar to java's hashMap, compared with strings, we can store only some of the properties of the object when we store the data, while the string needs to convert the whole object completely. Of course, the consumption of hash storage structure must be higher than that of string.
Hset redisKey hashKey1 value1
Hset redisKey hashKey2 value2 insert data
Hgetall redisKey acquires data, key value interval appears
Hlen redisKey View hash length
Hget redisKey hashKey gets the value corresponding to hashKey
Hmset redisKey hashKey1 value1 hashKey2 value2 hashKey2 value3 bulk insert value
Set: similar to HashSet, but similar to list. After the last data is fetched, the structure will be cleaned and the data cannot be obtained again.
Sadd key value
Add sadd key value1 value2 in batch
Smembers key view all
Sismember key value queries whether a value exists. It returns 1 if it exists.
Scard key View Siz
Spop key gets an element
Atomic counting operation
If value is an integer, you can also implement self-increment operation (it can also be used to implement distributed locks, the growth is limited, up to long max, if you exceed this value, you will directly report an error)
Incr key self-increment if key does not exist by default from 0 to 1
Incrby key step setting to increase step size step
2.redis persistence
Although redis is a memory-level operation, it is also persistent.
One is based on RDB snapshots.
Redis saves the in-memory database snapshot in a binary file named dump.rdb.
Redis can be set to automatically save the dataset when the condition of at least M changes to the dataset is met in N seconds.
The other is AOF (append-only file)
The snapshot is not reliable. When the condition of the next snapshot has not been reached after the last snapshot, there is a problem with the service, so the data during this period cannot be saved to the snapshot version. At this time, you need AOF, which records every instruction in a file. When redis restarts, re-execute the instructions in this file, and all the data can be restored to memory.
You can enable AOF by configuring appendonly yes. It is off by default.
AOF also has three strategies for synchronizing data.
Refresh the file every time there is an operation, very slow, but safe
Synchronous refresh once per second: data may be lost within one second
Never synchronize synchronous refresh: it is not safe to let the operating system refresh data when needed.
The default is to swipe once a second.
Mixed persistence:
RDB snapshot data recovery is fast, but there may be a large amount of data loss, so the recovered data is usually replayed with AOF logs, but AOF is relatively slow, especially when the amount of data is large. So it brings mixed persistence in 4. 0, that is, when AOF refreshes, it first records the last snapshot version, then records the incremental operations from the last snapshot version to the present, and then merges into a file, overwriting the original appendonly.aof file. When Redis restarts, load the contents of the RDB snapshot first, and then replay the contents of the incremental operation in the AOF log.
Enable mixed persistence: aof-use-rdb-preamble yes
Mixed persistence in the appendonly.aof content format, part of the RDB file content format, the other is the AOF file content format.
3. Cache elimination strategy:
When the Redis memory exceeds the physical memory limit, the data in the memory begins to exchange frequently with the disk. It will lead to a sharp decline in the performance of Redis, which is basically unavailable for Redis, which is visited frequently.
We do not allow Redis to swap in a production environment. In order to limit the maximum memory usage, Redis provides a configuration parameter maxmemory to limit the memory beyond the expected size.
When the actual memory exceeds maxmemory, Redis provides several optional strategies (maxmemory-policy) to allow users to decide for themselves how to free up new space to continue to provide read and write services.
Noeviction does not continue to process write requests (del,read requests can continue). This ensures that data will not be lost, but it will make the online write-related business unsustainable. This is the default elimination strategy.
Volatile-lru attempts to phase out the key with the expiration time set, and the least used key is eliminated first. Key that does not set an expiration time will not be eliminated, which ensures that data that needs to be persisted will not be suddenly lost.
Volatile-ttl is the same as above, except that the elimination strategy is not LRU, but the value of ttl of the remaining life of key. The smaller the ttl, the more priority it is to be eliminated.
The volatile-random is the same as above, except that the eliminated key is a random key in the expired key collection.
Allkeys-lru differs from volatile-lru in that the key objects to be eliminated in this strategy are all key collections, not just expired key collections. This means that key that does not set an expiration time will also be eliminated.
Allkeys-random is the same as above, but the elimination strategy is random key.
The volatile-xxx policy will only phase out the key with expiration time, and the allkeys-xxx policy will phase out all key. If you just use Redis for caching, you should use allkeys-xxx. Clients don't have to carry expiration time when writing to the cache. If you also want to use Redis's persistence feature at the same time, then use the volatile-xxx policy, so that you can retain the key that does not set the expiration time, they are permanent key and will not be eliminated by the LRU algorithm.
At this point, I believe you have a deeper understanding of the "Redis data structure and simple operation instructions". 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.