Skip to content

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.rb
index 0e3b3b1b93c..0ab91a992df 100644
--- a/lib/plugin/js_manager.rb
+++ b/lib/plugin/js_manager.rb
@@ -2,7 +2,10 @@
module Plugin
class JsManager
- @cache = {}
+ class Cache < ActiveSupport::CurrentAttributes
+ # Cache which persists for the duration of a request
+ attribute :request_cache
+ end
def 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 Plugin
end
def 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}") do
has_source_files_in_dir(plugin_directory_name, "assets/javascripts")
end
end
def 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}") do
has_source_files_in_dir(plugin_directory_name, "admin/assets/javascripts")
end
end
def 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}") do
has_source_files_in_dir(plugin_directory_name, "test/javascripts")
end
end
def self.read_manifest(plugin_directory_name)
- maybe_cache("manifest_#{plugin_directory_name}") do
+ get_set_cache("manifest_#{plugin_directory_name}") do
manifest_path =
"#{Rails.root.join("app/assets/generated/#{plugin_directory_name}/manifest.json")}"
JSON.parse(File.read(manifest_path))
@@ -236,11 +239,18 @@ module Plugin
STDERR.puts message
end
- 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.call
end
end