Skip to content

lua redis client driver that support redis cluster

Notifications You must be signed in to change notification settings

narzolliam/rcluster.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Name

rcluster - lua redis cluster client driver

Table of Contents

Description

The lua library is a redis client driver that support redis cluster

The library takes advantage of lua-resty-redis and luacrc16 :

Note: Recommended to use the lua library in openresty environment

Install

The following steps assume that the openresty environment have been ok and the installation path is /usr/local/openresty/

Compile luacrc16

git clone https://github.com/youlu-cn/luacrc16.git
cd luacrc16
gcc crc16.c -fPIC -shared -o crc16.so
mv crc16.so /usr/local/openresty/lualib/

Note:

  • If prompted lua.h lualib.h lauxlib.h does not exist when compiling, modify the crc16.c include path to absolute path.
-- modify before
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

-- modify after
#include </usr/local/openresty/luajit/include/luajit-2.1/lua.h>
#include </usr/local/openresty/luajit/include/luajit-2.1/lualib.h>
#include </usr/local/openresty/luajit/include/luajit-2.1/lauxlib.h>
  • If the server has multiple luajit environment, recommended to use the openresty luajit.

  • Paste the generated crc16.so to the lua_package_cpath supported directory (there paste to /usr/local/openresty/lualib/).

Back to TOC

Install rcluster

git clone https://github.com/standsun/rcluster.lua.git
cd rcluster.lua
mv rcluster.lua /usr/local/openresty/lualib/resty/rcluster.lua

Back to TOC

Methods

  • Rcluster:new(cfg)
  • Rcluster:init_pipeline()
  • Rcluster:cancel_pipeline()
  • Rcluster:commit_pipeline()
  • Redis single key operation method, see agentzh's lua-resty-redis#methods
-- copy from redis.lua

local common_cmds = {
    "get",      "set",          "mget",     "mset",
    "del",      "incr",         "decr",                 -- Strings
    "llen",     "lindex",       "lpop",     "lpush",
    "lrange",   "linsert",                              -- Lists
    "hexists",  "hget",         "hset",     "hmget",
    --[[ "hmset", ]]            "hdel",                 -- Hashes
    "smembers", "sismember",    "sadd",     "srem",
    "sdiff",    "sinter",       "sunion",               -- Sets
    "zrange",   "zrangebyscore", "zrank",   "zadd",
    "zrem",     "zincrby",                              -- Sorted Sets
    "auth",     "eval",         "expire",   "script",
    "sort"                                              -- Others
}

Back to TOC

Example

Init

local rcluster = require 'resty.rcluster'

local redis = rcluster:new({
    --db                  = 2,            -- select(db),可选,默认0
    --auth                = 'password',   -- auth(auth),可选,默认nil
    --keepalive_timeout   = 10000,        -- 连接池的响应时间(毫秒) 可选,默认10000
    --max_connections     = 100,          -- 连接池最大连接数,可选,默认 100
    server  = {                              -- 必选,获取slots信息
        { host = "192.168.0.11", port = 6525 },
        { host = "192.168.0.12", port = 6525 },
        { host = "192.168.0.13", port = 6525 },
    }
})

Operate a key

local res,err = redis:hget("user_info_1000","username")

Back to TOC

Operate multiple keys

redis:init_pipeline()

redis:hget("user_info_1000","username")
redis:hset("user_info_1000","username","standsun")
redis:hget("user_info_1002","username")

local res,err = redis:commit_pipeline()

Back to TOC

Issues

TODO

Back to TOC

MORE

About

lua redis client driver that support redis cluster

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%