{"id":13636461,"url":"https://github.com/LazyZhu/lua-resty-ssdb","last_synced_at":"2025-04-19T08:32:16.643Z","repository":{"id":6690557,"uuid":"7935699","full_name":"LazyZhu/lua-resty-ssdb","owner":"LazyZhu","description":"Lua ssdb client driver for the ngx_lua based on the cosocket API, SSDB is a  leveldb server.","archived":false,"fork":false,"pushed_at":"2019-07-11T14:44:22.000Z","size":154,"stargazers_count":100,"open_issues_count":6,"forks_count":57,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-02-14T19:33:17.045Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LazyZhu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-31T11:39:34.000Z","updated_at":"2023-03-01T12:11:49.000Z","dependencies_parsed_at":"2022-07-31T03:48:53.504Z","dependency_job_id":null,"html_url":"https://github.com/LazyZhu/lua-resty-ssdb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazyZhu%2Flua-resty-ssdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazyZhu%2Flua-resty-ssdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazyZhu%2Flua-resty-ssdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazyZhu%2Flua-resty-ssdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LazyZhu","download_url":"https://codeload.github.com/LazyZhu/lua-resty-ssdb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249650189,"owners_count":21305976,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T00:01:01.508Z","updated_at":"2025-04-19T08:32:16.408Z","avatar_url":"https://github.com/LazyZhu.png","language":"Lua","funding_links":[],"categories":["Libraries","Third Modules","Rust Modules"],"sub_categories":["C Modules","Lua Modules"],"readme":"Name\n====\n\nlua-resty-ssdb - Lua ssdb client driver for the ngx_lua based on the cosocket API\n\nStatus\n======\n\nThis library is considedb production ready.\n\nDescription\n===========\n\nThis Lua library is a SSDB client driver for the ngx_lua nginx module:\n\nhttp://wiki.nginx.org/HttpLuaModule\n\nThis Lua library takes advantage of ngx_lua's cosocket API, which ensures\n100% nonblocking behavior.\n\nNote that at least [ngx_lua 0.5.14](https://github.com/chaoslawful/lua-nginx-module/tags) or [ngx_openresty 1.2.1.14](http://openresty.org/#Download) is required.\n\nSSDB is an open source, advanced key-value store server based on leveldb database:\n\nhttps://github.com/ideawu/ssdb\n\nSynopsis\n========\n\n    lua_package_path \"/path/to/lua-resty-ssdb/lib/?.lua;;\";\n\n    server {\n        location /test {\n            content_by_lua '\n                local ssdb = require \"resty.ssdb\"\n                local db = ssdb:new()\n\n                db:set_timeout(1000) -- 1 sec\n\n                local ok, err = db:connect(\"127.0.0.1\", 8888)\n                if not ok then\n                    ngx.say(\"failed to connect: \", err)\n                    return\n                end\n\n                ok, err = db:set(\"dog\", \"an animal\")\n                if not ok then\n                    ngx.say(\"failed to set dog: \", err)\n                    return\n                end\n\n                ngx.say(\"set result: \", ok)\n\n                local res, err = db:get(\"dog\")\n                if not res then\n                    ngx.say(\"failed to get dog: \", err)\n                    return\n                end\n\n                if res == ngx.null then\n                    ngx.say(\"dog not found.\")\n                    return\n                end\n\n                ngx.say(\"dog: \", res)\n\n                db:init_pipeline()\n                db:set(\"cat\", \"Marry\")\n                db:set(\"horse\", \"Bob\")\n                db:get(\"cat\")\n                db:get(\"horse\")\n                local results, err = db:commit_pipeline()\n                if not results then\n                    ngx.say(\"failed to commit the pipelined requests: \", err)\n                    return\n                end\n\n                for i, res in ipairs(results) do\n                    if type(res) == \"table\" then\n                        if not res[1] then\n                            ngx.say(\"failed to run command \", i, \": \", res[2])\n                        else\n                            -- process the table value\n                        end\n                    else\n                        -- process the scalar value\n                    end\n                end\n\n                -- put it into the connection pool of size 100,\n                -- with 0 idle timeout\n                local ok, err = db:set_keepalive(0, 100)\n                if not ok then\n                    ngx.say(\"failed to set keepalive: \", err)\n                    return\n                end\n\n                -- or just close the connection right away:\n                -- local ok, err = db:close()\n                -- if not ok then\n                --     ngx.say(\"failed to close: \", err)\n                --     return\n                -- end\n            ';\n        }\n    }\n\nMethods\n=======\n\nAll of the SSDB commands have their own methods with the same name except all in lower case.\n\nYou can find the complete list of SSDB commands here:\n\nhttps://github.com/ideawu/ssdb/wiki/Commands\n\nYou need to check out this SSDB command reference to see what SSDB command accepts what arguments.\n\nThe SSDB command arguments can be directly fed into the corresponding method call. For example, the \"GET\" SSDB command accepts a single key argument, then you can just call the \"get\" method like this:\n\n    local res, err = db:get(\"key\")\n\nOr, the \"set\" method with two arguments like this:\n\n    local res, err = db:set(\"key\", \"value\")\n\nAnd, the \"SCAN\" ssdb command accepts three arguments, then you should call the \"scan\" method like this:\n\n    local res, err = db:scan(\"0\", \"z\", \"8\")\n\nFor example, \"SET\", \"GET\", \"SCAN\", and \"RSCAN\" commands correspond to the methods \"set\", \"get\", \"scan\", and \"rscan\".\n\nAll ssdb command methods returns a Lua table and `nil` otherwise. In case of errors or failures, it will also return a second value which is a string describing the error.\n\nIf ssdb command only results in \"status reply\"(such as `ok`, `not found` , etc), then returns a Lua table only include status, otherwise SSDB returns a Lua table holding all the composing values (not include status).\n\nA nil reply results in a `ngx.null` return value.\n\nSee https://github.com/ideawu/ssdb/wiki/Commands for details regarding various SSDB reply types.\n\nIn addition to all those ssdb command methods, the following methods are also provided:\n\nnew\n---\n`syntax: db, err = ssdb:new()`\n\nCreates a ssdb object. In case of failures, returns `nil` and a string describing the error.\n\nconnect\n-------\n`syntax: ok, err = db:connect(host, port, options_table?)`\n\nAttempts to connect to the remote host and port that the ssdb server is listening to.\n\nBefore actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method.\n\nAn optional Lua table can be specified as the last argument to this method to specify various connect options:\n\n* `pool`\n: Specifies a custom name for the connection pool being used. If omitted, then the connection pool name will be generated from the string template `\u003chost\u003e:\u003cport\u003e`.\n\nset_timeout\n----------\n`syntax: db:set_timeout(time)`\n\nSets the timeout (in ms) protection for subsequent operations, including the `connect` method.\n\nset_keepalive\n------------\n`syntax: ok, err = db:set_keepalive(max_idle_timeout, pool_size)`\n\nPuts the current SSDB connection immediately into the ngx_lua cosocket connection pool.\n\nYou can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process.\n\nIn case of success, returns `1`. In case of errors, returns `nil` with a string describing the error.\n\nOnly call this method in the place you would have called the `close` method instead. Calling this method will immediately turn the current ssdb object into the `closed` state. Any subsequent operations other than `connect()` on the current objet will return the `closed` error.\n\nget_reused_times\n----------------\n`syntax: times, err = db:get_reused_times()`\n\nThis method returns the (successfully) reused times for the current connection. In case of error, it returns `nil` and a string describing the error.\n\nIf the current connection does not come from the built-in connection pool, then this method always returns `0`, that is, the connection has never been reused (yet). If the connection comes from the connection pool, then the return value is always non-zero. So this method can also be used to determine if the current connection comes from the pool.\n\nclose\n-----\n`syntax: ok, err = db:close()`\n\nCloses the current ssdb connection and returns the status.\n\nIn case of success, returns `1`. In case of errors, returns `nil` with a string describing the error.\n\ninit_pipeline\n-------------\n`syntax: db:init_pipeline()`\n\nEnable the ssdb pipelining mode. All subsequent calls to SSDB command methods will automatically get cached and will send to the server in one run when the `commit_pipeline` method is called or get cancelled by calling the `cancel_pipeline` method.\n\nThis method always succeeds.\n\nIf the ssdb object is already in the SSDB pipelining mode, then calling this method will discard existing cached SSDB queries.\n\ncommit_pipeline\n---------------\n`syntax: results, err = db:commit_pipeline()`\n\nQuits the pipelining mode by committing all the cached SSDB queries to the remote server in a single run. All the replies for these queries will be collected automatically and are returned as if a big multi-bulk reply at the highest level.\n\nThis method returns `nil` and a Lua string describing the error upon failures.\n\ncancel_pipeline\n---------------\n`syntax: db:cancel_pipeline()`\n\nQuits the pipelining mode by discarding all existing cached SSDB commands since the last call to the `init_pipeline` method.\n\nThis method always succeeds.\n\nIf the ssdb object is not in the SSDB pipelining mode, then this method is a no-op.\n\nadd_commands\n------------\n`syntax: hash = ssdb.add_commands(cmd_name1, cmd_name2, ...)`\n\nAdds new ssdb commands to the `resty.ssdb` class. Here is an example:\n\n    local ssdb = require \"resty.ssdb\"\n\n    ssdb.add_commands(\"foo\", \"bar\")\n\n    local db = ssdb:new()\n\n    db:set_timeout(1000) -- 1 sec\n\n    local ok, err = db:connect(\"127.0.0.1\", 8888)\n    if not ok then\n        ngx.say(\"failed to connect: \", err)\n        return\n    end\n\n    local res, err = db:foo(\"a\")\n    if not res then\n        ngx.say(\"failed to foo: \", err)\n    end\n\n    res, err = db:bar()\n    if not res then\n        ngx.say(\"failed to bar: \", err)\n    end\n\nDebugging\n=========\n\nIt is usually convenient to use the [lua-cjson](http://www.kyne.com.au/~mark/software/lua-cjson.php) library to encode the return values of the ssdb command methods to JSON. For example,\n\n    local cjson = require \"cjson\"\n    ...\n    local res, err = db:multi_get(\"h1234\", \"h5678\")\n    if res then\n        print(\"res: \", cjson.encode(res))\n    end\n\nLimitations\n===========\n\n* This library cannot be used in code contexts like set_by_lua*, log_by_lua*, and\nheader_filter_by_lua* where the ngx_lua cosocket API is not available.\n* The `resty.ssdb` object instance cannot be stodb in a Lua variable at the Lua module level,\nbecause it will then be shadb by all the concurrent requests handled by the same nginx\n worker process (see\nhttp://wiki.nginx.org/HttpLuaModule#Data_Sharing_within_an_Nginx_Worker ) and\nresult in bad race conditions when concurrent requests are trying to use the same `resty.ssdb` instance.\nYou should always initiate `resty.ssdb` objects in function local\nvariables or in the `ngx.ctx` table. These places all have their own data copies for\neach request.\n\nInstallation\n============\n\nIf you are using the ngx_openresty bundle (http://openresty.org ), then\nyou need to copy `ssdb.lua` to resty dir because it does't includes\nlua-resty-ssdb by default. And you can just use it in your Lua code,\nas in\n\n    local ssdb = require \"resty.ssdb\"\n    ...\n\nIf you're using your own nginx + ngx_lua build, then you need to configure\nthe lua_package_path directive to add the path of your lua-resty-ssdb source\ntree to ngx_lua's LUA_PATH search path, as in\n\n    # nginx.conf\n    http {\n        lua_package_path \"/path/to/lua-resty-ssdb/lib/?.lua;;\";\n        ...\n    }\n\nTODO\n====\n\nCommunity\n=========\n\nEnglish Mailing List\n--------------------\n\nThe [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.\n\nChinese Mailing List\n--------------------\n\nThe [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.\n\nBugs and Patches\n================\n\nPlease report bugs or submit patches by\n\n1. creating a ticket on the [GitHub Issue Tracker](https://github.com/LazyZhu/lua-resty-ssdb/issues),\n1. or posting to the [OpenResty community](http://wiki.nginx.org/HttpLuaModule#Community).\n\nAuthor\n======\n\nLazyZhu (lazyzhu.com) \u003cbillfzhu@gmail.com\u003e\n\nYichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, CloudFlare Inc.\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2012-2013, by Yichun Zhang (agentzh) \u003cagentzh@gmail.com\u003e, CloudFlare Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nSee Also\n========\n* the ngx_lua module: http://wiki.nginx.org/HttpLuaModule\n* the ssdb widb protocol specification: https://github.com/ideawu/ssdb/wiki/Commands\n* the [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) library\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLazyZhu%2Flua-resty-ssdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLazyZhu%2Flua-resty-ssdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLazyZhu%2Flua-resty-ssdb/lists"}