Class: Killbill::Plugin::ActiveMerchant::Utils::BoundedLRUCache

Inherits:
Object
  • Object
show all
Includes:
ThreadSafe::Util::CheapLockable
Defined in:
lib/killbill/helpers/active_merchant/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(proc, max_size = 10000) ⇒ BoundedLRUCache

Returns a new instance of BoundedLRUCache.



107
108
109
110
111
112
113
114
115
116
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 107

def initialize(proc, max_size = 10000)
  @max_size = max_size
  @data = ThreadSafe::Cache.new do |hash, key|
    # mapping key -> value is constant for our purposes
    set_value = hash.fetch_or_store key, value = proc.call(key)
    store_key(key) if value.equal?(set_value) # very same object
    set_value
  end
  @keys = []
end

Instance Method Details

#[](key) ⇒ Object



118
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 118

def [](key); @data[key] end

#[]=(key, val) ⇒ Object



120
121
122
123
124
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 120

def []=(key, val)
  prev_val = @data.get_and_set(key, val)
  prev_val.nil? ? store_key(key) : update_key(key)
  val
end

#keysObject



130
131
132
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 130

def keys
  cheap_synchronize { @keys.dup }
end

#sizeObject



127
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 127

def size; @data.size end

#valuesObject



135
136
137
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 135

def values
  cheap_synchronize { @keys.map { |key| self[key] } }
end