Update cacher to use monitor mixin
This commit is contained in:
parent
687189f7b2
commit
63e253b506
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user