class Arachni::Support::Cache::LeastCostReplacement

Least Cost Replacement cache implementation.

Maintains 3 cost classes (low, medium, high) ) and discards entries from the lowest cost classes in order to make room for new ones.

@author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>

Constants

VALID_COSTS

Public Class Methods

new( * ) click to toggle source

@see Arachni::Cache::Base#initialize

Calls superclass method Arachni::Support::Cache::Base::new
# File lib/arachni/support/cache/least_cost_replacement.rb, line 23
def initialize( * )
    super
    reset_costs
end

Public Instance Methods

clear() click to toggle source

@see Arachni::Cache::Base#clear

Calls superclass method Arachni::Support::Cache::Base#clear
# File lib/arachni/support/cache/least_cost_replacement.rb, line 48
def clear
    super
ensure
    reset_costs
end
store( k, v, cost = :low ) click to toggle source

Storage method

@param [Object] k

Entry key.

@param [Object] v

Object to store.

@param [Symbol] cost

@return [Object] ‘v`

@see VALID_COSTS

Calls superclass method Arachni::Support::Cache::Base#store
# File lib/arachni/support/cache/least_cost_replacement.rb, line 39
def store( k, v, cost = :low )
    fail( "invalid cost: #{cost}" ) if !valid_cost?( cost )

    super( k, v )
ensure
    @costs[cost] << k
end

Private Instance Methods

candidate_from_cost_class( cost_class ) click to toggle source
# File lib/arachni/support/cache/least_cost_replacement.rb, line 65
def candidate_from_cost_class( cost_class )
    return if (costs = @costs[cost_class]).empty?
    costs.delete_at( rand( costs.size ) )
end
prune() click to toggle source
# File lib/arachni/support/cache/least_cost_replacement.rb, line 78
def prune
    delete( prune_candidate )
end
prune_candidate() click to toggle source
# File lib/arachni/support/cache/least_cost_replacement.rb, line 70
def prune_candidate
    VALID_COSTS.each do |cost|
        if c = candidate_from_cost_class( cost )
            return c
        end
    end
end
reset_costs() click to toggle source
# File lib/arachni/support/cache/least_cost_replacement.rb, line 56
def reset_costs
    @costs = {}
    VALID_COSTS.each { |c| @costs[c] = [] }
end
valid_cost?( cost ) click to toggle source
# File lib/arachni/support/cache/least_cost_replacement.rb, line 61
def valid_cost?( cost )
    VALID_COSTS.include?( cost )
end