class SessionValidator::InMemoryCache

Public Class Methods

new(ttl) click to toggle source
# File lib/session_validator/in_memory_cache.rb, line 3
def initialize(ttl)
  @ttl = ttl
  @cache = {}
end

Public Instance Methods

cleanup() click to toggle source
# File lib/session_validator/in_memory_cache.rb, line 21
def cleanup
  @cache.delete_if { |_, data| expired? data }
end
empty?() click to toggle source
# File lib/session_validator/in_memory_cache.rb, line 25
def empty?
  @cache.length == 0
end
get(key) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

expired?(data) click to toggle source
# File lib/session_validator/in_memory_cache.rb, line 31
def expired?(data)
  Time.now.to_i >= data[:expiry]
end