Class: SessionValidator::InMemoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/session_validator/in_memory_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ InMemoryCache

Returns a new instance of InMemoryCache.



3
4
5
6
# File 'lib/session_validator/in_memory_cache.rb', line 3

def initialize(ttl)
  @ttl = ttl
  @cache = {}
end

Instance Method Details

#cleanupObject



21
22
23
# File 'lib/session_validator/in_memory_cache.rb', line 21

def cleanup
  @cache.delete_if { |_, data| expired? data }
end

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/session_validator/in_memory_cache.rb', line 25

def empty?
  @cache.length == 0
end

#get(key) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/session_validator/in_memory_cache.rb', line 8

def get(key)
  return nil unless @cache.key? key

  data = @cache[key]
  return nil if expired? data

  data[:value]
end

#set(key, value) ⇒ Object



17
18
19
# File 'lib/session_validator/in_memory_cache.rb', line 17

def set(key, value)
  @cache[key] = { value: value, expiry: Time.now.to_i + @ttl }
end