Class: Killbill::Plugin::ActiveMerchant::Utils::BoundedLRUCache
- Includes:
- ThreadSafe::Util::CheapLockable
- Defined in:
- lib/killbill/helpers/active_merchant/utils.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#initialize(proc, max_size = 10000) ⇒ BoundedLRUCache
constructor
A new instance of BoundedLRUCache.
- #keys ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
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 |
#keys ⇒ Object
130 131 132 |
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 130 def keys cheap_synchronize { @keys.dup } end |
#size ⇒ Object
127 |
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 127 def size; @data.size end |
#values ⇒ Object
135 136 137 |
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 135 def values cheap_synchronize { @keys.map { |key| self[key] } } end |