Class: Rack::Throttle::Capped::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/throttle/capped/cache_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache: nil, cap: 3) ⇒ CacheStore

Returns a new instance of CacheStore.



7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5
6
7
# File 'lib/rack/throttle/capped/cache_store.rb', line 5

def cache
  @cache
end

#capObject (readonly)

Returns the value of attribute cap.



5
6
7
# File 'lib/rack/throttle/capped/cache_store.rb', line 5

def cap
  @cap
end

Instance Method Details

#[](k) ⇒ Object



24
25
26
# File 'lib/rack/throttle/capped/cache_store.rb', line 24

def [](k)
  cache[k]
end

#[]=(k, v) ⇒ Object



18
19
20
21
22
# 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