summaryrefslogtreecommitdiff
path: root/lib/storage.rb
blob: 57b2548dfa2556b3c65048ebb02223a1ab57da89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Storage
  def initialize(storage: {})
    @storage = storage
    @mutex = Mutex.new
  end

  def key?(key)
    with_lock { @storage.key?(key) }
  end

  def [](key)
    with_lock { @storage[key] }
  end

  def []=(key, value)
    with_lock { @storage[key] = value }
  end

  def delete(key)
    with_lock { @storage.delete(key) }
  end

  private

  def with_lock
    @mutex.synchronize do
      yield
    end
  end
end