Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve & simpluft collection caching in API (#12106)

Damian Legawiecc239a65066c1
diff --git a/api/app/controllers/concerns/spree/api/v2/caching.rb b/api/app/controllers/concerns/spree/api/v2/caching.rb
index efc2afa6d0..4036a26004 100644
--- a/api/app/controllers/concerns/spree/api/v2/caching.rb
+++ b/api/app/controllers/concerns/spree/api/v2/caching.rb
@@ -5,22 +5,22 @@ module Spree
extend ActiveSupport::Concern
def collection_cache_key(collection)
- ids_and_timestamps = collection.unscope(:includes).unscope(:order).pluck(:id, :updated_at)
+ # Calling reselect('*') is a workaround for the joined translations table
+ collection_cache_key = collection.unscope(:includes).unscope(:order).unscope(:group).reselect('*').cache_key_with_version
- ids = ids_and_timestamps.map(&:first)
- max_updated_at = ids_and_timestamps.map(&:last).max
+ params.delete(:page) if params[:page] == 1
cache_key_parts = [
- self.class.to_s,
- max_updated_at,
- ids,
+ collection_cache_key,
resource_includes,
sparse_fields,
- serializer_params,
+ serializer_params_cache_key,
params[:sort]&.strip,
params[:page]&.to_s&.strip,
params[:per_page]&.to_s&.strip,
- ].flatten.join('-')
+ ]
+ cache_key_parts += additional_cache_key_parts if defined?(additional_cache_key_parts)
+ cache_key_parts = cache_key_parts.flatten.join('-')
Digest::MD5.hexdigest(cache_key_parts)
end
@@ -31,6 +31,12 @@ module Spree
expires_in: Spree::Api::Config[:api_v2_collection_cache_ttl],
}
end
+
+ def serializer_params_cache_key
+ serializer_params.values.map do |param|
+ param.try(:cache_key) || param.try(:flatten).try(:join, '-') || param.try(:to_s)
+ end.compact.join('-')
+ end
end
end
end