Sha256: a54052934da0ab0629301336c1e8c3e8eb03adeeff1492a3c80cef9e6ff280d8

Contents?: true

Size: 1.52 KB

Versions: 32

Compression:

Stored size: 1.52 KB

Contents

module Cuboid
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@gmail.com>
class LeastCostReplacement < Base

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

    # @see Cuboid::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 Cuboid::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

32 entries across 32 versions & 1 rubygems

Version Path
cuboid-0.2.11 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.10 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.9 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.8 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.7 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.6 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.5 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.4.2 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.4.1 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.4 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.3 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.2 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2.1 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.2 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.9.1 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.9 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.8 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.7 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.6.1 lib/cuboid/support/cache/least_cost_replacement.rb
cuboid-0.1.6 lib/cuboid/support/cache/least_cost_replacement.rb