Sha256: b038d9b0ade2901a49cb7728ce9700a397f9e39130500b83aac432ca6063fa40

Contents?: true

Size: 782 Bytes

Versions: 9

Compression:

Stored size: 782 Bytes

Contents

class ProconBypassMan::OnMemoryCache
  class CacheValue
    attr_accessor :expired_at, :value

    def initialize(expired_at: , value: )
      self.expired_at = expired_at
      self.value = value
    end
  end

  def initialize
    @table = {}
  end

  # @param [Integer] expires_in 秒数
  # @param [String] key
  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
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
procon_bypass_man-0.1.16.1 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.16 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.15 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.14 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.13 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.12 lib/procon_bypass_man/on_memory_cache.rb
procon_bypass_man-0.1.11 lib/procon_bypass_man/on_memory_cache.rb
procon_bypass_man-0.1.10 lib/procon_bypass_man/on_memory_cache.rb
procon_bypass_man-0.1.9 lib/procon_bypass_man/on_memory_cache.rb