当前位置:网站首页>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
边栏推荐
- Pan details of deep learning
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
- XML配置文件
- How to install and configure altaro VM backup for VMware vSphere
- "C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
- 大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
- Xaml gradient issue in uwp for some devices
- Pytorch through load_ state_ Dict load weight
- Redis 过期淘汰机制
- 谷歌 | 蛋白序列的深度嵌入和比对
猜你喜欢

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

BTC-密码学原理

Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)

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

Yolov5 network structure + code + application details | CSDN creation punch in

es7创建索引容易犯的错误

Brief introduction of realsense d435i imaging principle

DEX net 2.0 for crawl detection

Latest version of source insight

3dslam with 16 line lidar and octomap
随机推荐
Yolov5 input (II) | CSDN creative punch in
Common interview questions of microservice
Redis 入門和數據類型講解
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Obtenir et surveiller les journaux du serveur distant
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
ansible防火墙firewalld设置
Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
Introduction to deep learning (II) -- univariate linear regression
Final review (Day5)
mapbox尝鲜值之云图动画
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
6.23 warehouse operation on Thursday
Xaml gradient issue in uwp for some devices
【无标题】
Best practices for setting up altaro VM backups
Altaro VM backup getting started
Communication - how to be a good listener?