Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-29 12:20:27 +01:00
39 Zeilen
620 B
Ruby
39 Zeilen
620 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
class ::CustomWizard::Cache
|
||
|
def initialize(key)
|
||
|
@key = "#{CustomWizard::PLUGIN_NAME}_#{key}"
|
||
|
end
|
||
|
|
||
|
def read
|
||
|
cache.read(@key)
|
||
|
end
|
||
|
|
||
|
def write(data)
|
||
|
synchronize { cache.write(@key, data) }
|
||
|
end
|
||
|
|
||
|
def delete
|
||
|
synchronize { cache.delete(@key) }
|
||
|
end
|
||
|
|
||
|
def synchronize
|
||
|
DistributedMutex.synchronize(@key) { yield }
|
||
|
end
|
||
|
|
||
|
def cache
|
||
|
@cache ||= Discourse.cache
|
||
|
end
|
||
|
|
||
|
def self.wrap(key, &block)
|
||
|
c = new(key)
|
||
|
|
||
|
if cached = c.read
|
||
|
cached
|
||
|
else
|
||
|
result = block.call()
|
||
|
c.write(result)
|
||
|
result
|
||
|
end
|
||
|
end
|
||
|
end
|