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