class Searchkick::IndexCache

Public Class Methods

new(max_size: 20) click to toggle 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

clear() click to toggle source
# File lib/searchkick/index_cache.rb, line 24
def clear
  @mutex.synchronize do
    @data.clear
  end
end
fetch(name) { || ... } click to toggle source

probably a better pattern for this but keep it simple

# 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