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.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fifocache.rb', line 77 def initialize(size, opts = { }) @data = { } @queue = Depq::new @counts = { } @size = size @puts = !!opts[:puts] @hits = !!opts[:hits] @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 #[]=.
64 65 66 |
# File 'lib/fifocache.rb', line 64 def factor @factor end |
#hits ⇒ Boolean
Indicates hits should be tracked.
51 52 53 |
# File 'lib/fifocache.rb', line 51 def hits @hits end |
#puts ⇒ Boolean
Indicates puts should be tracked.
43 44 45 |
# File 'lib/fifocache.rb', line 43 def puts @puts end |
#size ⇒ Integer
Contains maximal size of the cache.
35 36 37 |
# File 'lib/fifocache.rb', line 35 def size @size end |
Instance Method Details
#[](key) ⇒ Object
Returns item from cache.
131 132 133 134 135 136 137 |
# File 'lib/fifocache.rb', line 131 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fifocache.rb', line 102 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.
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/fifocache.rb', line 218 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.
238 239 240 241 242 |
# File 'lib/fifocache.rb', line 238 def clear! @data.replace({ }) @counts.replace({ }) @queue.clear end |
#has_key?(key) ⇒ Boolean Also known as: include?
Indicates key is in cache.
146 147 148 |
# File 'lib/fifocache.rb', line 146 def has_key?(key) @data.has_key? key end |
#length ⇒ Integer
Indicates current size of the cache.
174 175 176 |
# File 'lib/fifocache.rb', line 174 def length @data.length end |
#remove(key) ⇒ Object
Removes key.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/fifocache.rb', line 198 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.
251 252 253 |
# File 'lib/fifocache.rb', line 251 def to_h @data.dup end |
#touch(key) ⇒ Object
Touches key in cache.
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/fifocache.rb', line 157 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 |