Class: Xampl::XamplCacheLFU
- Inherits:
-
Object
- Object
- Xampl::XamplCacheLFU
- Defined in:
- lib/xamplr/persisters/caches.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #fetch(key, default_value = nil) ⇒ Object (also: #[])
-
#initialize(capacity = DEFAULT_CAPACITY) ⇒ XamplCacheLFU
constructor
A new instance of XamplCacheLFU.
- #limit ⇒ Object
- #print(out = "") ⇒ Object
- #size ⇒ Object
- #store(key, value) ⇒ Object (also: #[]=)
Constructor Details
#initialize(capacity = DEFAULT_CAPACITY) ⇒ XamplCacheLFU
Returns a new instance of XamplCacheLFU.
104 105 106 107 108 109 |
# File 'lib/xamplr/persisters/caches.rb', line 104 def initialize(capacity=DEFAULT_CAPACITY) @capacity = capacity @accesses = 0 @size = 0 @cache = {} end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
102 103 104 |
# File 'lib/xamplr/persisters/caches.rb', line 102 def cache @cache end |
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
102 103 104 |
# File 'lib/xamplr/persisters/caches.rb', line 102 def capacity @capacity end |
Instance Method Details
#delete(key) ⇒ Object
116 117 118 |
# File 'lib/xamplr/persisters/caches.rb', line 116 def delete(key) @cache.delete(key) end |
#fetch(key, default_value = nil) ⇒ Object Also known as: []
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/xamplr/persisters/caches.rb', line 144 def fetch(key, default_value=nil) @accesses += 1 pair = @cache[key] if pair then pair[1] += 1 return pair[0] else return default_value end end |
#limit ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/xamplr/persisters/caches.rb', line 120 def limit victim = nil actual_victim = nil min = 1 + @accesses live = 0 @cache.each do |key, pair| pair[1] -= 1 possibility = pair[0] if (not possibility.load_needed) and pair[1] < min then live += 1 victim = key actual_victim = possibility min = pair[1] end end #puts #puts #puts "REMOVE FROM CACHE(XamplCacheLFU): victim: #{victim}, actual: #{actual_victim} -- live: #{live}, size: #{@size}" #puts #puts #@cache.delete(victim) actual_victim.force_load if actual_victim end |
#print(out = "") ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/xamplr/persisters/caches.rb', line 174 def print(out = "") out << "Cache (LFU) with capacity: #{@capacity}, current size: #{@size}\n" @cache.each do |key, pair| out << sprintf(" key: '%s', value: '%s', count: %s\n", key, pair[0], pair[1]) end out end |
#size ⇒ Object
111 112 113 114 |
# File 'lib/xamplr/persisters/caches.rb', line 111 def size #@cache.size @size end |
#store(key, value) ⇒ Object Also known as: []=
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/xamplr/persisters/caches.rb', line 156 def store(key, value) @accesses += 1 if (@capacity <= @size) then self.limit end pair = @cache[key] if pair then pair[0] = value pair[1] += 1 else @cache[key] = [value, 1] end return value end |