lib/circuitbox/memory_store.rb in circuitbox-2.0.0.pre4 vs lib/circuitbox/memory_store.rb in circuitbox-2.0.0.pre5

- old
+ new

@@ -1,13 +1,13 @@ # frozen_string_literal: true -require_relative 'memory_store/monotonic_time' +require_relative 'time_helper/monotonic' require_relative 'memory_store/container' class Circuitbox class MemoryStore - include MonotonicTime + include TimeHelper::Monotonic def initialize(compaction_frequency: 60) @store = {} @mutex = Mutex.new @compaction_frequency = compaction_frequency @@ -25,11 +25,11 @@ seconds_to_expire = opts.fetch(:expires, 0) @mutex.synchronize do existing_container = fetch_container(key) - # reusing the existing container is a small optmization + # reusing the existing container is a small optimization # to reduce the amount of objects created if existing_container existing_container.expires_after(seconds_to_expire) existing_container.value += amount else @@ -38,26 +38,31 @@ end end end def load(key, _opts = {}) - @mutex.synchronize { fetch_value(key) } + @mutex.synchronize { fetch_container(key)&.value } end + def values_at(*keys, **_opts) + @mutex.synchronize do + current_time = current_second + keys.map! { |key| fetch_container(key, current_time)&.value } + end + end + def key?(key) @mutex.synchronize { !fetch_container(key).nil? } end def delete(key) @mutex.synchronize { @store.delete(key) } end private - def fetch_container(key) - current_time = current_second - + def fetch_container(key, current_time = current_second) compact(current_time) if @compact_after < current_time container = @store[key] return unless container @@ -66,16 +71,9 @@ @store.delete(key) nil else container end - end - - def fetch_value(key) - container = fetch_container(key) - return unless container - - container.value end def compact(current_time) @store.delete_if { |_, value| value.expired_at?(current_time) } @compact_after = current_time + @compaction_frequency