Sha256: 9c046d8847a067ec0da5b592bd79f5082633acb5a98cdf990cecaf5e2ce34651

Contents?: true

Size: 810 Bytes

Versions: 11

Compression:

Stored size: 810 Bytes

Contents

# frozen_string_literal: true

module Steppy
  # https://github.com/jeremyevans/roda/blob/master/lib/roda.rb#L14-L42
  # A thread safe cache class, offering only #[] and #[]= methods,
  # each protected by a mutex.
  class SteppyCache
    # Create a new thread safe cache.
    def initialize(hash = {})
      @mutex = Mutex.new
      @hash  = hash
    end

    # Make getting value from underlying hash thread safe.
    def [](key)
      @mutex.synchronize { @hash[key] }
    end

    # Make setting value in underlying hash thread safe.
    def []=(key, value)
      @mutex.synchronize { @hash[key] = value }
    end

    def to_h
      @mutex.synchronize { @hash }
    end

    def method_missing(method, *args, &block)
      @mutex.synchronize { @hash.public_send(method, *args, &block) }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
steppy-1.0.8 lib/steppy/cache.rb
steppy-1.0.7 lib/steppy/cache.rb
steppy-1.0.6 lib/steppy/cache.rb
steppy-1.0.5 lib/steppy/cache.rb
steppy-1.0.4 lib/steppy/cache.rb
steppy-1.0.3 lib/steppy/cache.rb
steppy-1.0.2 lib/steppy/cache.rb
steppy-1.0.1 lib/steppy/cache.rb
steppy-1.0.0 lib/steppy/cache.rb
steppy-0.8.0 lib/steppy/cache.rb
steppy-0.7.0 lib/steppy/cache.rb