class RedisHash
Constants
- VERSION
Attributes
name[R]
Public Class Methods
new(name, redis_or_options = {}, more_options = {})
click to toggle source
# File lib/redis_hash.rb, line 11 def initialize(name, redis_or_options = {}, more_options = {}) name = name.to_s if name.kind_of? Symbol raise InvalidNameException.new unless name.kind_of?(String) && name.size > 0 @name = name @redis = if redis_or_options.kind_of?(Redis) redis_or_options elsif redis_or_options.kind_of? Hash ::Redis.new redis_or_options elsif defined?(ActiveSupport::Cache::RedisStore) && redis_or_options.kind_of?(ActiveSupport::Cache::RedisStore) @pooled = redis_or_options.data.kind_of?(ConnectionPool) redis_or_options.data elsif defined?(ConnectionPool) && redis_or_options.kind_of?(ConnectionPool) @pooled = true redis_or_options else raise InvalidRedisConfigException.new end if more_options.kind_of?(Hash) && more_options[:expire] expire more_options[:expire] end end
Public Instance Methods
all()
click to toggle source
# File lib/redis_hash.rb, line 83 def all with{|redis| redis.hgetall name} end
clear()
click to toggle source
# File lib/redis_hash.rb, line 122 def clear with{|redis| redis.del name} {} end
Also aliased as: flush
enumerator(slice_size = 10)
click to toggle source
# File lib/redis_hash.rb, line 109 def enumerator(slice_size = 10) cursor = 0 Enumerator.new do |yielder| loop do cursor, items = scan cursor, slice_size items.each do |item| yielder << {item.first => item.last} end raise StopIteration if cursor.to_i.zero? end end end
expire(seconds)
click to toggle source
# File lib/redis_hash.rb, line 129 def expire seconds with{|redis| redis.expire name, seconds} end
get(*keys)
click to toggle source
# File lib/redis_hash.rb, line 35 def get *keys keys = keys.flatten if keys.size > 0 values = if 1 == keys.size [with{|redis| redis.hget(name, keys.first) }] else with{|redis| redis.hmget name, *keys } end keys.each_with_index.map do |k,i| [k, values[i]] end.to_h else {} end end
include?(key)
click to toggle source
# File lib/redis_hash.rb, line 95 def include? key with{|redis| redis.hexists(name, key)} end
increment_float_key(key, increment_amount = 1)
click to toggle source
# File lib/redis_hash.rb, line 72 def increment_float_key key, increment_amount = 1 with{|redis| redis.hincrbyfloat(name, key, increment_amount)} end
increment_integer_key(key, increment_amount = 1)
click to toggle source
# File lib/redis_hash.rb, line 68 def increment_integer_key key, increment_amount = 1 with{|redis| redis.hincrby(name, key, increment_amount)} end
keys()
click to toggle source
# File lib/redis_hash.rb, line 87 def keys with{|redis| redis.hkeys name} end
remove(*keys)
click to toggle source
# File lib/redis_hash.rb, line 76 def remove *keys keys = keys.flatten if keys.size > 0 with{|redis| redis.hdel name, keys} end end
scan(cursor = 0, amount = 10, match = "*")
click to toggle source
# File lib/redis_hash.rb, line 105 def scan cursor = 0, amount = 10, match = "*" with{|redis| redis.hscan name, cursor, :count => amount, :match => match} end
set(hash)
click to toggle source
# File lib/redis_hash.rb, line 52 def set hash if hash.size > 0 with do |redis| if 1 == hash.size redis.hset name, hash.keys.first, hash.values.first else redis.hmset name, *(hash.map { |k, v| [k, v] }.flatten) end end end end
set_if_does_not_exist(hash)
click to toggle source
# File lib/redis_hash.rb, line 64 def set_if_does_not_exist hash with{|redis| redis.hsetnx(name, hash.keys.first, hash.values.first)} end
size()
click to toggle source
# File lib/redis_hash.rb, line 99 def size with{|redis| redis.hlen name} end
Also aliased as: count
values()
click to toggle source
# File lib/redis_hash.rb, line 91 def values with{|redis| redis.hvals name} end
Private Instance Methods
pooled?()
click to toggle source
# File lib/redis_hash.rb, line 143 def pooled? !!@pooled end
with(&block)
click to toggle source
# File lib/redis_hash.rb, line 135 def with(&block) if pooled? @redis.with(&block) else block.call(@redis) end end