winescout-redis 0.0.3 → 0.0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -4
- data/lib/redis.rb +32 -15
- data/lib/server.rb +0 -3
- data/spec/redis_spec.rb +17 -3
- metadata +3 -2
- data/lib/better_timeout.rb +0 -191
data/Rakefile
CHANGED
@@ -7,10 +7,11 @@ require 'tasks/redis.tasks'
|
|
7
7
|
|
8
8
|
|
9
9
|
GEM = 'redis'
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
GEM_NAME = 'redis'
|
11
|
+
GEM_VERSION = '0.0.3.2'
|
12
|
+
AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley', 'Matthew Clark']
|
13
|
+
EMAIL = "matt.clark@punchstock.com"
|
14
|
+
HOMEPAGE = "http://github.com/winescout/redis-rb"
|
14
15
|
SUMMARY = "Ruby client library for redis key value storage server"
|
15
16
|
|
16
17
|
spec = Gem::Specification.new do |s|
|
data/lib/redis.rb
CHANGED
@@ -19,8 +19,9 @@ class Redis
|
|
19
19
|
|
20
20
|
|
21
21
|
def initialize(opts={})
|
22
|
-
@opts = {:host => 'localhost', :port => '6379'}.merge(opts)
|
22
|
+
@opts = {:host => 'localhost', :port => '6379', :db => 0}.merge(opts)
|
23
23
|
$debug = @opts[:debug]
|
24
|
+
@db = @opts[:db]
|
24
25
|
@server = Server.new(@opts[:host], @opts[:port])
|
25
26
|
end
|
26
27
|
|
@@ -53,25 +54,24 @@ class Redis
|
|
53
54
|
end
|
54
55
|
|
55
56
|
def monitor
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
puts x unless x.nil?
|
65
|
-
end
|
57
|
+
with_socket_management(@server) do |socket|
|
58
|
+
trap("INT") { puts "\nGot ^C! Dying!"; exit }
|
59
|
+
write "MONITOR\r\n"
|
60
|
+
puts "Now Monitoring..."
|
61
|
+
socket.read(12)
|
62
|
+
loop do
|
63
|
+
x = socket.gets
|
64
|
+
puts x unless x.nil?
|
66
65
|
end
|
67
|
-
|
68
|
-
|
66
|
+
end
|
67
|
+
end
|
69
68
|
|
70
69
|
def quit
|
71
70
|
write "QUIT\r\n"
|
72
71
|
end
|
73
72
|
|
74
73
|
def select_db(index)
|
74
|
+
@db = index
|
75
75
|
write "SELECT #{index}\r\n"
|
76
76
|
get_response
|
77
77
|
end
|
@@ -81,6 +81,16 @@ class Redis
|
|
81
81
|
get_response == OK
|
82
82
|
end
|
83
83
|
|
84
|
+
def flush_all
|
85
|
+
ensure_retry do
|
86
|
+
puts "Warning!\nFlushing *ALL* databases!\n5 Seconds to Hit ^C!"
|
87
|
+
trap('INT') {quit; return false}
|
88
|
+
sleep 5
|
89
|
+
write "FLUSHALL\r\n"
|
90
|
+
get_response == OK
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
84
94
|
def last_save
|
85
95
|
write "LASTSAVE\r\n"
|
86
96
|
get_response.to_i
|
@@ -198,7 +208,7 @@ class Redis
|
|
198
208
|
|
199
209
|
def decr(key, decrement=nil)
|
200
210
|
if decrement
|
201
|
-
write "
|
211
|
+
write "DECRBY #{key} #{decrement}\r\n"
|
202
212
|
else
|
203
213
|
write "DECR #{key}\r\n"
|
204
214
|
end
|
@@ -392,7 +402,14 @@ class Redis
|
|
392
402
|
|
393
403
|
def set(key, val, expiry=nil)
|
394
404
|
write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
|
395
|
-
get_response == OK
|
405
|
+
s = get_response == OK
|
406
|
+
return expire(key, expiry) if s && expiry
|
407
|
+
s
|
408
|
+
end
|
409
|
+
|
410
|
+
def expire(key, expiry=nil)
|
411
|
+
write("EXPIRE #{key} #{expiry}\r\n")
|
412
|
+
get_response == 1
|
396
413
|
end
|
397
414
|
|
398
415
|
def set_unless_exists(key, val)
|
data/lib/server.rb
CHANGED
@@ -91,9 +91,6 @@ class Server
|
|
91
91
|
addrs = Socket.getaddrinfo(host, nil)
|
92
92
|
addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
|
93
93
|
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
94
|
-
#addr = Socket.getaddrinfo(host, nil)
|
95
|
-
#sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
|
96
|
-
|
97
94
|
if timeout
|
98
95
|
secs = Integer(timeout)
|
99
96
|
usecs = Integer((timeout - secs) * 1_000_000)
|
data/spec/redis_spec.rb
CHANGED
@@ -22,7 +22,7 @@ describe "redis" do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
after(:each) do
|
25
|
-
@r.
|
25
|
+
@r.keys('*').each {|k| @r.delete k}
|
26
26
|
end
|
27
27
|
|
28
28
|
after(:all) do
|
@@ -39,6 +39,13 @@ describe "redis" do
|
|
39
39
|
@r['foo'].should == 'nik'
|
40
40
|
end
|
41
41
|
|
42
|
+
it "should be able to SET a key with an expiry" do
|
43
|
+
@r.set('foo', 'bar', 1)
|
44
|
+
@r['foo'].should == 'bar'
|
45
|
+
sleep 2
|
46
|
+
@r['foo'].should == nil
|
47
|
+
end
|
48
|
+
|
42
49
|
it "should be able to SETNX(set_unless_exists)" do
|
43
50
|
@r['foo'] = 'nik'
|
44
51
|
@r['foo'].should == 'nik'
|
@@ -59,8 +66,7 @@ describe "redis" do
|
|
59
66
|
@r.incr('counter').should == 2
|
60
67
|
@r.incr('counter').should == 3
|
61
68
|
@r.decr('counter').should == 2
|
62
|
-
@r.decr('counter').should ==
|
63
|
-
@r.decr('counter').should == 0
|
69
|
+
@r.decr('counter', 2).should == 0
|
64
70
|
end
|
65
71
|
#
|
66
72
|
it "should be able to RANDKEY(return a random key)" do
|
@@ -84,6 +90,14 @@ describe "redis" do
|
|
84
90
|
@r['bar'].should == 'ohai'
|
85
91
|
end
|
86
92
|
#
|
93
|
+
it "should be able to EXPIRE a key" do
|
94
|
+
@r['foo'] = 'bar'
|
95
|
+
@r.expire('foo', 1)
|
96
|
+
@r['foo'].should == "bar"
|
97
|
+
sleep 2
|
98
|
+
@r['foo'].should == nil
|
99
|
+
end
|
100
|
+
#
|
87
101
|
it "should be able to EXISTS(check if key exists)" do
|
88
102
|
@r['foo'] = 'nik'
|
89
103
|
@r.key?('foo').should be_true
|
metadata
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: winescout-redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.3
|
4
|
+
version: 0.0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ezra Zygmuntowicz
|
8
8
|
- Taylor Weibley
|
9
|
+
- Matthew Clark
|
9
10
|
autorequire: redis
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
@@ -34,7 +35,7 @@ files:
|
|
34
35
|
- spec/redis_spec.rb
|
35
36
|
- spec/spec_helper.rb
|
36
37
|
has_rdoc: true
|
37
|
-
homepage: http://github.com/
|
38
|
+
homepage: http://github.com/winescout/redis-rb
|
38
39
|
post_install_message:
|
39
40
|
rdoc_options: []
|
40
41
|
|
data/lib/better_timeout.rb
DELETED
@@ -1,191 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# = timeout.rb
|
3
|
-
#
|
4
|
-
# execution timeout
|
5
|
-
#
|
6
|
-
# = Copyright
|
7
|
-
#
|
8
|
-
# Copyright - (C) 2008 Evan Phoenix
|
9
|
-
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
|
10
|
-
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
|
11
|
-
#
|
12
|
-
#++
|
13
|
-
#
|
14
|
-
# = Description
|
15
|
-
#
|
16
|
-
# A way of performing a potentially long-running operation in a thread, and
|
17
|
-
# terminating it's execution if it hasn't finished within fixed amount of
|
18
|
-
# time.
|
19
|
-
#
|
20
|
-
# Previous versions of timeout didn't use a module for namespace. This version
|
21
|
-
# provides both Timeout.timeout, and a backwards-compatible #timeout.
|
22
|
-
#
|
23
|
-
# = Synopsis
|
24
|
-
#
|
25
|
-
# require 'timeout'
|
26
|
-
# status = Timeout::timeout(5) {
|
27
|
-
# # Something that should be interrupted if it takes too much time...
|
28
|
-
# }
|
29
|
-
#
|
30
|
-
|
31
|
-
require 'thread'
|
32
|
-
|
33
|
-
module Timeout
|
34
|
-
|
35
|
-
##
|
36
|
-
# Raised by Timeout#timeout when the block times out.
|
37
|
-
|
38
|
-
class Error<Interrupt
|
39
|
-
end
|
40
|
-
|
41
|
-
# A mutex to protect @requests
|
42
|
-
@mutex = Mutex.new
|
43
|
-
|
44
|
-
# All the outstanding TimeoutRequests
|
45
|
-
@requests = []
|
46
|
-
|
47
|
-
# Represents +thr+ asking for it to be timeout at in +secs+
|
48
|
-
# seconds. At timeout, raise +exc+.
|
49
|
-
class TimeoutRequest
|
50
|
-
def initialize(secs, thr, exc)
|
51
|
-
@left = secs
|
52
|
-
@thread = thr
|
53
|
-
@exception = exc
|
54
|
-
end
|
55
|
-
|
56
|
-
attr_reader :thread, :left
|
57
|
-
|
58
|
-
# Called because +time+ seconds have gone by. Returns
|
59
|
-
# true if the request has no more time left to run.
|
60
|
-
def elapsed(time)
|
61
|
-
@left -= time
|
62
|
-
@left <= 0
|
63
|
-
end
|
64
|
-
|
65
|
-
# Raise @exception if @thread.
|
66
|
-
def cancel
|
67
|
-
if @thread and @thread.alive?
|
68
|
-
@thread.raise @exception, "execution expired"
|
69
|
-
end
|
70
|
-
|
71
|
-
@left = 0
|
72
|
-
end
|
73
|
-
|
74
|
-
# Abort this request, ie, we don't care about tracking
|
75
|
-
# the thread anymore.
|
76
|
-
def abort
|
77
|
-
@thread = nil
|
78
|
-
@left = 0
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.add_timeout(time, exc)
|
83
|
-
|
84
|
-
@controller ||= Thread.new do
|
85
|
-
while true
|
86
|
-
if @requests.empty?
|
87
|
-
sleep
|
88
|
-
next
|
89
|
-
end
|
90
|
-
|
91
|
-
min = nil
|
92
|
-
|
93
|
-
@mutex.synchronize do
|
94
|
-
min = @requests.min { |a,b| a.left <=> b.left }
|
95
|
-
end
|
96
|
-
|
97
|
-
slept_for = sleep(min.left)
|
98
|
-
|
99
|
-
@mutex.synchronize do
|
100
|
-
@requests.delete_if do |r|
|
101
|
-
if r.elapsed(slept_for)
|
102
|
-
r.cancel
|
103
|
-
true
|
104
|
-
else
|
105
|
-
false
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
req = TimeoutRequest.new(time, Thread.current, exc)
|
114
|
-
|
115
|
-
@mutex.synchronize do
|
116
|
-
@requests << req
|
117
|
-
end
|
118
|
-
|
119
|
-
@controller.run
|
120
|
-
|
121
|
-
return req
|
122
|
-
end
|
123
|
-
|
124
|
-
##
|
125
|
-
# Executes the method's block. If the block execution terminates before +sec+
|
126
|
-
# seconds has passed, it returns true. If not, it terminates the execution
|
127
|
-
# and raises +exception+ (which defaults to Timeout::Error).
|
128
|
-
#
|
129
|
-
# Note that this is both a method of module Timeout, so you can 'include
|
130
|
-
# Timeout' into your classes so they have a #timeout method, as well as a
|
131
|
-
# module method, so you can call it directly as Timeout.timeout().
|
132
|
-
|
133
|
-
def timeout(sec, exception=Error)
|
134
|
-
return yield if sec == nil or sec.zero?
|
135
|
-
raise ThreadError, "timeout within critical session" if Thread.critical
|
136
|
-
|
137
|
-
req = Timeout.add_timeout sec, exception
|
138
|
-
|
139
|
-
begin
|
140
|
-
yield sec
|
141
|
-
ensure
|
142
|
-
req.abort
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
module_function :timeout
|
147
|
-
|
148
|
-
end
|
149
|
-
|
150
|
-
##
|
151
|
-
# Identical to:
|
152
|
-
#
|
153
|
-
# Timeout::timeout(n, e, &block).
|
154
|
-
#
|
155
|
-
# Defined for backwards compatibility with earlier versions of timeout.rb, see
|
156
|
-
# Timeout#timeout.
|
157
|
-
|
158
|
-
def timeout(n, e=Timeout::Error, &block) # :nodoc:
|
159
|
-
Timeout::timeout(n, e, &block)
|
160
|
-
end
|
161
|
-
|
162
|
-
##
|
163
|
-
# Another name for Timeout::Error, defined for backwards compatibility with
|
164
|
-
# earlier versions of timeout.rb.
|
165
|
-
|
166
|
-
class Object
|
167
|
-
remove_const(:TimeoutError) if const_defined?(:TimeoutError)
|
168
|
-
end
|
169
|
-
TimeoutError = Timeout::Error # :nodoc:
|
170
|
-
|
171
|
-
if __FILE__ == $0
|
172
|
-
p timeout(5) {
|
173
|
-
45
|
174
|
-
}
|
175
|
-
p timeout(5, TimeoutError) {
|
176
|
-
45
|
177
|
-
}
|
178
|
-
p timeout(nil) {
|
179
|
-
54
|
180
|
-
}
|
181
|
-
p timeout(0) {
|
182
|
-
54
|
183
|
-
}
|
184
|
-
p timeout(5) {
|
185
|
-
loop {
|
186
|
-
p 10
|
187
|
-
sleep 1
|
188
|
-
}
|
189
|
-
}
|
190
|
-
end
|
191
|
-
|