Class: Collectr::RedisHash

Inherits:
RedisBase show all
Defined in:
lib/collectr/redis/redis_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RedisBase

#deserialize, #serialize

Constructor Details

#initialize(name, options = {}) ⇒ RedisHash

Returns a new instance of RedisHash.



27
28
29
30
31
32
33
# File 'lib/collectr/redis/redis_hash.rb', line 27

def initialize(name, options={})
  @title = name
  # Use raw only when both the keys and values are strings.
  @raw = options.fetch(:raw) { false }
  @store = Redis.current
  @expires_in = options[:expires_in]
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



25
26
27
# File 'lib/collectr/redis/redis_hash.rb', line 25

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/collectr/redis/redis_hash.rb', line 35

def [](key)
  deserialize @store.hget(@title, serialize(key))
end

#[]=(key, val) ⇒ Object



39
40
41
# File 'lib/collectr/redis/redis_hash.rb', line 39

def []=(key, val)
  @store.hset @title, serialize(key), serialize(val)
end

#cache(key, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/collectr/redis/redis_hash.rb', line 56

def cache(key, options={})
  result = self[key]
  if result.nil? and block_given?
    result = yield key
    self[key] = result
  else
    raise KeyError
  end
  result
end

#clearObject



102
103
104
105
# File 'lib/collectr/redis/redis_hash.rb', line 102

def clear
  destroy
  # keys.each{ |key| delete key }
end

#delete(key) ⇒ Object



71
72
73
# File 'lib/collectr/redis/redis_hash.rb', line 71

def delete(key)
  @store.hdel @title, serialize(key)
end

#destroyObject



67
68
69
# File 'lib/collectr/redis/redis_hash.rb', line 67

def destroy
  @store.del @title
end

#empty?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/collectr/redis/redis_hash.rb', line 75

def empty?
  size == 0
end

#fetch(key, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/collectr/redis/redis_hash.rb', line 43

def fetch(key, options={})
  result = self[key]
  if result.nil?
    return nil if has_key?(key)
    if block_given?
      result = yield key
    else
      raise KeyError
    end
  end
  result
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/collectr/redis/redis_hash.rb', line 83

def has_key?(key)
  key? key
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/collectr/redis/redis_hash.rb', line 86

def key?(key)
  @store.hexists @title, serialize(key)
end

#keysObject



90
91
92
# File 'lib/collectr/redis/redis_hash.rb', line 90

def keys
  @store.hkeys(@title).collect{ |key| deserialize key }
end

#sizeObject



79
80
81
# File 'lib/collectr/redis/redis_hash.rb', line 79

def size
  @store.hlen @title
end

#to_hashObject



94
95
96
97
98
99
100
# File 'lib/collectr/redis/redis_hash.rb', line 94

def to_hash
  hash = {}
  @store.hgetall(@title).each do |key, val|
    hash[deserialize(key)] = deserialize(val)
  end
  hash
end