Class: SpreeCmCommissioner::ApiCaches::Invalidate
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::ApiCaches::Invalidate
- Includes:
- Spree::ServiceModule::Base
- Defined in:
- app/services/spree_cm_commissioner/api_caches/invalidate.rb
Constant Summary collapse
- SUPPORTED_MODELS =
{ 'SpreeCmCommissioner::HomepageSection' => SpreeCmCommissioner::HomepageSection, 'SpreeCmCommissioner::HomepageBackground' => SpreeCmCommissioner::HomepageBackground, 'Spree::Menu' => Spree::Menu, 'Spree::Taxon' => Spree::Taxon, 'Spree::Product' => Spree::Product }.freeze
Instance Method Summary collapse
-
#call(model:, id: nil) ⇒ Object
eg.
-
#invalidate_all_records(model_class:) ⇒ Object
eg.
-
#invalidate_single_record(model_class:, id:) ⇒ Object
eg.
- #invalidate_uris ⇒ Object
- #single_api_pattern ⇒ Object
Instance Method Details
#call(model:, id: nil) ⇒ Object
eg. ‘Spree::Product’, 123 or ‘Spree::Product’, nil
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 15 def call(model:, id: nil) model_class = SUPPORTED_MODELS[model] return failure(nil, 'Model is not supported') if model_class.nil? if id.present? invalidate_single_record(model_class: model_class, id: id) else invalidate_all_records(model_class: model_class) end end |
#invalidate_all_records(model_class:) ⇒ Object
eg. ‘Spree::Product’ => [‘/api/v2/tenant/products/*’, ‘/api/v2/storefront/products/*’]
49 50 51 52 53 54 55 56 57 |
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 49 def invalidate_all_records(model_class:) model_class.find_each do |record| refresh_record_cache_key(record) end enqueue_invalidate(invalidate_uris[model_class.name]) success(api_patterns: invalidate_uris[model_class.name]) end |
#invalidate_single_record(model_class:, id:) ⇒ Object
eg. ‘Spree::Product’, 123 => [‘/api/v2/tenant/products/123’, ‘/api/v2/tenant/products/slug-of-product’, …]
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 28 def invalidate_single_record(model_class:, id:) return failure(nil, "#{model_class.name} ID is required") if id.blank? record = model_class.find_by(id: id) return failure(nil, "#{model_class.name} #{id} record not found") if record.nil? refresh_record_cache_key(record) api_patterns = single_api_pattern[record.class.name].map do |pattern| pattern.gsub(/:(\w+)/) do key = Regexp.last_match(1) record.public_send(key).to_s end end enqueue_invalidate(api_patterns) success(record: record, api_patterns: api_patterns) end |
#invalidate_uris ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 74 def invalidate_uris { 'SpreeCmCommissioner::HomepageSection' => homepage_section_patterns, 'SpreeCmCommissioner::HomepageBackground' => homepage_background_patterns, 'Spree::Menu' => ['/api/v2/storefront/menus/*'], 'Spree::Taxon' => ['/api/v2/storefront/taxons/*'], 'Spree::Product' => [ '/api/v2/tenant/products/*', '/api/v2/storefront/products/*' ] } end |
#single_api_pattern ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 59 def single_api_pattern { 'SpreeCmCommissioner::HomepageSection' => homepage_section_patterns, 'SpreeCmCommissioner::HomepageBackground' => homepage_background_patterns, 'Spree::Menu' => ['/api/v2/storefront/menus/:id'], 'Spree::Taxon' => ['/api/v2/storefront/taxons/:id'], 'Spree::Product' => [ '/api/v2/tenant/products/:id', '/api/v2/tenant/products/:slug', '/api/v2/storefront/products/:id', '/api/v2/storefront/products/:slug' ] } end |