Class: Collectr::RedisHash
- Defined in:
- lib/collectr/redis/redis_hash.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #cache(key, options = {}) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #destroy ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, options = {}) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(name, options = {}) ⇒ RedisHash
constructor
A new instance of RedisHash.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #size ⇒ Object
- #to_hash ⇒ Object
- #values ⇒ Object
Methods inherited from RedisBase
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, ={}) @title = name # Use raw only when both the keys and values are strings. @raw = .fetch(:raw) { false } @store = Redis.current @expires_in = [:expires_in] end |
Instance Attribute Details
#store ⇒ Object (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, ={}) result = self[key] if result.nil? and block_given? result = yield key self[key] = result else raise KeyError end result end |
#clear ⇒ Object
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 |
#destroy ⇒ Object
49 50 51 |
# File 'lib/collectr/redis/redis_hash.rb', line 49 def destroy @store.del @title end |
#empty? ⇒ 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, ={}) 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
65 66 67 |
# File 'lib/collectr/redis/redis_hash.rb', line 65 def has_key?(key) key? key end |
#key?(key) ⇒ Boolean
68 69 70 |
# File 'lib/collectr/redis/redis_hash.rb', line 68 def key?(key) @store.hexists @title, serialize(key) end |
#keys ⇒ Object
72 73 74 |
# File 'lib/collectr/redis/redis_hash.rb', line 72 def keys @store.hkeys(@title).collect{ |key| deserialize key } end |
#size ⇒ Object
61 62 63 |
# File 'lib/collectr/redis/redis_hash.rb', line 61 def size @store.hlen @title end |
#to_hash ⇒ Object
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 |
#values ⇒ Object
76 77 78 |
# File 'lib/collectr/redis/redis_hash.rb', line 76 def values keys.collect{ |key| self[key] } end |