Class: FakeRedis::ExpiringHash
- Inherits:
-
Hash
- Object
- Hash
- FakeRedis::ExpiringHash
- Defined in:
- lib/fakeredis/expiring_hash.rb
Overview
Represents a normal hash with some additional expiration information associated with each key
Instance Attribute Summary collapse
-
#expires ⇒ Object
readonly
Returns the value of attribute expires.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #delete(key) ⇒ Object
- #expire(key) ⇒ Object
- #expired?(key) ⇒ Boolean
-
#initialize ⇒ ExpiringHash
constructor
A new instance of ExpiringHash.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #normalize(key) ⇒ Object
- #values_at(*keys) ⇒ Object
Constructor Details
#initialize ⇒ ExpiringHash
Returns a new instance of ExpiringHash.
7 8 9 10 |
# File 'lib/fakeredis/expiring_hash.rb', line 7 def initialize(*) super @expires = {} end |
Instance Attribute Details
#expires ⇒ Object (readonly)
Returns the value of attribute expires.
5 6 7 |
# File 'lib/fakeredis/expiring_hash.rb', line 5 def expires @expires end |
Instance Method Details
#[](key) ⇒ Object
12 13 14 15 16 |
# File 'lib/fakeredis/expiring_hash.rb', line 12 def [](key) key = normalize key delete(key) if expired?(key) super end |
#[]=(key, val) ⇒ Object
18 19 20 21 22 |
# File 'lib/fakeredis/expiring_hash.rb', line 18 def []=(key, val) key = normalize key expire(key) super end |
#delete(key) ⇒ Object
24 25 26 27 28 |
# File 'lib/fakeredis/expiring_hash.rb', line 24 def delete(key) key = normalize key expire(key) super end |
#expire(key) ⇒ Object
30 31 32 33 |
# File 'lib/fakeredis/expiring_hash.rb', line 30 def expire(key) key = normalize key expires.delete(key) end |
#expired?(key) ⇒ Boolean
35 36 37 38 |
# File 'lib/fakeredis/expiring_hash.rb', line 35 def expired?(key) key = normalize key expires.include?(key) && expires[key] <= Time.now end |
#key?(key) ⇒ Boolean
40 41 42 43 44 |
# File 'lib/fakeredis/expiring_hash.rb', line 40 def key?(key) key = normalize key delete(key) if expired?(key) super end |
#keys ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fakeredis/expiring_hash.rb', line 54 def keys super.select do |key| key = normalize(key) if expired?(key) delete(key) false else true end end end |
#normalize(key) ⇒ Object
66 67 68 |
# File 'lib/fakeredis/expiring_hash.rb', line 66 def normalize key key.to_s end |
#values_at(*keys) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/fakeredis/expiring_hash.rb', line 46 def values_at(*keys) keys.each do |key| key = normalize(key) delete(key) if expired?(key) end super end |