当前位置:网站首页>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
边栏推荐
- ansible防火墙firewalld设置
- kubernetes资源对象介绍及常用命令(五)-(ConfigMap)
- Mapbox tasting value cloud animation
- Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
- 【一起上水硕系列】Day 10
- XML配置文件
- How do I migrate my altaro VM backup configuration to another machine?
- Progressive multi grasp detection using grasp path for rgbd images
- 大二困局(复盘)
- Robot capture experiment demonstration video
猜你喜欢

【一起上水硕系列】Day 10

College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN

Apache+PHP+MySQL环境搭建超详细!!!
![[set theory] relational closure (relational closure related theorem)](/img/6a/b6dca7abf592f8d8ab1d6aecc43381.jpg)
[set theory] relational closure (relational closure related theorem)

redis 无法远程连接问题。

Latest version of source insight

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

"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released

Introduction to deep learning (II) -- univariate linear regression

Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
随机推荐
牛客网 JS 分隔符
Final review (Day5)
Obtenir et surveiller les journaux du serveur distant
[basic grammar] C language uses for loop to print Pentagram
Can altaro back up Microsoft teams?
Why is go language particularly popular in China
Apache+PHP+MySQL环境搭建超详细!!!
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
Basic introduction of redis and explanation of eight types and transactions
乾元通多卡聚合路由器的技术解析
Go practice -- closures in golang (anonymous functions, closures)
Sophomore dilemma (resumption)
redis 无法远程连接问题。
Differences among bio, NiO and AIO
Source insight operation manual installation trial
微服务常见面试题
2022.7.2 simulation match
Redis 入门和数据类型讲解
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
Pessimistic lock and optimistic lock of multithreading