0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-19 23:31:11 +02:00
discourse-custom-wizard/lib/custom_wizard/cache.rb

40 Zeilen
601 B
Ruby

# frozen_string_literal: true
class ::CustomWizard::Cache
def initialize(key)
@key = "#{CustomWizard::PLUGIN_NAME}_#{key}"
end
2021-03-11 07:30:15 +01:00
def read
cache.read(@key)
end
2021-03-11 07:30:15 +01:00
def write(data)
synchronize { cache.write(@key, data) }
end
2021-03-11 07:30:15 +01:00
def delete
synchronize { cache.delete(@key) }
end
2021-03-11 07:30:15 +01:00
def synchronize
DistributedMutex.synchronize(@key) { yield }
end
2021-03-11 07:30:15 +01:00
def cache
@cache ||= Discourse.cache
end
2021-03-11 07:30:15 +01:00
def self.wrap(key, &block)
c = new(key)
2021-03-11 07:30:15 +01:00
if cached = c.read
cached
else
result = block.call()
c.write(result)
result
end
end
2021-03-11 07:30:15 +01:00
end