Luajit IO 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
Luajit IO 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
Luajit IO 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 BSD
开发语言 Lua 查看源码 »
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
投 递 者 红薯
适用人群 未知
收录时间 2015-04-17

软件简介

Luajit IO 是一个纯 Lua 的 IO 框架,提供 C 的高效率,简单但强大的设计,得益于 luajit 这个 JIT 引擎。可用来开发 TCP/UDP 服务器程序。

下面是连接 redis 的示例代码:

local ffi = require"ffi"
local null = ffi.new("void*")
local redis = require "resty.redis"

local function test_redis(req, rsp)
	local red = redis:new()

	-- red:set_timeout(1000) -- 1 sec

	-- or connect to a unix domain socket file listened
	-- by a redis server:
	--     local ok, err = red:connect("unix:/path/to/redis.sock")

	local ok, err = red:connect("127.0.0.1", 6379)
	if not ok then
		rsp:say("failed to connect: ", err)
		return
	end

	ok, err = red:set("dog", "an animal")
	if not ok then
		rsp:say("failed to set dog: ", err)
		return
	end

	rsp:say("set result: ", ok)

	local res, err = red:get("dog")
	if not res then
		rsp:say("failed to get dog: ", err)
		return
	end

	if res == null then
		rsp:say("dog not found.")
		return
	end

	rsp:say("dog: ", res)

	red:init_pipeline()
	red:set("cat", "Marry")
	red:set("horse", "Bob")
	red:get("cat")
	red:get("horse")
	local results, err = red:commit_pipeline()
	if not results then
		rsp:say("failed to commit the pipelined requests: ", err)
		return
	end

	for i, res in ipairs(results) do
		if type(res) == "table" then
			if not res[1] then
				rsp:say("failed to run command ", i, ": ", res[2])
			else
				-- process the table value
			end
		else
			-- process the scalar value
		end
	end

	-- put it into the connection pool of size 100,
	-- with 10 seconds max idle time
	local ok, err = red:set_keepalive(10000, 100)
	if not ok then
		rsp:say("failed to set keepalive: ", err)
		return
	end

	-- or just close the connection right away:
	-- local ok, err = red:close()
	-- if not ok then
	--     rsp:say("failed to close: ", err)
	--     return
	-- end
end

return test_redis
展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
6 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部