Class: Fifocache
- Inherits:
-
Object
- Object
- Fifocache
- Defined in:
- lib/fifocache.rb
Overview
Represents cache object accessible by similar way as hash, but with fixed capacity with FIFO funcionality.
It’s useful for limited size caches. Oldest cache records are removed. Also can be used by dynamic mode, so the less putted or less accessed (or both) cache records are removed instead of the oldest records in that cases.
For touches tracking utilizes implicit heap queue.
Constant Summary collapse
- INFINITY =
Integer overflow protection.
1.0/0
Instance Attribute Summary collapse
-
#factor ⇒ Float
Indicates new items handicap factor.
-
#hits ⇒ Boolean
Indicates hits should be tracked.
-
#puts ⇒ Boolean
Indicates puts should be tracked.
-
#size ⇒ Integer
Contains maximal size of the cache.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns item from cache.
-
#[]=(key, item) ⇒ Object
Puts item with key to cache.
-
#clean!(count = 1) ⇒ Hash
(also: #clean)
Cleans specified number of slots.
-
#clear! ⇒ Object
(also: #clear)
Clear whole cache.
-
#has_key?(key) ⇒ Boolean
(also: #include?)
Indicates key is in cache.
-
#initialize(size, opts = { }) ⇒ Fifocache
constructor
Constructor.
-
#length ⇒ Integer
Indicates current size of the cache.
-
#remove(key) ⇒ Object
Removes key.
-
#to_h ⇒ Hash
Converts to hash.
-
#touch(key) ⇒ Object
Touches key in cache.
Constructor Details
#initialize(size, opts = { }) ⇒ Fifocache
Constructor. Initializes cache to appropriate size.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/fifocache.rb', line 79 def initialize(size, opts = { }) @data = { } @queue = Depq::new @counts = { } @size = size @puts = opts[:puts].to_b @hits = opts[:hits].to_b @factor = opts[:factor].to_f end |
Instance Attribute Details
#factor ⇒ Float
Indicates new items handicap factor.
Handicap factor is multiplier of the min hits count of all items in the cache. It’s important set it in some cases. See #[]=.
66 67 68 |
# File 'lib/fifocache.rb', line 66 def factor @factor end |
#hits ⇒ Boolean
Indicates hits should be tracked.
53 54 55 |
# File 'lib/fifocache.rb', line 53 def hits @hits end |
#puts ⇒ Boolean
Indicates puts should be tracked.
45 46 47 |
# File 'lib/fifocache.rb', line 45 def puts @puts end |
#size ⇒ Integer
Contains maximal size of the cache.
37 38 39 |
# File 'lib/fifocache.rb', line 37 def size @size end |
Instance Method Details
#[](key) ⇒ Object
Returns item from cache.
133 134 135 136 137 138 139 |
# File 'lib/fifocache.rb', line 133 def [](key) if @hits self.touch(key) end @data[key] end |
#[]=(key, item) ⇒ Object
Puts item with key to cache.
If tracking is turned on and no #factor explicitly set, handicap 1 is assigned to new items. It’s safe, but not very acceptable because cache will become static after filling. So it’s necessary (or at least higly reasonable) to set priority weighting factor to number higher than 1 according dynamics of your application.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/fifocache.rb', line 104 def []=(key, item) # Adds to cache if not self.has_key? key # Inserts to tracking structures @data[key] = item locator = @queue.insert(key, __new_priority) @counts[key] = locator # Eventually removes first (last) if self.length > @size self.clean! end elsif @puts self.touch(key) end end |
#clean!(count = 1) ⇒ Hash Also known as: clean
Cleans specified number of slots.
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/fifocache.rb', line 220 def clean!(count = 1) result = { } count.times do dkey = @queue.delete_min result[dkey] = __purge(dkey) if @data.empty? break end end return result end |
#clear! ⇒ Object Also known as: clear
Clear whole cache.
240 241 242 243 244 |
# File 'lib/fifocache.rb', line 240 def clear! @data.replace({ }) @counts.replace({ }) @queue.clear end |
#has_key?(key) ⇒ Boolean Also known as: include?
Indicates key is in cache.
148 149 150 |
# File 'lib/fifocache.rb', line 148 def has_key?(key) @data.has_key? key end |
#length ⇒ Integer
Indicates current size of the cache.
176 177 178 |
# File 'lib/fifocache.rb', line 176 def length @data.length end |
#remove(key) ⇒ Object
Removes key.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/fifocache.rb', line 200 def remove(key) if self.has_key? key # Heap queue locator = @counts[key] @queue.delete_locator(locator) # Data holders result = __purge(key) else result = nil end return result end |
#to_h ⇒ Hash
Converts to hash.
253 254 255 |
# File 'lib/fifocache.rb', line 253 def to_h @data.dup end |
#touch(key) ⇒ Object
Touches key in cache.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/fifocache.rb', line 159 def touch(key) locator = @counts[key] if locator priority = locator.priority + 1 end if locator and priority != self.class::INFINITY locator.update(key, locator.priority + 1) end end |