---
title: "爱奇艺开源lua-resty-couchbase"
author: "mtnt2008"
date: "2020-08-25T08:28:34Z"
tags: []
categories: ["开源软件"]
slug: "lua-resty-couchbase"
draft: false
lastmod: "2020-08-25T08:28:34Z"
id: 2516426
url: "https://mtnt2008.iteye.com/blog/2516426"
---

**lua-resty-couchbase**

    使用cosocket纯lua实现的couchbase的client，已经在爱奇艺重要的播放服务稳定运行5年多，线上支持峰值QPS达到20W，在OpenResty架构下面，可以直接从nginx上面访问CouchBase.

    开源地址：[https://github.com/iqiyi/lua-resty-couchbase](https://github.com/iqiyi/lua-resty-couchbase)

**一个例子**

    lua\_package\_path "/path/to/lua-resty-couchbase/lib/?.lua;;";

    lua\_shared\_dict ldict 10m;

    server {  
        location /test {  
            content\_by\_lua\_block {  
                local cjson = require "cjson"  
                local couchbase = require "resty.couchbase"

                local function get\_from\_service()  
                    -- nothing  
                    return "{}"  
                end

                local conf = {  
                    hosts = { "10.10.10.1:8091", "10.10.10.2:8091"},  
                    bucket\_name = "test",  
                    bucketpwd = "test-password",  
                }

                local client, err = couchbase:create\_client(conf.hosts, conf.bucket\_name, conf.bucketpwd)  
                if client == nil then  
                    ngx.log(ngx.ERR, err)  
                end

                -- test set\_timeout  
                client:set\_timeout(500)

                local key = "test-key"  
                local key1 = "test-key1"  
                -- test set  
                client:set(key, "{}")  
                client:set(key1, "{}")

                -- test get\_bluk  
                local values, err = client:get\_bluk(key, key1)  
                if not err then  
                    ngx.say(cjson.encode(values))  
                end

                -- test n1ql  
                local result, err = client:query('SELECT country FROM \`travel-sample\` WHERE name = "Excel Airways";')  
                if not err then  
                    ngx.say(result)  
                end

                -- test get get\_from\_replica  
                local value, err = client:get(key)  
                if value then  
                    ngx.say(value)  
                else  
                    if err then  
                        if string.find(err, "Not found") then  
                            ngx.log(ngx.INFO, "key not found: ", key, " error: ", err)  
                            ngx.say(get\_from\_service())  
                        else  
                            local value\_bak, err\_bak = client:get\_from\_replica(key)  
                            if value\_bak then  
                                ngx.log(ngx.WARN, "get key from replica success: ", key)  
                                ngx.say(value\_bak)  
                            else  
                                ngx.log(ngx.ERR, "get replica error: ", key, "error: ", err\_bak)  
                                ngx.say(get\_from\_service())  
                            end  
                        end  
                    end  
                end

                -- test close  
                client:close()

            }  
        }  
    }

**关于OpenResty**

    OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台，其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

    OpenResty® 通过汇聚各种设计精良的 Nginx 模块（主要由 OpenResty 团队自主开发），从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样，Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块，快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

    OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部，充分利用 Nginx 的非阻塞 I/O 模型，不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。