• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Redis learning notes (7) business scenario analysis and related cases

King Wang

1 月 3, 2022

Catalog

One . Case a

Two . Wechat accept message order        

3、 … and . General instructions —key Basic operation          

Four . General database operations

 


 

 

One . Case a

          1. Baidu self-developed speech recognition service , Free for enterprises to use , Now speed limit for users , Limit Each user initiates at most per minute 10 Time call

          Solution :

                   1. Design Counter , The number of calls is used for Control the number of business execution , as well as user id by key, The number of times of use is value

                   2. Get number of times before calling , Judge whether the limit is exceeded

                            No more than the times , Count per call +1

                            Business call failed , Count -1

                      Set the life cycle of the counter to the specified period , for example :1 second , 1 minute

            As shown in the figure :

                   

            The case is as follows :

                   

                    Realization 1 Growth in minutes

              Solution improvements :

                        Cancel the determination of the maximum value , utilize incr Throw an exception if the maximum value is exceeded Every time, we should judge whether it is more than 10

                        Judge Is it nil

                                  If it is , Set to MAX- Scheduled times

                                  If not , Count +1

                                  Business call failed , Count -1

                      An exception is encountered + operation Over the limit , It is considered that the usage has reached the upper limit

                     

                    

                      exceed ERR increment or decrement wouid overflow Catch the exception

                      Use the maximum , Make a trigger judgment

                     tips16:redis It is applied to time limited service settlement control

 

 

Two . Wechat accept message order

        Business scenario :

                      After wechat receives the message , By default, the most recently received messages are set to the top , When multiple friends and followers send messages at the same time , This sort of sorting will continue to alternate

             , Set repeat session settings to top . Once the user is offline , When you open wechat again , In what order should messages be displayed

             

        Solution :

                 1. rely on list The data has The order To manage messages , take list Structure as a stack Use

                 2. Create the top and normal conversations separately independent list Manage separately

                 3. When a list After receiving the user message in , Send the message to the sender id from list Add… At one end list( This is set to the left )         

                 4. Many are the same id The message is repeated Push There will be problems , Before being put on the stack, whether or not it has the current id The corresponding message , First delete the id

                 5. Push the top session first when pushing messages list,    Then push the normal conversation list, Push finished list Clear all the data

                 6. The number of messages , In other words, the number of wechat users’ conversations is recorded separately with the idea of counter , Along with list Operation synchronization update

          tips17:

                  redis be applied to Data manipulation based on time order Instead of focusing on the specific time

 

3、 … and . General instructions —key Basic operation

          1.key General instructions and database general instructions

              key features :

                      key It’s a character string , adopt key obtain redis Data stored in

                      key What operations should be designed

                            1. about key Operation related to its own effectiveness : Delete… For example , Determine the existence of , Get type, etc

                            2. Control the effectiveness of the relevant operations : For example, expiration date setting , Determine whether it is valid , Effective state switching, etc

                            3. about key Quick query operation , For example, query according to the specified policy key etc.

                            4. Delete the specified key  del key

                                     

                            5. obtain key Whether there is   exists key

                                    

                            6. obtain key The type of    type key

                                   

 

                       

                                 

            key Extended operation ( timeliness )

                       1. by key Set expiration date

                                expire key seconds

                                 

                                pexpire key milliseconds millisecond

                                 

                                expireat key seconds  Time stamp

                              pexpireat key milliseconds   timestamp Time stamp

 

                            obtain key Effective time of

                                    ttl  key (time to live )   

                                   

                                    pttl  key 

                                    

                                 

                                            Switch key From timeliness to permanence   persist key

                                            

                                               

                        2.key Query operation

                                  key Extended operation ( Query mode )

                                  Inquire about key  keys pattern

                                  Rules for query mode

                                            1.* Match any number of arbitrary symbols

                                               ? With an arbitrary symbol

                                               [] Match a specified symbol

                                          Example :

                                               keys * Query all

                                               keys it* Query all with it start

                                               keys *heima Query all with heima ending

                                               keys  ??kong  Query all the first two characters any , Behind heima ending

                                               keys  user?  Query all with user The first and last character is arbitrary

                                               keys  u[st]1  Query all with u start , With 1 ending , Contains a middle letter ,s perhaps t

                       3.key Other operations of

                                          by key Change of name

                                           rename key  newkey If there is a repetition, the result is still changed

                                          

                                           renamenx  key newkey  There are repetitive results , Failed to rename without covering

                                          

                                           sort For all key Sort

                                           

 

                                                   

                                           help@ generic Query the usage of related commands

                                             Commands for viewing common groups include del,dump… Wait for the general command

                                           

 

Four . General database operations

     1.  database

                    key Value repetition problem

                   1.key It’s defined by the programmer

                   2.redis In use , With the increase of operation data, there will be a large number of data and corresponding bug

                   3. Data doesn’t differentiate between categories , Categories are mixed up , It’s easy to repeat or conflict

                  Solution :

                    1. According to business Canonical namespace

                     2.redis For every service Provide 16 A database , Number from 0 To 15

                    3. Every Data between databases are independent of each other

        2.db Basic operation

                Switch database

                             select index

                              Just enter the password

                

                Data manipulation

                            move key db Move data warehouse to target data

                            

                Data clearing

                           dbsize The total amount of data in the library

                              

                           flushdb eliminate db Data in the library , Default 0 Warehouse No

                           

                           flushall  Empty redis database

                           

              

            

 

 

   

                        

                                  

 

 

                                  

 

 

 

                             

                     

 

 

               

            

 

 

 

 

 

                       

                 

发表回复