Redis executes the application of the actual project of the lua script.

       Blogging is still used to write first, the reason for writing this blog. A few days ago, I wrote the blog "How to use the arrival time to sort with the same points in the leaderboard using redis". I used this method to modify the original leaderboard. I did not do this in the previous leaderboard. After the modification, it is necessary to suspend the leaderboard first, record the scores of the previous leaderboard, and open the new leaderboard after the award is issued. But the planner finally said that I forgot. After the update on Thursday, the scores recorded by the old method existed, and the scores recorded by the new method were also recorded. The leaderboard was messed up. I had to temporarily suspend the leaderboard and wait for me to fix it.

       The reason for the confusion is the score recorded in redis, the old score is the actual score, and the new score is score<<26 + time. In this way, even if a player who is new to the leaderboard scores only 1 point, it will have a higher score than the first player before, which is a mess. The repair method is to modify the old score in redis, use the old score<<26 + time, write a lua script and let redis run directly.

       There are two ways for redis to execute Lua:

       1. After connecting to redis, enter a string of strings on the command line. This string of strings is a lua string connected according to certain rules. I haven't studied this method in depth. It is probably as follows:       

       2. Write the lua script to be executed into a file and run it directly. I use the second method, and I recommend the second method. The second method is clearly written by yourself, can be seen clearly, and is easy to execute.

       lua_2_redis.lua is as follows :

      

      The execution method is as follows:

      redis-cli -h 127.0.0.1 -p 36001 -a xxxxxxx --eval lua_2_redis.lua >> ret.txt

      Write return in the script, it will return the result you want. I used >>ret.txt here to redirect and save the output to the ret.txt file. The execution result shows that the corresponding score in redis has been changed:

      The output file ret.txt:

Note: Because Lua is only supported in redis, some methods in Lua are not supported, such as os.time(), and bit operations are not supported. So I wrote the calculated time directly above the time. Bit operations are replaced by multiplication. The unsupported method needs to find another way to solve it.

 

Finally, I have a feeling, don't be afraid of problems in the project, the code is written by people, there will always be a solution.

 

 

Guess you like

Origin blog.csdn.net/banfushen007/article/details/107298736