class Rack::Throttle::Capped::CacheStore

Attributes

cache[R]
cap[R]

Public Class Methods

new(cache: nil, cap: 3) click to toggle source
# File lib/rack/throttle/capped/cache_store.rb, line 7
def initialize(cache: nil, cap: 3)
  @cache = if cache
             cache
           elsif defined?(Concurrent::Hash)
             Concurrent::Hash.new
           else
             Hash.new
           end
  @cap   = cap
end

Public Instance Methods

[](k) click to toggle source
# File lib/rack/throttle/capped/cache_store.rb, line 24
def [](k)
  cache[k]
end
[]=(k, v) click to toggle source
# File lib/rack/throttle/capped/cache_store.rb, line 18
def []= (k, v)
  keys = @cache.keys.sort.reverse[0..cap-1]
  @cache = @cache.select{|k, v| keys.include?(k) }
  @cache[k] = v
end