Class: SizedList
Instance Attribute Summary collapse
-
#enable_time_based_stats ⇒ Object
Basic Stats.
-
#evictions ⇒ Object
readonly
Returns the value of attribute evictions.
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#last_time_between_evictions ⇒ Object
readonly
Returns the value of attribute last_time_between_evictions.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
-
#writes ⇒ Object
readonly
Returns the value of attribute writes.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #each ⇒ Object
- #evicted? ⇒ Boolean
- #eviction_frequency ⇒ Object
- #exist?(key) ⇒ Boolean (also: #exists?)
- #get(key) ⇒ Object (also: #[])
-
#initialize(max_size) ⇒ SizedList
constructor
A new instance of SizedList.
- #keys ⇒ Object
- #reset_stats ⇒ Object
- #set(key, value) ⇒ Object (also: #[]=)
- #size ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(max_size) ⇒ SizedList
Returns a new instance of SizedList.
15 16 17 18 19 |
# File 'lib/sized_list.rb', line 15 def initialize(max_size) @max_size = max_size @items = {} self.reset_stats end |
Instance Attribute Details
#enable_time_based_stats ⇒ Object
Basic Stats
7 8 9 |
# File 'lib/sized_list.rb', line 7 def enable_time_based_stats @enable_time_based_stats end |
#evictions ⇒ Object (readonly)
Returns the value of attribute evictions.
9 10 11 |
# File 'lib/sized_list.rb', line 9 def evictions @evictions end |
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
9 10 11 |
# File 'lib/sized_list.rb', line 9 def hits @hits end |
#last_time_between_evictions ⇒ Object (readonly)
Returns the value of attribute last_time_between_evictions.
9 10 11 |
# File 'lib/sized_list.rb', line 9 def last_time_between_evictions @last_time_between_evictions end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
4 5 6 |
# File 'lib/sized_list.rb', line 4 def max_size @max_size end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
9 10 11 |
# File 'lib/sized_list.rb', line 9 def misses @misses end |
#writes ⇒ Object (readonly)
Returns the value of attribute writes.
9 10 11 |
# File 'lib/sized_list.rb', line 9 def writes @writes end |
Instance Method Details
#delete(key) ⇒ Object
56 57 58 |
# File 'lib/sized_list.rb', line 56 def delete(key) @items.delete key end |
#each ⇒ Object
60 61 62 63 64 |
# File 'lib/sized_list.rb', line 60 def each @items.each do |k, v| yield v end end |
#evicted? ⇒ Boolean
78 79 80 |
# File 'lib/sized_list.rb', line 78 def evicted? !! @evicted end |
#eviction_frequency ⇒ Object
87 88 89 90 |
# File 'lib/sized_list.rb', line 87 def eviction_frequency return 0.0 unless @enable_time_based_stats && @evictions > 1 @total_eviction_time / @evictions end |
#exist?(key) ⇒ Boolean Also known as: exists?
82 83 84 |
# File 'lib/sized_list.rb', line 82 def exist?(key) @items.has_key? key end |
#get(key) ⇒ Object Also known as: []
31 32 33 34 35 36 37 38 39 |
# File 'lib/sized_list.rb', line 31 def get(key) if value = @items[key] @hits += 1 used! key else @misses += 1 end value end |
#keys ⇒ Object
70 71 72 |
# File 'lib/sized_list.rb', line 70 def keys @items.keys end |
#reset_stats ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/sized_list.rb', line 21 def reset_stats @hits = 0 @misses = 0 @writes = 0 @evictions = 0 @total_eviction_time = 0.0 @last_time_between_evictions = 0.0 @last_evicted_at = nil end |
#set(key, value) ⇒ Object Also known as: []=
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sized_list.rb', line 42 def set(key, value) @writes += 1 unless exist?(key) @items[key] = value if @items.size > @max_size @evicted = true remove_least_recently_used! else @evicted = false end used! key nil end |
#size ⇒ Object
66 67 68 |
# File 'lib/sized_list.rb', line 66 def size @items.size end |
#values ⇒ Object
74 75 76 |
# File 'lib/sized_list.rb', line 74 def values @items.values end |