In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Overview
As a memory database with excellent performance, Redis has a variety of application scenarios in Internet companies. This article will introduce how to use Redis reasonably in the project.
Background
Redis is an open source in-memory data structure storage system. It can be used as database, cache, and message middleware. Multiple types of data structures are supported. Redis has built-in replication (replication), LUA script (Lua scripting), LRU driven event (LRU eviction), transaction (transactions) and different levels of disk persistence (persistence). Provides high availability (high availability) through automatic partitioning of Redis Sentinels (Sentinel) and Redis clusters (Cluster).
Basic data type
String (strings)
1. The expiration time of string will be cleared after resetting the value.
127.0.0.1 set hello 3OK127.0.0.1:6379 > get hello "3" 127.0.0.1 ttl hello (integer)-1127.0.0.1 ttl hello > expire hello 3000 (integer) 1127.0.1 > set hello 4OK127.0.0.1:6379 > ttl hello (integer)-1
2. Set the value of string type to override any other type.
127.0.0.1 sadd settest 6379 > type settestset127.0.0.1:6379 > set settest helloOK127.0.0.1:6379 > type setteststring127.0.0.1:6379 > sadd settest a Magi b (error) WRONGTYPE Operation against a key holding the wrong kind of value Hash (hashes) list (lists)
Redis lists is implemented based on Linked Lists. The head and tail operation is extremely fast, and the retrieval is slow.
Collections (sets) support ordered collections of scope lookups (sorted sets)
The sorting of ordered sets is arranged in dictionary order by default.
Bitmapshyperloglogs supports geospatial queries by radius index (geospatial)
Application scenario
String
Cache data
Both simple and complex data can be converted directly to string storage.
Key: active:spring2019:title value: "2019 Spring Festival activities" Operation: set
A series of cold data caches that do not change frequently, such as commodity information, provincial, municipal and regional information, activity configuration, etc.
Very popular data caching, game ranking, background data updating every second
Simple counting
Number of participants in 2019 Spring Festival activities
Key: active:spring2019:total value:3045 operation: incr
Timed expiration
A person can only check in once a day.
Key:active:checkin:userId:10000:day:20190101 value: check-in timestamp operation: expire
Distributed lock
The following code is not rigorous, nx can be put and issued at the same time
127.0.0.1 set lockkey 6379 > set lockkey 1 nxOK127.0.0.1:6379 > set lockkey 1 nx (nil)
List
User queue
Push,pop
Ordered message
Push,pop
Implement the producer and consumer model
Blocking access to BRPOP and BLPOP commands
Set
De-relist
Number of participants in 2019 Spring Festival activities
Key: active:spring2019:users value:100010,10020 operation: many
Label
User label
Merchant label
The Spring Festival activity has a total of 5 tasks of abcde. User A has completed AME B, and user B has completed CMI d.
Intersection
Tasks completed by user An and user B
Union set
Tasks completed by either user An or user B
Difference set
Tasks that user A has not yet completed
Get random elements
Get a random gift from the gift library set
Hash
Different attributes of the same resource
Users received a total of different kinds of prizes during the event.
Key:active:spring:g'ifts:user:10010 value: {"giftA": 2, "giftB": 5} Operations: many
You can perform incr operations on giftA directly
Zset
Ranking
Ranking of user consumption, ranking of likes, etc.
Key:active:spring:star:rank value: user ID,score: number of likes operation: a lot
Get top 10 based on score
Query a user's score
Users with query scores between 90 and 100
Sometimes our score is not determined by a certain business value, but may be sorted by two business values. For example, if we look at the actual score of the user and the user level, we can use the value before the decimal point to represent the score when designing the score, and the value after the decimal point to indicate the grade. If there are other special requirements, you can also consider adding a certain maximum to deal with the score.
Matters needing attention
Each key should have a reasonable expiration time. The expiration time of string will be overwritten after the reset value. The set operation of string type can cover the type and use the corresponding data structure reasonably.
Do not use list to store a large amount of data and retrieve it
Reasonable planning of the number of key
Set should be used to judge whether users have participated or not. There should not be one key per user.
Environment data isolation business data isolation user redis business redis activity redis should make a distinction. After the activity ends, the active redis is free to clean up the reasonable use of pipes, lua scripts and redis transactions to improve performance, especially in the Reids online system with a large number of key when using redis in the script. Disable the keys * operation in the main library to prevent jamming.
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
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.