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.

Added tests and used cacke_key_with_version rather than cache_key

Damian Legawiec0faa86700b55
diff --git a/api/app/serializers/spree/v2/storefront/base_serializer.rb b/api/app/serializers/spree/v2/storefront/base_serializer.rb
index da6b641984..928b2a034c 100644
--- a/api/app/serializers/spree/v2/storefront/base_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/base_serializer.rb
@@ -11,7 +11,7 @@ module Spree
def self.record_cache_options(options, fieldset, include_list, params)
opts = options.dup
- params_cache_key = [params[:currency], params[:user]&.cache_key, params[:store]&.cache_key].join('-')
+ params_cache_key = [params[:currency], params[:user]&.cache_key_with_version, params[:store]&.cache_key_with_version].join('-')
opts[:namespace] += "-#{params_cache_key}"
diff --git a/api/spec/requests/spree/api/v2/caching_spec.rb b/api/spec/requests/spree/api/v2/caching_spec.rb
new file mode 100644
index 0000000000..e1cbaf306e
--- /dev/null
+++ b/api/spec/requests/spree/api/v2/caching_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe 'API v2 Caching spec', type: :request do
+ let!(:product) { create(:product, name: 'Some name') }
+ let(:cache_store) { Spree::V2::Storefront::ProductSerializer.cache_store_instance }
+ let(:cache_entry) { cache_store.read(product, namespace: cache_namespace) }
+
+ before do
+ ActionController::Base.perform_caching = true
+ end
+
+ after do
+ ActionController::Base.perform_caching = false
+ Rails.cache.clear
+ end
+
+ context 'auto expiration' do
+ let(:cache_namespace) { 'jsonapi-serializer---spree/stores/new' }
+
+ it 'auto expire cache after record being updated' do
+ get "/api/v2/storefront/products/#{product.id}"
+
+ expect(json_response['data']).to have_attribute('name').with_value('Some name')
+
+ expect(cache_entry).not_to be_nil
+ expect(cache_entry[:attributes].as_json).to eq(json_response[:data][:attributes])
+
+ product.update(name: 'Something else')
+ expect(cache_store.read(product, namespace: cache_namespace)).to be_nil
+
+ get "/api/v2/storefront/products/#{product.id}"
+ expect(cache_store.read(product, namespace: cache_namespace)).not_to be_nil
+ expect(json_response['data']).to have_attribute('name').with_value('Something else')
+ end
+ end
+
+ context 'currency and user' do
+ include_context 'API v2 tokens'
+
+ let!(:user) { create(:user) }
+ let!(:store) { create(:store, supported_currencies: 'USD,EUR') }
+ let(:currency) { 'EUR' }
+ let(:cache_namespace) { "jsonapi-serializer-EUR-#{user.cache_key_with_version}-#{store.cache_key_with_version}" }
+
+ it 'includes currency and signed user in the cache key' do
+ get "/api/v2/storefront/products/#{product.id}?currency=#{currency}", headers: headers_bearer
+
+ expect(cache_entry).not_to be_nil
+ end
+ end
+end