A platform for community discussion. Free, open, simple.
PERF: Actually cache plugin manifests and asset lookups (#43178)
David Taylor17780093e345
diff --git a/lib/plugin/js_manager.rb b/lib/plugin/js_manager.rbindex 0e3b3b1b93c..0ab91a992df 100644--- a/lib/plugin/js_manager.rb+++ b/lib/plugin/js_manager.rb@@ -2,7 +2,10 @@module Pluginclass JsManager- @cache = {}+ class Cache < ActiveSupport::CurrentAttributes+ # Cache which persists for the duration of a request+ attribute :request_cache+ enddef self.optional_plugin_stub"data:text/javascript,/* autogenerated missing optional plugin stub */const m=new Proxy({},{get:()=>null});export default new Proxy({},{get:()=>m});"@@ -17,25 +20,25 @@ module Pluginenddef self.js_asset_exists?(plugin_directory_name)- maybe_cache("js_asset_exists_#{plugin_directory_name}") do+ get_set_cache("js_asset_exists_#{plugin_directory_name}") dohas_source_files_in_dir(plugin_directory_name, "assets/javascripts")endenddef self.admin_js_asset_exists?(plugin_directory_name)- maybe_cache("admin_js_asset_exists_#{plugin_directory_name}") do+ get_set_cache("admin_js_asset_exists_#{plugin_directory_name}") dohas_source_files_in_dir(plugin_directory_name, "admin/assets/javascripts")endenddef self.test_js_asset_exists?(plugin_directory_name)- maybe_cache("test_js_asset_exists_#{plugin_directory_name}") do+ get_set_cache("test_js_asset_exists_#{plugin_directory_name}") dohas_source_files_in_dir(plugin_directory_name, "test/javascripts")endenddef self.read_manifest(plugin_directory_name)- maybe_cache("manifest_#{plugin_directory_name}") do+ get_set_cache("manifest_#{plugin_directory_name}") domanifest_path ="#{Rails.root.join("app/assets/generated/#{plugin_directory_name}/manifest.json")}"JSON.parse(File.read(manifest_path))@@ -236,11 +239,18 @@ module PluginSTDERR.puts messageend- private_class_method def self.maybe_cache(key, &blk)- if Rails.env.production?- @cache.fetch(key, &blk)+ private_class_method def self.get_set_cache(key, &blk)+ store =+ if Rails.env.development?+ Cache.request_cache ||= {}+ else+ @production_cache ||= {}+ end++ if store.key?(key)+ store[key]else- blk.call+ store[key] = blk.callendend