Class: ProconBypassMan::OnMemoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/support/on_memory_cache.rb

Defined Under Namespace

Classes: CacheValue

Instance Method Summary collapse

Constructor Details

#initializeOnMemoryCache

Returns a new instance of OnMemoryCache.



13
14
15
# File 'lib/procon_bypass_man/support/on_memory_cache.rb', line 13

def initialize
  @table = {}
end

Instance Method Details

#fetch(key:, expires_in:, &block) ⇒ Object

Parameters:

  • expires_in (Integer)

    秒数

  • key (String)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/procon_bypass_man/support/on_memory_cache.rb', line 19

def fetch(key: , expires_in: , &block)
  now = Time.now
  if @table[key].nil?
    value = block.call
    value_object = CacheValue.new(expired_at: now + expires_in, value: value)
    @table[key] = value_object
    return value
  end

  if @table[key].expired_at < now
    value = block.call
    @table[key] = CacheValue.new(expired_at: now + expires_in, value: value)
    return value
  else
    return @table[key].value
  end
end