$ ll /usr/local/share/lua/5.1/ total 72 drwxr-xr-x 3 root root 4096 Oct 25 03:35 ./ drwxr-xr-x 3 root root 4096 Sep 17 14:14 ../ -rw-r--r-- 1 root root 8331 Oct 25 03:34 ltn12.lua -rw-r--r-- 1 root root 2487 Oct 25 03:34 mime.lua -rw-r--r-- 1 root root 35599 Oct 25 03:35 redis.lua drwxr-xr-x 2 root root 4096 Oct 25 03:34 socket/ -rw-r--r-- 1 root root 4451 Oct 25 03:34 socket.lua
安装成功后, 可以简单的测试一下。
利用Docker启动Redis容器
1
$ docker run -ti -d -p 6379:6379 redis
测试脚本hello_redis.lua
1 2 3 4 5 6 7 8 9 10 11 12 13
local redis = require"redis"
local client = redis.connect("127.0.0.1", 6379)
local response = client:ping() if response == falsethen return0 end
client:set("hello", "world")
local var = client:get("hello") print(var)
可能会存在环境变量不对导致的报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ luajit hello_redis.lua luajit: /usr/local/share/lua/5.1/redis.lua:793: module 'socket' not found: no field package.preload['socket'] no file './socket.lua' no file '/usr/local/share/luajit-2.0.5/socket.lua' no file '/usr/local/share/lua/5.1/socket.lua' no file '/usr/local/share/lua/5.1/socket/init.lua' no file './socket.so' no file '/usr/local/lib/lua/5.1/socket.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: [C]: infunction'require' /usr/local/share/lua/5.1/redis.lua:793: infunction'create_connection' /usr/local/share/lua/5.1/redis.lua:836: infunction'connect' a.lua:3: in main chunk [C]: at 0x56508049e440
执行luarocks path --bin 并将结果输入
1 2 3 4 5
$ luarocks path --bin Warning: The directory '/home/canon/.cache/luarocks' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing /usr/local/bin/luarocks with sudo, you may want sudo's -H flag. export LUA_PATH='/home/canon/.luarocks/share/lua/5.1/?.lua;/home/canon/.luarocks/share/lua/5.1/?/init.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.0.5/?.lua' export LUA_CPATH='/home/canon/.luarocks/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/?.so;./?.so;/usr/local/lib/lua/5.1/loadall.so' export PATH='/home/canon/.luarocks/bin:/usr/local/bin:/home/canon/anaconda3/bin:/home/canon/anaconda3/condabin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin'
-- defind functioin function(args) m = md5.new() m:update(args) return md5.tohex(m:finish()) end
functionformatStr(args) t = {} ios = string.match(args, 'canon') if ios ~= nilthen mail = 'email"%s+(.-)%s' t['email'] = string.match(args, mail) else data = string.split(args, '&') for n, v inipairs(data) do d = string.split(v, '=') t[d[1]] = d[2] end end return t end
functionstring.split(s, p) rt = {} string.gsub(s, '[^'..p..']+', function(w)table.insert(rt, w) end ) return rt end
-- default function functioninit(args) local needs = {} needs["protocol"] = "http" return needs end
functionsetup(args) filename = SCLogPath() .. "/" .. name file = assert(io.open(filename, "a")) SCLogInfo("app_login_audit filename: " .. filename) http = 0 -- Connect Redis Server SCLogInfo("Connect Redis Server...") client = redis.connect(host, port) response = client:ping() if response then SCLogInfo("Redis Server connection succeeded.") end end