Class: Redis::BigHash

Inherits:
Object
  • Object
show all
Includes:
ClientHelper, KeyHelper
Defined in:
lib/redis/big_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeyHelper

#convert_key, #generate_key, #key, #redis_key

Methods included from ClientHelper

included

Constructor Details

#initialize(key = nil, namespace = nil) ⇒ BigHash

Returns a new instance of BigHash.



10
11
12
13
# File 'lib/redis/big_hash.rb', line 10

def initialize( key = nil, namespace = nil )
  @key       = key
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/redis/big_hash.rb', line 8

def namespace
  @namespace
end

Class Method Details

.copy_hash(source_key, dest_key) ⇒ Object



95
96
97
98
99
100
# 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) ⇒ Object



91
92
93
# File 'lib/redis/big_hash.rb', line 91

def keys(redis_key)
  redis.hkeys redis_key
end

Instance Method Details

#[](*hash_keys) ⇒ Object



15
16
17
18
19
20
21
22
# 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) ⇒ Object



24
25
26
# 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) ⇒ Object

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



30
31
32
# 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

#clearObject Also known as: destroy



85
86
87
# File 'lib/redis/big_hash.rb', line 85

def clear
  redis.del redis_key
end

#delete(hash_key) ⇒ Object



79
80
81
82
83
# 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

#key=(new_key) ⇒ Object



34
35
36
37
38
39
40
41
# 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) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


55
56
57
# File 'lib/redis/big_hash.rb', line 55

def key?(hash_key)
  keys.include?(convert_key(hash_key))
end

#keysObject



51
52
53
# File 'lib/redis/big_hash.rb', line 51

def keys
  self.class.keys redis_key
end

#sizeObject Also known as: count, length



62
63
64
# File 'lib/redis/big_hash.rb', line 62

def size
  redis.hlen redis_key
end

#update(other_hash) ⇒ Object Also known as: merge, merge!



68
69
70
71
72
73
74
75
# 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