1
0
Fork 0

Add custom-wizard-specific caching system

Initially for use with registered custom fields
Dieser Commit ist enthalten in:
Angus McLeod 2020-12-04 18:05:56 +11:00
Ursprung 8a2a143ea8
Commit d453d97df3
11 geänderte Dateien mit 118 neuen und 30 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,5 @@
{ {
"result": { "result": {
"covered_percent": 89.25 "covered_percent": 89.41
} }
} }

Datei anzeigen

@ -4,7 +4,7 @@ module CustomWizardCustomFieldPreloader
@cw_klass = objects.first.class.name.underscore @cw_klass = objects.first.class.name.underscore
if cw_fields.any? if cw_fields.any?
cw_fields.each do |field| cw_fields.each do |field|
fields << field.name fields << field[:name]
end end
end end
end end

Datei anzeigen

@ -8,9 +8,9 @@ module CustomWizardCustomFieldSerializer
if cw_fields.any? if cw_fields.any?
cw_fields.each do |field| cw_fields.each do |field|
if @cw_klass == "topic_view" if @cw_klass == "topic_view"
hash[field.name.to_sym] = object.topic.custom_fields["#{field.name}"] hash[field[:name].to_sym] = object.topic.custom_fields["#{field[:name]}"]
else else
hash[field.name.to_sym] = object.custom_fields["#{field.name}"] hash[field[:name].to_sym] = object.custom_fields["#{field[:name]}"]
end end
end end
end end

Datei anzeigen

@ -454,7 +454,7 @@ class CustomWizard::Action
user: user user: user
).perform ).perform
registered_fields = CustomWizard::CustomField.list registered_fields = CustomWizard::CustomField.cached_list
field_map.each do |field| field_map.each do |field|
keyArr = field[:key].split('.') keyArr = field[:key].split('.')
@ -468,9 +468,9 @@ class CustomWizard::Action
end end
registered = registered_fields.select { |f| f.name == name } registered = registered_fields.select { |f| f[:name] == name }
if registered.first.present? if registered.first.present?
klass = registered.first.klass klass = registered.first[:klass]
end end
if klass === 'topic' if klass === 'topic'

39
lib/custom_wizard/cache.rb Normale Datei
Datei anzeigen

@ -0,0 +1,39 @@
# 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

Datei anzeigen

@ -18,6 +18,7 @@ class ::CustomWizard::CustomField
post: ["post"] post: ["post"]
} }
TYPES ||= ["string", "boolean", "integer", "json"] TYPES ||= ["string", "boolean", "integer", "json"]
LIST_CACHE_KEY ||= 'custom_field_list'
def self.serializers def self.serializers
CLASSES.values.flatten.uniq CLASSES.values.flatten.uniq
@ -120,22 +121,29 @@ class ::CustomWizard::CustomField
errors.blank? errors.blank?
end end
def self.reset
@list = nil
@any = nil
end
def self.list def self.list
@list ||= PluginStoreRow.where(plugin_name: NAMESPACE) PluginStoreRow.where(plugin_name: NAMESPACE).map do |record|
.map { |record| create_from_store(record) } create_from_store(record)
end
end end
def self.list_by(attr, value) def self.cached_list
self.list.select do |cf| ::CustomWizard::Cache.wrap(LIST_CACHE_KEY) do
PluginStoreRow.where(plugin_name: NAMESPACE).map do |record|
create_from_store(record).as_json.with_indifferent_access
end
end
end
def self.list_by(attr, value, cached: true)
attr = attr.to_sym
fields = cached ? cached_list : list
fields.select do |cf|
if attr == :serializers if attr == :serializers
cf.send(attr).include?(value) cf[attr].include?(value)
else else
cf.send(attr) == value cf[attr] == value
end end
end end
end end
@ -196,17 +204,13 @@ class ::CustomWizard::CustomField
end end
def self.invalidate_cache def self.invalidate_cache
self.reset CustomWizard::Cache.new(LIST_CACHE_KEY).delete
Discourse.clear_readonly! Discourse.clear_readonly!
Discourse.request_refresh! Discourse.request_refresh!
end end
def self.any? def self.any?
if @any.nil? cached_list.length > 0
@any = PluginStoreRow.where(plugin_name: NAMESPACE).exists?
else
@any
end
end end
def self.enabled? def self.enabled?

