Sha256: fb1388c05973085c13b53c58738c49a2a32b3a2f0092cb9523ec9bfb4312772b

Contents?: true

Size: 819 Bytes

Versions: 29

Compression:

Stored size: 819 Bytes

Contents

class ProconBypassMan::OnMemoryCache
  class CacheValue
    # @param [Time]
    attr_accessor :expired_at
    attr_accessor :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

29 entries across 29 versions & 1 rubygems

Version Path
procon_bypass_man-0.3.12 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.11 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.10 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.9 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.8.1 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.8 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.7 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.6 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.5 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.4 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.3.1 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.3 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.2 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.1 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.3.0 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.2.3 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.2.2 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.2.1 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.2.0 lib/procon_bypass_man/support/on_memory_cache.rb
procon_bypass_man-0.1.23 lib/procon_bypass_man/support/on_memory_cache.rb