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.



9
10
11
12
13
14
15
# File 'lib/collectr/redis/redis_hash.rb', line 9

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.



7
8
9
# File 'lib/collectr/redis/redis_hash.rb', line 7

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/collectr/redis/redis_hash.rb', line 17

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

#[]=(key, val) ⇒ Object



21
22
23
# File 'lib/collectr/redis/redis_hash.rb', line 21

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

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/collectr/redis/redis_hash.rb', line 38

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



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

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

#delete(key) ⇒ Object



53
54
55
# File 'lib/collectr/redis/redis_hash.rb', line 53

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

#destroyObject



49
50
51
# File 'lib/collectr/redis/redis_hash.rb', line 49

def destroy
  @store.del @title
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/collectr/redis/redis_hash.rb', line 57

def empty?
  size == 0
end

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/collectr/redis/redis_hash.rb', line 25

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)


65
66
67
# File 'lib/collectr/redis/redis_hash.rb', line 65

def has_key?(key)
  key? key
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

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

#sizeObject



61
62
63
# File 'lib/collectr/redis/redis_hash.rb', line 61

def size
  @store.hlen @title
end

#to_hashObject



80
81
82
83
84
85
86
# File 'lib/collectr/redis/redis_hash.rb', line 80

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

#valuesObject



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

def values
  keys.collect{ |key| self[key] }
end