当前位置:网站首页>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
mathTable 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.randomThe 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
边栏推荐
- 今天很多 CTO 都是被幹掉的,因為他沒有成就業務
- Classification and discussion of plane grab detection methods based on learning
- Go practice -- use redis in golang (redis and go redis / redis)
- Transferring images using flask
- Yolov5 input (II) | CSDN creative punch in
- Pan details of deep learning
- Pessimistic lock and optimistic lock of multithreading
- Redis使用Lua脚本简介
- XML Configuration File
- redis 遇到 NOAUTH Authentication required
猜你喜欢

(subplots用法)matplotlib如何绘制多个子图(轴域)

(perfect solution) how to set the position of Matplotlib legend freely

Yolov5 model construction source code details | CSDN creation punch in

Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN

穀歌 | 蛋白序列的深度嵌入和比對

6.23星期四库作业

ES7 easy mistakes in index creation

Deploy crawl detection network using tensorrt (I)

Altaro virtual machine replication failed: "unsupported file type vmgs"

Technical analysis of qianyuantong multi card aggregation router
随机推荐
乾元通多卡聚合路由器的技术解析
The IntelliJ platform completely disables the log4j component
Botu uses peek and poke for IO mapping
【一起上水硕系列】Day 10
BTC-密码学原理
Pytorch through load_ state_ Dict load weight
Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
Progressive multi grasp detection using grasp path for rgbd images
Calculation method of AUC
XML Configuration File
期末复习(Day2)
ninja: build stopped: subcommand failed.
Deploy crawl detection network using tensorrt (I)
How to install and configure altaro VM backup for VMware vSphere
Apache+PHP+MySQL环境搭建超详细!!!
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Source insight automatic installation and licensing
6.23星期四库作业
ROS Compilation Principle
求质数的方法