1
1
module (' Redis' , package.seeall )
2
2
3
- local socket = require (' socket' )
4
- local uri = require (' socket.url' )
3
+ local commands , network , request , response = {}, {}, {}, {}
5
4
6
- local commands = {}
7
- local network , request , response = {}, {}, {}
5
+ local defaults = {
6
+ host = ' 127.0.0.1' ,
7
+ port = 6379 ,
8
+ tcp_nodelay = true ,
9
+ path = nil
10
+ }
8
11
9
- local defaults = { host = ' 127.0.0.1' , port = 6379 , tcp_nodelay = true }
10
12
local protocol = {
11
13
newline = ' \r\n ' ,
12
14
ok = ' OK' ,
@@ -15,6 +17,18 @@ local protocol = {
15
17
null = ' nil'
16
18
}
17
19
20
+ local function merge_defaults (parameters )
21
+ if parameters == nil then
22
+ parameters = {}
23
+ end
24
+ for k , v in pairs (defaults ) do
25
+ if parameters [k ] == nil then
26
+ parameters [k ] = defaults [k ]
27
+ end
28
+ end
29
+ return parameters
30
+ end
31
+
18
32
local function parse_boolean (v )
19
33
if v == ' 1' or v == ' true' or v == ' TRUE' then
20
34
return true
@@ -614,47 +628,72 @@ end
614
628
615
629
-- ############################################################################
616
630
631
+ function connect_tcp (socket , parameters )
632
+ local host , port = parameters .host , tonumber (parameters .port )
633
+ local ok , err = socket :connect (host , port )
634
+ if not ok then
635
+ error (' could not connect to ' .. host .. ' :' .. port .. ' [' .. err .. ' ]' )
636
+ end
637
+ socket :setoption (' tcp-nodelay' , parameters .tcp_nodelay )
638
+ return socket
639
+ end
640
+
641
+ function connect_unix (socket , parameters )
642
+ local ok , err = socket :connect (parameters .path )
643
+ if not ok then
644
+ error (' could not connect to ' .. parameters .path .. ' [' .. err .. ' ]' )
645
+ end
646
+ return socket
647
+ end
648
+
649
+ function create_connection (parameters )
650
+ local perform_connection , driver
651
+ local socket = require (' socket' )
652
+
653
+ if parameters .scheme == ' unix' then
654
+ perform_connection , driver = connect_unix , require (' socket.unix' )
655
+ assert (driver , ' your build of LuaSocket does not support UNIX domain sockets' )
656
+ else
657
+ if parameters .scheme then
658
+ local scheme = parameters .scheme
659
+ assert (scheme == ' redis' or scheme == ' tcp' , ' invalid scheme: ' .. scheme )
660
+ end
661
+ perform_connection , driver = connect_tcp , socket .tcp
662
+ end
663
+
664
+ return perform_connection (driver (), parameters )
665
+ end
666
+
617
667
function connect (...)
618
- local args = {... }
619
- local host , port = defaults .host , defaults .port
620
- local tcp_nodelay = defaults .tcp_nodelay
668
+ local args , parameters = {... }, nil
621
669
622
670
if # args == 1 then
623
671
if type (args [1 ]) == ' table' then
624
- host = args [1 ].host or defaults .host
625
- port = args [1 ].port or defaults .port
626
- if args [1 ].tcp_nodelay ~= nil then
627
- tcp_nodelay = args [1 ].tcp_nodelay == true
628
- end
672
+ parameters = args [1 ]
629
673
else
630
- local server = uri .parse (select (1 , ... ))
631
- if server .scheme then
632
- assert (server .scheme == ' redis' , ' "' .. server .scheme .. ' " is an invalid scheme' )
633
- host , port = server .host , server .port or defaults .port
634
- if server .query then
635
- for k ,v in server .query :gmatch (' ([-_%w]+)=([-_%w]+)' ) do
674
+ local uri = require (' socket.url' )
675
+ parameters = uri .parse (select (1 , ... ))
676
+ if parameters .scheme then
677
+ if parameters .query then
678
+ for k , v in parameters .query :gmatch (' ([-_%w]+)=([-_%w]+)' ) do
636
679
if k == ' tcp_nodelay' or k == ' tcp-nodelay' then
637
- tcp_nodelay = parse_boolean (v )
638
- if tcp_nodelay == nil then
639
- tcp_nodelay = defaults .tcp_nodelay
640
- end
680
+ parameters .tcp_nodelay = parse_boolean (v )
641
681
end
642
682
end
643
683
end
644
684
else
645
- host , port = server .path , defaults . port
685
+ parameters . host = parameters .path
646
686
end
647
687
end
648
688
elseif # args > 1 then
649
- host , port = unpack (args )
689
+ local host , port = unpack (args )
690
+ parameters = { host = host , port = port }
650
691
end
651
692
652
- assert (host , ' please specify the address of running redis instance' )
653
- local client_socket = socket .connect (host , tonumber (port ))
654
- assert (client_socket , ' could not connect to ' .. host .. ' :' .. port )
655
- client_socket :setoption (' tcp-nodelay' , tcp_nodelay )
693
+ local socket = create_connection (merge_defaults (parameters ))
694
+ local client = create_client (client_prototype , socket , commands )
656
695
657
- return create_client ( client_prototype , client_socket , commands )
696
+ return client
658
697
end
659
698
660
699
-- ############################################################################
0 commit comments