Class: Redis::Connection::Memory::ExpiringHash
- Inherits:
-
Hash
- Object
- Hash
- Redis::Connection::Memory::ExpiringHash
- Defined in:
- lib/redis/connection/memory.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
- #values_at(*keys) ⇒ Object
Constructor Details
#initialize ⇒ ExpiringHash
Returns a new instance of ExpiringHash.
13 14 15 16 |
# File 'lib/redis/connection/memory.rb', line 13 def initialize(*) super @expires = {} end |
Instance Attribute Details
#expires ⇒ Object (readonly)
Returns the value of attribute expires.
11 12 13 |
# File 'lib/redis/connection/memory.rb', line 11 def expires @expires end |
Instance Method Details
#[](key) ⇒ Object
18 19 20 21 |
# File 'lib/redis/connection/memory.rb', line 18 def [](key) delete(key) if expired?(key) super end |
#[]=(key, val) ⇒ Object
23 24 25 26 |
# File 'lib/redis/connection/memory.rb', line 23 def []=(key, val) expire(key) super end |
#delete(key) ⇒ Object
28 29 30 31 |
# File 'lib/redis/connection/memory.rb', line 28 def delete(key) expire(key) super end |
#expire(key) ⇒ Object
33 34 35 |
# File 'lib/redis/connection/memory.rb', line 33 def expire(key) expires.delete(key) end |
#expired?(key) ⇒ Boolean
37 38 39 |
# File 'lib/redis/connection/memory.rb', line 37 def expired?(key) expires.include?(key) && expires[key] < Time.now end |
#key?(key) ⇒ Boolean
41 42 43 44 |
# File 'lib/redis/connection/memory.rb', line 41 def key?(key) delete(key) if expired?(key) super end |
#keys ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/redis/connection/memory.rb', line 51 def keys super.select do |key| if expired?(key) delete(key) false else true end end end |
#values_at(*keys) ⇒ Object
46 47 48 49 |
# File 'lib/redis/connection/memory.rb', line 46 def values_at(*keys) keys.each {|key| delete(key) if expired?(key)} super end |