Update cacher to use monitor mixin

This commit is contained in:
Chris Roberts 2021-11-17 14:31:00 -08:00 committed by Paul Hinze
parent 687189f7b2
commit 63e253b506
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0

View File

@ -1,40 +1,43 @@
require "monitor"
module VagrantPlugins
module CommandServe
module Util
class Cacher
include MonitorMixin
class Entry
include MonitorMixin
attr_reader :value
attr_reader :m
def initialize(value)
super()
@value = value
@m = Mutex.new
end
end
def initialize
@m = Mutex.new
super()
@registry = {}
end
def registered?(key)
@registry.key?(key)
synchronize { @registry.key?(key) }
end
def []=(key, value)
entry = Entry.new(value)
@m.synchronize { @registry[key] = entry }
synchronize { @registry[key] = entry }
end
def [](key)
@m.synchronize { @registry[key]&.value }
synchronize { @registry[key]&.value }
end
def delete(key)
entry = @registry[key]
return if entry.nil?
entry.m.synchronize do
entry.synchronize do
value = @registry[key].value
@registry.delete(key)
value
@ -52,7 +55,7 @@ module VagrantPlugins
"No value cached with key `#{key}'"
end
entry.m.synchronize do
entry.synchronize do
yield entry.value
end
end