请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

smart_eredis: smart eredis 是基于ketama算法和eredis项目的redis erlang驱动,主要 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

smart_eredis

开源软件地址:

https://gitee.com/zeal/smart_eredis

开源软件介绍:

smart_eredis

smart eredis based on eredis, and add external algorithm for redis sharding,currently, we provide two algorithm: ketama and random.

The following smart_eredis.config has two pool, the pool_1 areusing ketama algorithm to sharding the keys, and there has two working node,and one debug node, the pool_2 are using random algorithm you can random toaccess every redis server, it is good scenario for read operation withredis(slave), and it can load balance the redis-server's load.

working node: normally using redis node.

debug node : record every key accessed frequency.

e.g.if you want to access pool_1 to set a key, then the algorithm will help youto find the real redis server, then the key will store in the redis node,if you enabled the debug mode, this key will also store in debug redis node,but the value is the key accessed count and the latest accessed time

config

{smart_eredis,     [{pools,        [            %% {pool_name            :: atom(),             %%  servers              :: list(),             %%  scheduling_algorithm :: list(),             %%  options              :: list()}            {pool_1,[                        [{id, 1},                          {host, "127.0.0.1"},                          {port, 6379},                          {database, 1},                          {password, ""}],                         [{id, 2},                         {host, "127.0.0.1"},                          {port, 6380},                          {database, 1},                          {password, ""}],                        [{id, debug},                          {host, "127.0.0.1"},                          {port, 6380},                          {database, 0},                          {password, ""}]                    ],                                         %% [ALGORITHM_MODULE, INIT_OPTIONS, RUNTIME_OPTIONS]                    %% init option: while load this pool, it will use                     %% this option to initial ketama algorithm                    %% runtime option: this parameter are using in every                     %% request, see example of random                    [ {algorithm, algo_ketama},                      {init_options,                            [{ring_opt, [{expand_node, false},                                         {match_operator, '>'},                                         {concat_char, ":"},                                         {copies_gen_type, specific}]},                             {nodes, [{1, "redis:node_001:6379", 100, 1},                                      {2, "redis:node_001:6380", 100, 2}]}]},                      {runtime_options, []}                    ],                    [{debug, true}]            },            {pool_2,[                        [{id, 1},                          {host, "127.0.0.1"},                          {port, 6379},                          {database, 1},                          {password, ""}],                         [{id, 2},                          {host, "127.0.0.1"},                          {port, 6380},                          {database, 1},                          {password, ""}],                        [{id, debug},                          {host, "127.0.0.1"},                          {port, 6380},                          {database, 0},                          {password, ""}]                    ],                    [                        {algorithm, algo_random},                        {init_options, []},                        {runtime_options, [{ids, [1,2]}]}                    ],                    [{debug, true}]            }        ]}    ]}.

usage:

tiny different of eredis, it is not necessary to use Client(pid),and you need show clearly the keyin the scecond parameter

eredis

{ok, <<"OK">>} = eredis:q(C, ["SET", "foo", "bar"]).

smart_eredis

{ok, <<"OK">>} = smart_eredis:q(pool_1, "foo", ["SET", "foo", "bar"]).

customize your own algorithm

you need to implament following behavior

-module(smart_eredis_algorithm).-callback init( PoolName ::atom(),                 InitOption :: list()) ->    ok | {error, Reason :: term()}.-callback get_client_id( PoolName :: atom(),                          Key     :: string(),                          Options :: list()) ->    ok | {error, Reason :: term()}.

example:

-module(algo_random).-behaviour(smart_eredis_algorithm).-export([init/2, get_client_id/3]).init(_PoolName, _Options)->    ok.%% {algorithm, algo_random},%% {init_options, []},%% {runtime_options, [{ids, [1,2]}]}get_client_id(_PoolName, _Key, Options)->    Ids = proplists:get_value(ids, Options, []),    Len = length(Ids),    case Len > 0 of        true ->            random:seed(os:timestamp()),            {ok, random:uniform(Len)};        false ->            {error, no_random_ids}    end.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap