class Searchkick::IndexCache
Public Class Methods
Source
# File lib/searchkick/index_cache.rb, line 3 def initialize(max_size: 20) @data = {} @mutex = Mutex.new @max_size = max_size end
Public Instance Methods
Source
# File lib/searchkick/index_cache.rb, line 24 def clear @mutex.synchronize do @data.clear end end
Source
# File lib/searchkick/index_cache.rb, line 11 def fetch(name) # thread-safe in MRI without mutex # due to how context switching works @mutex.synchronize do if @data.key?(name) @data[name] else @data.clear if @data.size >= @max_size @data[name] = yield end end end
probably a better pattern for this but keep it simple