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
Methods inherited from RedisBase
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, ={}) @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.
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, ={}) result = self[key] if result.nil? and block_given? result = yield key self[key] = result else raise KeyError end result end |
#clear ⇒ Object
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 |
#destroy ⇒ Object
67 68 69 |
# File 'lib/collectr/redis/redis_hash.rb', line 67 def destroy @store.del @title end |
#empty? ⇒ 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, ={}) 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
83 84 85 |
# File 'lib/collectr/redis/redis_hash.rb', line 83 def has_key?(key) key? key end |
#key?(key) ⇒ Boolean
86 87 88 |
# File 'lib/collectr/redis/redis_hash.rb', line 86 def key?(key) @store.hexists @title, serialize(key) end |
#keys ⇒ Object
90 91 92 |
# File 'lib/collectr/redis/redis_hash.rb', line 90 def keys @store.hkeys(@title).collect{ |key| deserialize key } end |
#size ⇒ Object
79 80 81 |
# File 'lib/collectr/redis/redis_hash.rb', line 79 def size @store.hlen @title end |
#to_hash ⇒ Object
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 |