Class: RubyRoutes::Route::SmallLru
- Inherits:
-
Object
- Object
- RubyRoutes::Route::SmallLru
- Defined in:
- lib/ruby_routes/route/small_lru.rb
Overview
small LRU used for path generation cache
Instance Attribute Summary collapse
-
#evictions ⇒ Object
readonly
Returns the value of attribute evictions.
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
Instance Method Summary collapse
- #get(key) ⇒ Object
- #increment_hits ⇒ Object
- #increment_misses ⇒ Object
-
#initialize(max_size = 1024) ⇒ SmallLru
constructor
larger default to reduce eviction likelihood in benchmarks.
- #set(key, val) ⇒ Object
Constructor Details
#initialize(max_size = 1024) ⇒ SmallLru
larger default to reduce eviction likelihood in benchmarks
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ruby_routes/route/small_lru.rb', line 8 def initialize(max_size = 1024) @max_size = max_size @h = {} @hits = 0 @misses = 0 @evictions = 0 @hit_strategy = RubyRoutes::Constant::LRU_HIT_STRATEGY @miss_strategy = RubyRoutes::Constant::LRU_MISS_STRATEGY end |
Instance Attribute Details
#evictions ⇒ Object (readonly)
Returns the value of attribute evictions.
5 6 7 |
# File 'lib/ruby_routes/route/small_lru.rb', line 5 def evictions @evictions end |
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
5 6 7 |
# File 'lib/ruby_routes/route/small_lru.rb', line 5 def hits @hits end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
5 6 7 |
# File 'lib/ruby_routes/route/small_lru.rb', line 5 def misses @misses end |
Instance Method Details
#get(key) ⇒ Object
19 20 21 22 |
# File 'lib/ruby_routes/route/small_lru.rb', line 19 def get(key) strategy = @h.key?(key) ? @hit_strategy : @miss_strategy strategy.call(self, key) end |
#increment_hits ⇒ Object
34 35 36 |
# File 'lib/ruby_routes/route/small_lru.rb', line 34 def increment_hits @hits += 1 end |
#increment_misses ⇒ Object
38 39 40 |
# File 'lib/ruby_routes/route/small_lru.rb', line 38 def increment_misses @misses += 1 end |
#set(key, val) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/ruby_routes/route/small_lru.rb', line 24 def set(key, val) @h.delete(key) if @h.key?(key) @h[key] = val if @h.size > @max_size @h.shift @evictions += 1 end val end |