Sha256: 29e13dc53442c67437cd89f664513b5f5eccdcff03527b0f44c9fd4653e15550

Contents?: true

Size: 609 Bytes

Versions: 17

Compression:

Stored size: 609 Bytes

Contents

module Searchkick
  class IndexCache
    def initialize(max_size: 20)
      @data = {}
      @mutex = Mutex.new
      @max_size = max_size
    end

    # probably a better pattern for this
    # but keep it simple
    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

    def clear
      @mutex.synchronize do
        @data.clear
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
searchkick-5.4.0 lib/searchkick/index_cache.rb
searchkick-5.3.1 lib/searchkick/index_cache.rb
searchkick-5.3.0 lib/searchkick/index_cache.rb
searchkick-5.2.4 lib/searchkick/index_cache.rb
searchkick-5.2.3 lib/searchkick/index_cache.rb
searchkick-5.2.2 lib/searchkick/index_cache.rb
searchkick-5.2.1 lib/searchkick/index_cache.rb
searchkick-5.2.0 lib/searchkick/index_cache.rb
searchkick-5.1.2 lib/searchkick/index_cache.rb
searchkick-5.1.1 lib/searchkick/index_cache.rb
searchkick-5.1.0 lib/searchkick/index_cache.rb
searchkick-5.0.5 lib/searchkick/index_cache.rb
searchkick-5.0.4 lib/searchkick/index_cache.rb
searchkick-5.0.3 lib/searchkick/index_cache.rb
searchkick-5.0.2 lib/searchkick/index_cache.rb
searchkick-5.0.1 lib/searchkick/index_cache.rb
searchkick-5.0.0 lib/searchkick/index_cache.rb