class UniCache::LruEviction

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.

Public Class Methods

new() click to toggle source

Instantiate.

# File lib/unicache.rb, line 359
def initialize
    @lock = Mutex.new
    clear
end

Public Instance Methods

clear() click to toggle source

Clear eviction list.

# File lib/unicache.rb, line 366
def clear
    @lock.lock
    @list = []
    @lock.unlock
end
nextEvict() click to toggle source

Return the oldest i.e. next to be evicted.

# File lib/unicache.rb, line 413
def nextEvict
    @list[0]
end
remove( key = nil ) click to toggle source

Remove oldest entry.

@param key [Object] If given, remove this entry instead of oldest.

# 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
update( key ) click to toggle source

Keep track of the least recently used keys. Place oldest at the beginning of the list.

@param key [Object] Entry key.

# 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