Class: UniCache::LruEviction
- Inherits:
-
Object
- Object
- UniCache::LruEviction
- Defined in:
- lib/unicache.rb
Overview
LeastRecentlyUsed eviction policy. Mutex should be used to ensure atomicity of operations.
Eviction policy class should include methods:
- update
-
Called when key is accessed (get/put).
- remove
-
Called when key is removed.
- nextEvict
-
Peek to what is going to be removed next.
- clear
-
Reset eviction state.
Instance Method Summary (collapse)
-
- (Object) clear
Clear eviction list.
-
- (LruEviction) initialize
constructor
Instantiate.
-
- (Object) nextEvict
Return the oldest i.e.
-
- (Object) remove(key = nil)
Remove oldest entry.
-
- (Object) update(key)
Keep track of the least recently used keys.
Constructor Details
- (LruEviction) initialize
Instantiate.
359 360 361 362 |
# File 'lib/unicache.rb', line 359 def initialize @lock = Mutex.new clear end |
Instance Method Details
- (Object) clear
Clear eviction list.
366 367 368 369 370 |
# File 'lib/unicache.rb', line 366 def clear @lock.lock @list = [] @lock.unlock end |
- (Object) nextEvict
Return the oldest i.e. next to be evicted.
413 414 415 |
# File 'lib/unicache.rb', line 413 def nextEvict @list[0] end |
- (Object) remove(key = nil)
Remove oldest entry.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/unicache.rb', line 394 def remove( key = nil ) @lock.lock res = nil if key @list.delete_if do |i| i == key end res = key else res = @list.shift end @lock.unlock res end |
- (Object) update(key)
Keep track of the least recently used keys. Place oldest at the beginning of the list.
377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/unicache.rb', line 377 def update( key ) @lock.lock # Delete old entries of this key. @list.delete_if do |i| i == key end # Add to end of LRU. @list.push key @lock.unlock end |