Class: RedisCall
- Inherits:
-
Object
show all
- Defined in:
- lib/redis-call/redis_call.rb
Defined Under Namespace
Modules: JSON, KeepSerializedElement
Classes: Connection, Key, NonTransactionalMethod, Railtie, TransactionAborted, UnexpectedResult
Constant Summary
collapse
- DEFAULT_HOST =
"127.0.0.1"
- DEFAULT_PORT =
6379
- @@config =
{}
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args = {}) ⇒ RedisCall
Returns a new instance of RedisCall.
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/redis-call/redis_call.rb', line 185
def initialize(args = {})
@host = args[:host] || @@config[:host] || DEFAULT_HOST
@port = args[:port] || @@config[:port] || DEFAULT_PORT
if args[:connect]
@connection = Connection.new(@host, @port)
else
@pool_key = "redis_#{@host}:#{@port}".to_sym
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
221
222
223
|
# File 'lib/redis-call/redis_call.rb', line 221
def method_missing *args, &block
connection.__send__ *args, &block
end
|
Class Method Details
.config=(conf) ⇒ Object
178
179
180
|
# File 'lib/redis-call/redis_call.rb', line 178
def self.config= conf
@@config = conf
end
|
.query(*args, &block) ⇒ Object
171
172
173
|
# File 'lib/redis-call/redis_call.rb', line 171
def self.query(*args, &block)
self.new(*args).instance_exec(&block)
end
|
Instance Method Details
#connection ⇒ Object
Also known as:
connect
196
197
198
|
# File 'lib/redis-call/redis_call.rb', line 196
def connection
@connection || (Thread.current[@pool_key] ||= Connection.new(@host, @port))
end
|
#decrzerodelex(key, ttl) ⇒ Object
244
245
246
247
248
249
250
251
252
|
# File 'lib/redis-call/redis_call.rb', line 244
def decrzerodelex key, ttl
multi do
queued(decr key) do |result|
del(key) if result <= 0
result
end
queued(expire key, ttl)
end
end
|
#disconnect(thread = nil, limit = 10) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/redis-call/redis_call.rb', line 202
def disconnect(thread = nil, limit = 10)
begin
connection.disconnect
rescue RuntimeError => exception
raise(exception) if exception.message != "not connected"
end
Thread.current[@pool_key] = nil if @pool_key
if thread
begin
thread.run
rescue ThreadError => exception
raise exception if exception.message != "killed thread"
end
thread.join(limit)
end
end
|
#geti(key) ⇒ Object
269
270
271
|
# File 'lib/redis-call/redis_call.rb', line 269
def geti key
queued(get key) {|result| result.to_i}
end
|
#getnnil(key) ⇒ Object
258
259
260
261
262
263
|
# File 'lib/redis-call/redis_call.rb', line 258
def getnnil key
queued(get key) do |result|
raise(RedisCall::UnexpectedResult, "Key #{key.inspect} expected to be not nil") if result == nil
result
end
end
|
#getnnili(key) ⇒ Object
265
266
267
|
# File 'lib/redis-call/redis_call.rb', line 265
def getnnili key
queued(getnnil key) {|result| result.to_i}
end
|
#hgetallarr(key) ⇒ Object
277
278
279
280
281
282
283
|
# File 'lib/redis-call/redis_call.rb', line 277
def hgetallarr key
queued(hgetall key) do |raw|
result = []
Hash[*raw].each {|k, v| result[k.to_i] = v}
result
end
end
|
#insist(retries = 42, *exceptions) ⇒ Object
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/redis-call/redis_call.rb', line 225
def insist(retries = 42, *exceptions)
exceptions.push RedisCall::TransactionAborted
yield
rescue *exceptions => exception
if (retries -= 1) > 0
retry
else
raise exception
end
end
|
#key(name) ⇒ Object
48
49
50
|
# File 'lib/redis-call/redis_call.rb', line 48
def key name
RedisCall::Key.new name
end
|
#lgetall(key) ⇒ Object
273
274
275
|
# File 'lib/redis-call/redis_call.rb', line 273
def lgetall key
lrange key, 0, -1
end
|
#llen(key) ⇒ Object
254
255
256
|
# File 'lib/redis-call/redis_call.rb', line 254
def llen key
queued(call :LLEN, key) {|result| result.to_i}
end
|
#rpushex(key, ttl, value) ⇒ Object
237
238
239
240
241
242
|
# File 'lib/redis-call/redis_call.rb', line 237
def rpushex key, ttl, value
multi do
queued(rpush key, value) {|result| result}
queued(expire key, ttl)
end
end
|