Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Update the acts_as_customizable plugin custom field value caching mechanism to use cache keys

Dombi Attila93b7923f2856
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index dce3416b4e3..57fe0c07a7f 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -315,7 +315,6 @@ class WorkPackage < ApplicationRecord
def type_id=(tid)
self.type = nil
result = write_attribute(:type_id, tid)
- @custom_field_values = nil
result
end
@@ -452,6 +451,10 @@ class WorkPackage < ApplicationRecord
WorkPackage::AvailableCustomFields.for(work_package.project, work_package.type)
end
+ def custom_field_cache_key
+ [project_id, type_id]
+ end
+
protected
def <=>(other)
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index ca4fee51796..722a9f2889d 100644
--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -108,18 +108,31 @@ module Redmine
end
def custom_field_values
- @custom_field_values ||= available_custom_fields.flat_map do |custom_field|
- existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }
-
- if existing_cvs.empty?
- new_value = custom_values.build(customized: self,
- custom_field:,
- value: custom_field.default_value)
- existing_cvs.push new_value
+ custom_field_values_cache[custom_field_cache_key] ||=
+ available_custom_fields.flat_map do |custom_field|
+ existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }
+
+ if existing_cvs.empty?
+ new_value = custom_values.build(customized: self,
+ custom_field:,
+ value: custom_field.default_value)
+ existing_cvs.push new_value
+ end
+
+ existing_cvs
end
+ end
- existing_cvs
- end
+ # Returns the cache key for caching @custom_field_values_cache.
+ #
+ # In certain cases, the implementing models have a changing list of custom field values
+ # depending on certain attributes. By overriding this method, we can include the
+ # dependent attributes in the cache key, providing a more flexible key caching mechanism.
+ #
+ # i.e.: The work package custom field values are changing based on the project_id and type_id.
+ # The only way to keep the cache updated is to include those ids in the cache key.
+ def custom_field_cache_key
+ 1
end
##
@@ -193,7 +206,7 @@ module Redmine
end
def reset_custom_values_change_tracker
- @custom_field_values = nil
+ @custom_field_values_cache = nil
self.custom_value_destroyed = false
end
@@ -384,6 +397,10 @@ module Redmine
self.custom_value_destroyed = true
end
+ def custom_field_values_cache
+ @custom_field_values_cache ||= {}
+ end
+
module ClassMethods
def available_custom_fields(_model)
RequestStore.fetch(:"#{name.underscore}_custom_fields") do