Datei anzeigen

@ -1,6 +1,10 @@
# frozen_string_literal: true
module ::CustomWizard module ::CustomWizard
PLUGIN_NAME ||= 'custom_wizard'
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
engine_name 'custom_wizard' engine_name PLUGIN_NAME
isolate_namespace CustomWizard isolate_namespace CustomWizard
end end
end end

Datei anzeigen

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CustomWizard::Template class CustomWizard::Template
include HasErrors include HasErrors
@ -19,7 +21,7 @@ class CustomWizard::Template
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
schedule_save_jobs unless opts[:skip_jobs] schedule_save_jobs unless opts[:skip_jobs]
PluginStore.set('custom_wizard', @data[:id], @data) PluginStore.set(CustomWizard::PLUGIN_NAME, @data[:id], @data)
end end
@data[:id] @data[:id]
@ -30,7 +32,7 @@ class CustomWizard::Template
end end
def self.find(wizard_id) def self.find(wizard_id)
PluginStore.get('custom_wizard', wizard_id) PluginStore.get(CustomWizard::PLUGIN_NAME, wizard_id)
end end
def self.remove(wizard_id) def self.remove(wizard_id)
@ -39,7 +41,7 @@ class CustomWizard::Template
return false if !wizard return false if !wizard
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
PluginStore.remove('custom_wizard', wizard.id) PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id)
if wizard.after_time if wizard.after_time
Jobs.cancel_scheduled_job(:set_after_time_wizard) Jobs.cancel_scheduled_job(:set_after_time_wizard)

Datei anzeigen

@ -55,6 +55,7 @@ after_initialize do
../lib/custom_wizard/action_result.rb ../lib/custom_wizard/action_result.rb
../lib/custom_wizard/action.rb ../lib/custom_wizard/action.rb
../lib/custom_wizard/builder.rb ../lib/custom_wizard/builder.rb
../lib/custom_wizard/cache.rb
../lib/custom_wizard/custom_field.rb ../lib/custom_wizard/custom_field.rb
../lib/custom_wizard/field.rb ../lib/custom_wizard/field.rb
../lib/custom_wizard/mapper.rb ../lib/custom_wizard/mapper.rb
@ -178,7 +179,7 @@ after_initialize do
klass.to_s klass.to_s
.classify .classify
.constantize .constantize
.register_custom_field_type(field.name, field.type.to_sym) .register_custom_field_type(field[:name], field[:type].to_sym)
end end
end end
end end

Datei anzeigen

@ -0,0 +1,35 @@
# frozen_string_literal: true
require_relative '../../plugin_helper.rb'
describe CustomWizard::Cache do
it "writes and reads values to the cache" do
CustomWizard::Cache.new('list').write([1,2,3])
expect(CustomWizard::Cache.new('list').read).to eq([1,2,3])
end
it "deletes values from the cache" do
CustomWizard::Cache.new('list').delete
expect(CustomWizard::Cache.new('list').read).to eq(nil)
end
describe "#wrap" do
before do
@raw = [1,2,3]
end
def list
CustomWizard::Cache.wrap('list') { @raw }
end
it "returns value from passed block" do
expect(list).to eq([1,2,3])
end
it "returns cached value" do
cached = list
@raw = [3,2,1]
expect(list).to eq(cached)
end
end
end

Datei anzeigen

@ -10,6 +10,10 @@ describe CustomWizard::CustomField do
).read) ).read)
} }
before do
CustomWizard::CustomField.invalidate_cache
end
it "saves custom field records" do it "saves custom field records" do
custom_field_json['custom_fields'].each do |field_json| custom_field_json['custom_fields'].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json) custom_field = CustomWizard::CustomField.new(nil, field_json)
@ -184,7 +188,6 @@ describe CustomWizard::CustomField do
end end
it "is not enabled if there are no custom fields" do it "is not enabled if there are no custom fields" do
CustomWizard::CustomField.invalidate_cache
expect(CustomWizard::CustomField.enabled?).to eq(false) expect(CustomWizard::CustomField.enabled?).to eq(false)
end end
end end