redis lua 学习笔记 hello world

来源:互联网 发布:sql server as用法 编辑:程序博客网 时间:2024/05/21 07:47

创建一个hello.lua脚本

<span style="color:#330099;">local msg = "Hello, world!"return msg</span>
./redis-cli EVAL "$(cat hello.lua)" 0

返回hello,world!


创建incr-and-stor.lua脚本

<span style="color:#330099;">local key1 = KEYS[1]local key2 = KEYS[2]local argv1 = ARGV[1]local link_id = redis.call("INCR", key1)redis.call("HSET", key2, link_id, argv1)return link_id</span>


./redis-cli EVAL "$(cat incr-and-stor.lua)" 2 links:counter links:urls www.sina.com



命令 lua 脚本KEYS表长度逐个的KEYS逐个的ARGV./redis-cliEVAL "$(cat test2.lua)"1"XX"10      
如果是远程服务在eval 前增加 -h -p -a 等参数


如果使用script load 和 evalsha 命令

先使用

./redis-cli -h 192.168.1.21 -p xxxx -a xxxx SCRIPT LOAD "$(cat incr-and-stor.lua)"

返回"ada6c37ff315095bb64765d5e6b8808b6d971a3a"

然后

./redis-cli -h 192.168.1.21 -p xxxx -a xxxx EVALSHA "ada6c37ff315095bb64765d5e6b8808b6d971a3a" 2 links:counter links:urls www.sina.com



0 0
原创粉丝点击