在golang中使用lua脚本

栏目: Lua · 发布时间: 4年前

内容简介:注释:声明全局变量:声明本地变量:

lua基础

注释:

-- this is a comment

声明全局变量:

x = 123

声明本地变量:

local y = 456

方法声明:

function hello_world()
 return "Hello World"
end

迭代:

for i = 1, 10 do
   print(i)
end

条件:

if x == 123 then
    print("x is the magic number")
else
    print("I have no idea what x is")
end

字符串连接:

print("Hello" .. " World")

作为数组使用一个table — 数组的索引从1开始:

data_types = {1.0, 123, "redis", true, false, hello_world}
print(data_types[3]) -- the output is "redis"

作为hash使用一个table:

languages = {lua = 1993, javascript = 1995, python = 1991, ruby =1995}
print("Lua was created in " .. languages["lua"])
print("JavaScript was created in " .. languages.javascript)

redis脚本执行是一种原子操作,所以在执行期间 redis 服务会被阻塞。默认脚本操作会有5秒的执行时间

当一个 lua 脚本执行超时不会自动停止,它会开始对每一个命令回复一个busy状态,表明脚本正在运行。使用命令 SCRIPT KILL 或者 SHUTDOWN NOSAVE 终止脚本执行才能使redis服务回到正常状态

举个栗子

package main

import (
    "fmt"
    "github.com/go-redis/redis"
)

var Client *redis.Client

func init() {
    Client = redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
}

//noinspection GoInvalidCompositeLiteral
func main() {

    Client.FlushAll()

    Client.Set("foo", "bar" , 0)

    var luaScript = redis.NewScript(`return redis.call("GET" , KEYS[1])`)

    n , err := luaScript.Run(Client , []string{"foo"}).Result()
    if err != nil {
        panic(err)
    }

    fmt.Println(n , err)

}

另一个栗子

package main

import (
    "fmt"
    "github.com/go-redis/redis"
)

var Client *redis.Client

func init() {
    Client = redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
}

//noinspection GoInvalidCompositeLiteral
func main() {

    Client.FlushAll()

    foo := []redis.Z{
        {
            1732, "George Washington",
        },
        {
            1809, "Abraham Lincoln",
        },
        {
            1858, "Theodore Roosevelt",
        },
    }

    Client.ZAdd("presidents", foo...)

    var luaScript = redis.NewScript(`
        local elements = redis.call("ZRANGE" , KEYS[1] , 0 , 0) 
        redis.call("ZREM" , KEYS[1] , elements[1])
        return elements[1]
    `)

    n , err := luaScript.Run(Client , []string{"presidents"} , 1).Result()
    if err != nil {
        panic(err)
    }

    fmt.Println(n , err)

}

使用evalsha来操作lua

当执行脚本多次的时候,可以使用 SCRIPT LOADEVALSHA 代替 EVAL 有可能节省带宽。命令 SCRIPT LOAD 缓存一个lua脚本,同时返回一个sha1的标识符。命令 EVALSHA 基于sha1的标识符执行脚本,通过 SCRIPT LOAD 返回。使用 EVALSHA 只有标识符通过网络传输,而不是一个lua代码块

继续举个栗子

package main

import (
    "fmt"
    "github.com/go-redis/redis"
)

var Client *redis.Client

func init() {
    Client = redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
}

//noinspection GoInvalidCompositeLiteral
func main() {

    Client.FlushAll()

    Client.Set("foo" , "bar" , -1)

    var luaScript = `return redis.call("INFO")`
    result ,err := Client.ScriptLoad(luaScript).Result()   //返回的脚本会产生一个sha1哈希值,下次用的时候可以直接使用这个值,类似于
    if err != nil {
        panic(err)
    }

    foo :=Client.EvalSha(result ,[]string{})
    fmt.Println(foo.Val())
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Tango with Django

Tango with Django

David Maxwell、Leif Azzopardi / Leanpub / 2016-11-12 / USD 19.00

Tango with Django is a beginner's guide to web development using the Python programming language and the popular Django web framework. The book is written in a clear and friendly style teaching you th......一起来看看 《Tango with Django》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具