当前位置:网站首页>Introduction to redis using Lua script
Introduction to redis using Lua script
2022-07-03 05:35:00 【Young】
1. Basic usage
1.1 EVAL script numkeys key [key …] arg [arg …]
numkeys yes key The number of , Write later key1 key2… val1 val2…, give an example
127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 val1 val2
1) "key1"
2) "key2"
3) "val1"
4) "val2"
1.2 SCRIPT LOAD script
Load the script into the script cache , return SHA1 The checksum . But it will not be implemented immediately , give an example
127.0.0.1:6379> SCRIPT LOAD "return 'hello world'"
"5332031c6b470dc5a0dd9b4bf2030dea6d65de91"
1.3 EVALSHA sha1 numkeys key [key …] arg [arg …]
Execute the script content according to the cache code . give an example
127.0.0.1:6379> SCRIPT LOAD "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}"
"a42059b356c875f0717db19a51f6aaca9ae659ea"
127.0.0.1:6379> EVALSHA "a42059b356c875f0717db19a51f6aaca9ae659ea" 2 key1 key2 val1 val2
1) "key1"
2) "key2"
3) "val1"
4) "val2"
1.4 SCRIPT EXISTS script [script …]
adopt sha1 Check and determine whether the script is in the cache
1.5 SCRIPT FLUSH
Empty cache
127.0.0.1:6379> SCRIPT LOAD "return 'hello jihite'"
"3a43944275256411df941bdb76737e71412946fd"
127.0.0.1:6379> SCRIPT EXISTS "3a43944275256411df941bdb76737e71412946fd"
1) (integer) 1
127.0.0.1:6379> SCRIPT FLUSH
OK
127.0.0.1:6379> SCRIPT EXISTS "3a43944275256411df941bdb76737e71412946fd"
1) (integer) 0
1.6 SCRIPT KILL
Kill the currently executing script
2. The main advantage
Reduce network overhead : Multiple requests are sent by script at one time , Reduce network latency
Atomic manipulation : Execute the script as a whole , No other commands will be inserted in the middle , No need to use transactions
Reuse : The script sent by the client is permanent redis in , Other clients can reuse scripts
Embeddable sex : Embeddable JAVA,C# And many other programming languages , Support cross platform interaction of different operating systems
3. actual combat
Directly in redis-cli Direct writing lua Script , This is very inconvenient to edit , Usually we put lua script In a lua In file , Then execute this lua Script ,
** Example : Active user judgment :** Determine whether a game user is an active user , If it meets the standard , Then the number of active users +1
if redis.call("EXISTS",KEYS[1]) == 1 then
return redis.call("INCRBY",KEYS[1],ARGV[1])
else
return nil
end
Storage location :
/Users/jihite/activeuser.lua
perform
$ redis-cli --eval /Users/jihite/activeuser.lua user , 1
(integer) 1
127.0.0.1:6379> get user
"1"
127.0.0.1:6379> exit
$ redis-cli --eval /Users/jihite/activeuser.lua user , 1
(integer) 2
$ redis-cli
127.0.0.1:6379> get user
"2"
127.0.0.1:6379> exit
$ redis-cli --eval /Users/jihite/activeuser.lua user , 4
(integer) 6
4. Script security
Such as the command to generate random numbers , If in master After the execution of , And then slave The execution will be different , This destroys the consistency of the master and slave nodes
To solve this problem , Redis Yes Lua There is a strict limit on the scripts that the environment can execute —— All scripts must be pure functions without side effects (pure function). All the situations just mentioned don't exist at all .Redis Yes Lua The environment has made a series of corresponding measures :
- No library to access system state ( For example, system time library )
- No use loadfile function
- If the script is executing commands with random properties ( such as RANDOMKEY ), Or commands with side effects ( such as TIME ) after , Trying to execute a write command ( such as SET ), that Redis Will prevent this script from continuing , And return an error .
- If the script executes a random read command ( such as SMEMBERS ), Then the output of the script is returned to Redis Before , An automatic lexicographic sort will be performed first , So as to ensure that the output results are orderly .
- use Redis Self defined random generating function , Replace Lua Environment
math
Table original math.random Functions and math.randomseed function , The new function has such properties : Every time you execute Lua Script time , Unless you explicitly callmath.randomseed
, otherwisemath.random
The generated pseudo-random number sequence is always the same .
Reference resources
https://www.runoob.com/redis/redis-scripting.html ( Basic use )
https://www.cnblogs.com/Don/articles/5731856.html ( example )
https://www.cnblogs.com/huangxincheng/p/6230129.html
https://redisbook.readthedocs.io/en/latest/feature/scripting.html#lua ( Security )
https://blog.csdn.net/demon7552003/article/details/120165160 Redis And Lua Detailed explanation
边栏推荐
- How do I migrate my altaro VM backup configuration to another machine?
- Altaro VM backup getting started
- BIO、NIO、AIO区别
- 期末复习(DAY6)
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- 聊聊如何利用p6spy进行sql监控
- Common interview questions of microservice
- 求质数的方法
- Win10 install pytullet and test
- 2022.7.2day594
猜你喜欢
es7创建索引容易犯的错误
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
Intégration profonde et alignement des séquences de protéines Google
Map的扩容机制
Deploy crawl detection network using tensorrt (I)
联想R7000显卡的拆卸与安装
Skip table: principle introduction, advantages and disadvantages of skiplist
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Simpleitk learning notes
Why should we rewrite hashcode when we rewrite the equals method?
随机推荐
Redis Introduction et explication des types de données
一起上水硕系列】Day 9
How to set up altaro offsite server for replication
@Solutions to null pointer error caused by Autowired
Get and monitor remote server logs
Source insight operation manual installation trial
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
[practical project] autonomous web server
Source insight License Activation
联想R7000显卡的拆卸与安装
mapbox尝鲜值之云图动画
Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
Can altaro back up Microsoft teams?
redis 无法远程连接问题。
Go practice - gorilla / handlers used by gorilla web Toolkit
How to use source insight
NG Textarea-auto-resize
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
Classification and discussion of plane grab detection methods based on learning