Class: Redis::Connection::Memory::ExpiringHash

Inherits:
Hash
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeExpiringHash

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

#expiresObject (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

Returns:

  • (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

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/redis/connection/memory.rb', line 41

def key?(key)
  delete(key) if expired?(key)
  super
end

#keysObject



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