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