ffi-hiredis_vip 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Exists
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def exists(*keys)
9
+ reply = nil
10
+ keys = keys.flatten
11
+ number_of_exists = keys.size
12
+ command = "EXISTS#{' %b' * number_of_exists}"
13
+ command_args = []
14
+ keys.each do |key|
15
+ command_args << :string << key << :size_t << key.size
16
+ end
17
+
18
+ @client.synchronize do |connection|
19
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
20
+ end
21
+
22
+ return nil if reply.nil? || reply.null?
23
+
24
+ case reply[:type]
25
+ when :REDIS_REPLY_INTEGER
26
+ reply[:integer]
27
+ else
28
+ 0
29
+ end
30
+ end
31
+ end # class Exists
32
+ end # module HiredisVip
33
+ end # module FFI
@@ -0,0 +1,32 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class ExistsBefore3
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def exists(*keys)
9
+ keys = keys.flatten
10
+ number_of_exists = 0
11
+ command = "EXISTS %b"
12
+
13
+ keys.each do |key|
14
+ reply = nil
15
+ command_args = [ :string, key, :size_t, key.size ]
16
+ @client.synchronize do |connection|
17
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
18
+ end
19
+
20
+ next if reply.nil? || reply.null?
21
+
22
+ case reply[:type]
23
+ when :REDIS_REPLY_INTEGER
24
+ number_of_exists = number_of_exists + reply[:integer]
25
+ end
26
+ end
27
+
28
+ number_of_exists
29
+ end
30
+ end # class ExistsBefore3
31
+ end # module HiredisVip
32
+ end # module FFI
@@ -0,0 +1,32 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Info
4
+ attr_reader :original_info, :info_hash
5
+
6
+ def initialize(info_string)
7
+ @info_hash = {}
8
+ @original_info = info_string
9
+ process_original_info
10
+ end
11
+
12
+ def [](key)
13
+ @info_hash[key]
14
+ end
15
+
16
+ def method_missing(method_name, *args, &block)
17
+ @info_hash[method_name] || super
18
+ end
19
+
20
+ private
21
+
22
+ def process_original_info
23
+ original_info.each_line do |info_line|
24
+ next unless info_line.include?(":")
25
+
26
+ parts = info_line.split(":")
27
+ @info_hash[parts.shift.strip] = parts.join(":").strip
28
+ end
29
+ end
30
+ end # Info
31
+ end # HiredisVip
32
+ end # FFI
@@ -0,0 +1,54 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Keys
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def keys(pattern)
9
+ reply = nil
10
+ pattern = "#{pattern}"
11
+ command = "KEYS %b"
12
+ command_args = [ :string, pattern, :size_t, pattern.size ]
13
+
14
+ synchronize do |connection|
15
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
16
+ end
17
+
18
+ return nil if reply.nil? || reply.null?
19
+
20
+ # TODO: more error checking here? what is correct response on nothing?
21
+ case reply[:type]
22
+ when :REDIS_REPLY_ARRAY
23
+ keys_results_to_array(reply)
24
+ else
25
+ []
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def keys_results_to_array(array_reply)
32
+ keys_results = []
33
+
34
+ 0.upto(array_reply[:elements] - 1) do |element_number|
35
+ result = ::FFI::HiredisVip::Core.redisReplyElement(array_reply, element_number)
36
+
37
+ case result[:type]
38
+ when :REDIS_REPLY_STRING
39
+ keys_results << result[:str]
40
+ end
41
+ end
42
+
43
+ keys_results
44
+ end
45
+
46
+ def synchronize
47
+ @client.synchronize do |connection|
48
+ yield(connection)
49
+ end
50
+ end
51
+
52
+ end # class Keys
53
+ end # module HiredisVip
54
+ end # module FFI
@@ -0,0 +1,60 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Mget
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def mget(*keys)
9
+ reply = nil
10
+ keys = keys.flatten
11
+ command = "MGET"
12
+ command_args = []
13
+ keys.each do |key|
14
+ command << " %b"
15
+ command_args << :string << key << :size_t << key.size
16
+ end
17
+
18
+ synchronize do |connection|
19
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
20
+ end
21
+
22
+ return nil if reply.nil? || reply.null?
23
+
24
+ # TODO: more error checking here? what is correct response on nothing?
25
+ case reply[:type]
26
+ when :REDIS_REPLY_ARRAY
27
+ mget_results_to_array(reply)
28
+ else
29
+ []
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def mget_results_to_array(array_reply)
36
+ mget_results = []
37
+
38
+ 0.upto(array_reply[:elements] - 1) do |element_number|
39
+ result = ::FFI::HiredisVip::Core.redisReplyElement(array_reply, element_number)
40
+
41
+ case result[:type]
42
+ when :REDIS_REPLY_STRING
43
+ mget_results << result[:str]
44
+ when :REDIS_REPLY_NIL
45
+ mget_results << nil
46
+ end
47
+ end
48
+
49
+ mget_results
50
+ end
51
+
52
+ def synchronize
53
+ @client.synchronize do |connection|
54
+ yield(connection)
55
+ end
56
+ end
57
+
58
+ end # class Mget
59
+ end # module HiredisVip
60
+ end # module FFI
@@ -0,0 +1,37 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Persist
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def persist(key)
9
+ reply = nil
10
+ command = "PERSIST %b"
11
+ command_args = [ :string, key, :size_t, key.size ]
12
+ synchronize do |connection|
13
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
14
+ end
15
+
16
+ return nil if reply.nil? || reply.null?
17
+
18
+ case reply[:type]
19
+ when :REDIS_REPLY_INTEGER
20
+ reply[:integer]
21
+ end
22
+ end
23
+
24
+ def supports_persist?
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ def synchronize
31
+ @client.synchronize do |connection|
32
+ yield(connection)
33
+ end
34
+ end
35
+ end # class Persist
36
+ end # module HiredisVip
37
+ end # module FFI
@@ -0,0 +1,23 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class PersistBefore22
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def persist(key)
9
+ raise <<-SCAN_ERROR
10
+ PERSIST Command in Redis is only available on Servers >= 2.2.0
11
+ The Redis Server you are connecting to is using a version that is not supported.
12
+
13
+ == > INFO
14
+ #{@client.info}
15
+ SCAN_ERROR
16
+ end
17
+
18
+ def supports_persist?
19
+ false
20
+ end
21
+ end # class PersistBefore22
22
+ end # module HiredisVip
23
+ end # module FFI
@@ -0,0 +1,42 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Sadd
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def sadd(key, *values)
9
+ reply = nil
10
+ values = values.flatten
11
+ number_of_values = values.size
12
+ command = "SADD %b#{' %b' * number_of_values}"
13
+ command_args = [ :string, key, :size_t, key.size ]
14
+ values.each do |value|
15
+ command_args << :string << value << :size_t << value.size
16
+ end
17
+
18
+ synchronize do |connection|
19
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
20
+ end
21
+
22
+ return nil if reply.nil? || reply.null?
23
+
24
+ case reply[:type]
25
+ when :REDIS_REPLY_INTEGER
26
+ reply[:integer]
27
+ else
28
+ 0
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def synchronize
35
+ @client.synchronize do |connection|
36
+ yield(connection)
37
+ end
38
+ end
39
+
40
+ end # class Sadd
41
+ end # module HiredisVip
42
+ end # module FFI
@@ -0,0 +1,40 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class SaddBefore24
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def sadd(key, *values)
9
+ values = values.flatten
10
+ number_added_to_set = 0
11
+ command = "SADD %b %b"
12
+
13
+ values.each do |value|
14
+ command_args = [ :string, key, :size_t, key.size, :string, value, :size_t, value.size ]
15
+ synchronize do |connection|
16
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
17
+
18
+ next if reply.nil? || reply.null?
19
+
20
+ case reply[:type]
21
+ when :REDIS_REPLY_INTEGER
22
+ number_added_to_set = number_added_to_set + reply[:integer]
23
+ end
24
+ end
25
+ end
26
+
27
+ number_added_to_set
28
+ end
29
+
30
+ private
31
+
32
+ def synchronize
33
+ @client.synchronize do |connection|
34
+ yield(connection)
35
+ end
36
+ end
37
+
38
+ end # class SaddBefore24
39
+ end # module HiredisVip
40
+ end # module FFI
@@ -0,0 +1,78 @@
1
+ module FFI
2
+ module HiredisVip
3
+ class Scan
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def scan(cursor, options = {})
9
+ reply = nil
10
+ command = "SCAN %b"
11
+ command_args = [ :string, cursor, :size_t, cursor.size ]
12
+
13
+ if options[:match]
14
+ matcher = "#{options[:match]}"
15
+ command << " MATCH %b"
16
+ command_args << :string << matcher << :size_t << matcher.size
17
+ end
18
+
19
+ if options[:count]
20
+ count = "#{options[:count]}"
21
+ command << " COUNT %b"
22
+ command_args << :string << count << :size_t << count.size
23
+ end
24
+
25
+ synchronize do |connection|
26
+ reply = ::FFI::HiredisVip::Core.command(connection, command, *command_args)
27
+ end
28
+
29
+ return nil if reply.nil?
30
+
31
+ # TODO: more error checking here?
32
+ case reply[:type]
33
+ when :REDIS_REPLY_ARRAY
34
+ [ scan_results_cursor(reply), scan_results_to_array(reply) ]
35
+ end
36
+ end
37
+
38
+ def supports_scan?
39
+ true
40
+ end
41
+
42
+ private
43
+
44
+ def scan_results_cursor(reply)
45
+ zeroth_result = ::FFI::HiredisVip::Core.redisReplyElement(reply, 0)
46
+
47
+ if !zeroth_result.null? && zeroth_result[:type] == :REDIS_REPLY_STRING
48
+ zeroth_result[:str]
49
+ else
50
+ raise "probs" # TODO: what do we do here
51
+ end
52
+ end
53
+
54
+ def scan_results_to_array(reply)
55
+ scan_results = []
56
+ array_reply = ::FFI::HiredisVip::Core.redisReplyElement(reply, 1)
57
+
58
+ if !array_reply.null? && array_reply[:type] == :REDIS_REPLY_ARRAY
59
+ 0.upto(array_reply[:elements] - 1) do |element_number|
60
+ result = ::FFI::HiredisVip::Core.redisReplyElement(array_reply, element_number)
61
+ scan_results << result[:str] if result[:type] == :REDIS_REPLY_STRING
62
+ end
63
+
64
+ scan_results
65
+ else
66
+ raise "probs" # TODO: what do we do here
67
+ end
68
+ end
69
+
70
+ def synchronize
71
+ @client.synchronize do |connection|
72
+ yield(connection)
73
+ end
74
+ end
75
+
76
+ end # class Scan
77
+ end # module HiredisVip
78
+ end # module FFI