class RedisCall

Copyright 2011 Stanislav Senotrusov <stan@senotrusov.com>

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Constants

DEFAULT_HOST
DEFAULT_PORT

Public Class Methods

config=(conf) click to toggle source
# File lib/redis-call/redis_call.rb, line 178
def self.config= conf
  @@config = conf
end
new(args = {}) click to toggle source
# 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
query(*args, &block) click to toggle source
# File lib/redis-call/redis_call.rb, line 171
def self.query(*args, &block)
  self.new(*args).instance_exec(&block)
end

Public Instance Methods

connect()
Alias for: connection
connection() click to toggle source
# File lib/redis-call/redis_call.rb, line 196
def connection
  @connection || (Thread.current[@pool_key] ||= Connection.new(@host, @port))
end
Also aliased as: connect
decrzerodelex(key, ttl) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/redis-call/redis_call.rb, line 269
def geti key
  queued(get key) {|result| result.to_i}
end
getnnil(key) click to toggle source
# 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) click to toggle source
# File lib/redis-call/redis_call.rb, line 265
def getnnili key
  queued(getnnil key) {|result| result.to_i}
end
hgetallarr(key) click to toggle source
# 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) { || ... } click to toggle source
# 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) click to toggle source
# File lib/redis-call/redis_call.rb, line 48
def key name
  RedisCall::Key.new name
end
lgetall(key) click to toggle source
# File lib/redis-call/redis_call.rb, line 273
def lgetall key
  lrange key,  0, -1
end
llen(key) click to toggle source
# File lib/redis-call/redis_call.rb, line 254
def llen key
  queued(call :LLEN, key) {|result| result.to_i}
end
method_missing(*args, &block) click to toggle source
# File lib/redis-call/redis_call.rb, line 221
def method_missing *args, &block
  connection.__send__ *args, &block
end
rpushex(key, ttl, value) click to toggle source
# 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