class Redis::BigHash

Attributes

namespace[R]

Public Class Methods

copy_hash(source_key, dest_key) click to toggle source
# File lib/redis/big_hash.rb, line 95
def copy_hash(source_key, dest_key)
  keys(source_key).each do |k|
    redis.hset( dest_key, k,
      redis.hget(source_key, k) )
  end
end
keys(redis_key) click to toggle source
# File lib/redis/big_hash.rb, line 91
def keys(redis_key)
  redis.hkeys redis_key
end
new( key = nil, namespace = nil ) click to toggle source
# File lib/redis/big_hash.rb, line 10
def initialize( key = nil, namespace = nil )
  @key       = key
  @namespace = namespace
end

Public Instance Methods

[](*hash_keys) click to toggle source
# File lib/redis/big_hash.rb, line 15
def [](*hash_keys)
  if hash_keys.one?
    Redis::Marshal.load( redis.hget(redis_key, convert_key(hash_keys.first)) )
  elsif hash_keys.any?
    values = redis.hmget( redis_key, *hash_keys.map{ |k| convert_key(k) } )
    values.map{ |v| Redis::Marshal.load(v) }
  end
end
[]=(hash_key, value) click to toggle source
# File lib/redis/big_hash.rb, line 24
def []=(hash_key, value)
  redis.hset( redis_key, convert_key(hash_key), Redis::Marshal.dump(value) )
end
add(hash_key, value) click to toggle source

set only if key doesn’t already exist equivilent to doing ‘hash ||= value`, but more efficient

# File lib/redis/big_hash.rb, line 30
def add(hash_key, value)
  redis.hsetnx( redis_key, convert_key(hash_key), Redis::Marshal.dump(value) )
end
clear() click to toggle source
# File lib/redis/big_hash.rb, line 85
def clear
  redis.del redis_key
end
Also aliased as: destroy
count()
Alias for: size
delete(hash_key) click to toggle source
# File lib/redis/big_hash.rb, line 79
def delete(hash_key)
  current_value = self[hash_key]
  redis.hdel( redis_key, hash_key )
  current_value
end
destroy()
Alias for: clear
has_key?(hash_key)
Alias for: key?
include?(hash_key)
Alias for: key?
key=(new_key) click to toggle source
# File lib/redis/big_hash.rb, line 34
def key=(new_key)
  new_key = generate_key if new_key.nil?
  unless @key.nil? || @key == new_key
    self.class.copy_hash( redis_key, redis_key(new_key) )
    clear
  end
  @key = new_key
end
key?(hash_key) click to toggle source
# File lib/redis/big_hash.rb, line 55
def key?(hash_key)
  keys.include?(convert_key(hash_key))
end
Also aliased as: include?, has_key?, member?
keys() click to toggle source
# File lib/redis/big_hash.rb, line 51
def keys
  self.class.keys redis_key
end
length()
Alias for: size
member?(hash_key)
Alias for: key?
merge(other_hash)
Alias for: update
merge!(other_hash)
Alias for: update
namespace=(new_namespace) click to toggle source
# File lib/redis/big_hash.rb, line 43
def namespace=(new_namespace)
  unless new_namespace == namespace
    self.class.copy_hash( redis_key, redis_key(key, new_namespace) )
    clear
    @namespace = new_namespace
  end
end
size() click to toggle source
# File lib/redis/big_hash.rb, line 62
def size
  redis.hlen redis_key
end
Also aliased as: count, length
update(other_hash) click to toggle source
# File lib/redis/big_hash.rb, line 68
def update(other_hash)
  writes = []
  other_hash.each_pair do |hash_key, value|
    writes << hash_key.to_s
    writes << Redis::Marshal.dump( value )
  end
  redis.hmset(redis_key, *writes)
end
Also aliased as: merge, merge!