class BELParser::Resource::LRUCache

LRUCache implements a least recently used cache. This implementation was adapted from github.com/SamSaffron/lru_redux.

Public Class Methods

new(max_size) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 8
def initialize(max_size)
        raise ArgumentError.new(:max_size) if max_size < 1

        @max_size = max_size
        @data = Concurrent::Hash.new
end

Public Instance Methods

[](key) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 47
def [](key)
        found = true
        value = @data.delete(key){ found = false }
        if found
                @data[key] = value
        else
                nil
        end
end
[]=(key,val) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 57
def []=(key,val)
        @data.delete(key)
        @data[key] = val
        @data.shift if @data.length > @max_size
        val
end
clear() click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 95
def clear
        @data.clear
end
count() click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 99
def count
        @data.size
end
delete(key) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 83
def delete(key)
        @data.delete(key)
end
Also aliased as: evict
each() { |pair| ... } click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 64
def each
        unless block_given?
                return enum_for(:each)
        end

        array = @data.to_a
        array.reverse!.each do |pair|
                yield pair
        end
end
Also aliased as: each_unsafe
each_unsafe()

used further up the chain, non thread safe each

Alias for: each
evict(key)
Alias for: delete
fetch(key) { || ... } click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 37
def fetch(key)
        found = true
        value = @data.delete(key){ found = false }
        if found
                @data[key] = value
        else
                yield if block_given?
        end
end
getset(key) { || ... } click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 25
def getset(key)
        found = true
        value = @data.delete(key){ found = false }
        if found
                @data[key] = value
        else
                result = @data[key] = yield
                @data.shift if @data.length > @max_size
                result
        end
end
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 89
def key?(key)
        @data.key?(key)
end
Also aliased as: has_key?
max_size=(max_size) click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 15
def max_size=(max_size)
        max_size ||= @max_size

        raise ArgumentError.new(:max_size) if max_size < 1

        @max_size = max_size

        @data.shift while @data.size > @max_size
end
to_a() click to toggle source
# File lib/bel_parser/resource/lru_cache.rb, line 78
def to_a
        array = @data.to_a
        array.reverse!
end

Protected Instance Methods

valid?() click to toggle source

for cache validation only, ensures all is sound

# File lib/bel_parser/resource/lru_cache.rb, line 106
def valid?
        true
end