Sha256: a4e530d65434962801e3140b88f7639004531048f4cbd375802e00d823969f02

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

=begin
    Copyright 2010-2022 Ecsypno <http://www.ecsypno.com>

    This file is part of the Arachni Framework project and is subject to
    redistribution and commercial restrictions. Please see the Arachni Framework
    web site for more information on licensing and terms of use.
=end

module Arachni
module Support::Cache

# 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>
class LeastCostReplacement < Base

    VALID_COSTS = [ :low, :medium, :high ]

    # @see Arachni::Cache::Base#initialize
    def initialize( * )
        super
        reset_costs
    end

    # Storage method
    #
    # @param    [Object]    k
    #   Entry key.
    # @param    [Object]    v
    #   Object to store.
    # @param    [Symbol]    cost
    #
    # @return   [Object]    `v`
    #
    # @see VALID_COSTS
    def store( k, v, cost = :low )
        fail( "invalid cost: #{cost}" ) if !valid_cost?( cost )

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

    # @see Arachni::Cache::Base#clear
    def clear
        super
    ensure
        reset_costs
    end

    private

    def reset_costs
        @costs = {}
        VALID_COSTS.each { |c| @costs[c] = [] }
    end

    def valid_cost?( cost )
        VALID_COSTS.include?( cost )
    end

    def candidate_from_cost_class( cost_class )
        return if (costs = @costs[cost_class]).empty?
        costs.delete_at( rand( costs.size ) )
    end

    def prune_candidate
        VALID_COSTS.each do |cost|
            if c = candidate_from_cost_class( cost )
                return c
            end
        end
    end

    def prune
        delete( prune_candidate )
    end

end

end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
arachni-1.6.1.3 lib/arachni/support/cache/least_cost_replacement.rb
arachni-1.6.1.2 lib/arachni/support/cache/least_cost_replacement.rb
arachni-1.6.1.1 lib/arachni/support/cache/least_cost_replacement.rb
arachni-1.6.1 lib/arachni/support/cache/least_cost_replacement.rb
arachni-1.6.0 lib/arachni/support/cache/least_cost_replacement.rb