Class: PartnerService::MultilingualCacheService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/partner_service/multilingual_cache_service.rb

Constant Summary collapse

CACHE_NAMESPACE =

Cache namespace for multilingual data

'restaurant_multilingual_names'
CACHE_EXPIRATION =

Cache expiration time (24 hours)

24.hours
AVAILABLE_LOCALES =

Available locales for name attributes

MyLocaleManager.available_locales.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_ids) ⇒ MultilingualCacheService

Returns a new instance of MultilingualCacheService.

Parameters:

  • restaurant_ids (Array<Integer>)

    Array of restaurant IDs to cache data for



45
46
47
# File 'app/services/partner_service/multilingual_cache_service.rb', line 45

def initialize(restaurant_ids)
  @restaurant_ids = Array(restaurant_ids).compact.uniq
end

Class Method Details

.clear_all_multilingual_cachevoid

This method returns an undefined value.

Clear all multilingual cache entries

This is used when package names are updated to ensure all possible cache combinations are invalidated (handles cross-restaurant scenarios)



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/partner_service/multilingual_cache_service.rb', line 81

def self.clear_all_multilingual_cache
  # Get all cache keys that start with our namespace
  if Rails.cache.respond_to?(:delete_matched)
    # For Redis or other caches that support pattern deletion
    Rails.cache.delete_matched("#{CACHE_NAMESPACE}:*")
  else
    # Fallback for memory cache or other cache stores
    # This is less efficient but works for all cache types
    Rails.cache.clear
  end

  Rails.logger.info 'Cleared all multilingual cache entries'
end

.extract_restaurant_ids_from_model(model_instance) ⇒ Array<Integer>

Extract restaurant IDs from model instance for cache invalidation

Parameters:

  • model_instance (ActiveRecord::Base)

    model instance

Returns:

  • (Array<Integer>)

    restaurant IDs



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/services/partner_service/multilingual_cache_service.rb', line 259

def self.extract_restaurant_ids_from_model(model_instance)
  case model_instance
  when HhPackage::Package::Base
    # Get restaurant IDs from restaurant_packages
    model_instance.restaurant_packages.pluck(:restaurant_id)
  when AddOns::AddOn
    # Get restaurant IDs from restaurant_add_ons
    model_instance.restaurant_add_ons.pluck(:restaurant_id)
  when PackageSpecialMenu
    # Get restaurant IDs through package_attr -> package -> restaurant_packages
    model_instance.package_attr&.package&.restaurant_packages&.pluck(:restaurant_id) || []
  when HhPackage::RestaurantPackage
    [model_instance.restaurant_id]
  when AddOns::Restaurant
    [model_instance.restaurant_id]
  else
    []
  end
end

.invalidate_cache(restaurant_ids) ⇒ void

This method returns an undefined value.

Invalidate cache for specific restaurants

Parameters:

  • restaurant_ids (Array<Integer>)

    Restaurant IDs to invalidate



67
68
69
70
71
72
73
# File 'app/services/partner_service/multilingual_cache_service.rb', line 67

def self.invalidate_cache(restaurant_ids)
  restaurant_ids = Array(restaurant_ids).compact.uniq
  return if restaurant_ids.empty?

  cache_key = new(restaurant_ids).send(:build_cache_key, restaurant_ids)
  Rails.cache.delete(cache_key)
end

.invalidate_cache_for_model(model_instance) ⇒ void

This method returns an undefined value.

Invalidate cache when packages, add-ons, or special menus are updated

Parameters:

  • model_instance (ActiveRecord::Base)

    The updated model instance



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/services/partner_service/multilingual_cache_service.rb', line 99

def self.invalidate_cache_for_model(model_instance)
  # For package name changes, we need to clear all multilingual cache
  # because packages can appear in cross-restaurant scenarios
  case model_instance
  when HhPackage::Package::Base
    # Clear all multilingual cache to handle cross-restaurant package references
    clear_all_multilingual_cache
    Rails.logger.info "Cleared all multilingual cache due to package #{model_instance.class.name} #{model_instance.id} update"
  else
    # For other models, use targeted invalidation
    restaurant_ids = extract_restaurant_ids_from_model(model_instance)
    invalidate_cache(restaurant_ids) if restaurant_ids.present?
  end
end

Instance Method Details

#fetch_multilingual_dataHash

Fetch multilingual data from cache or database

Returns:

  • (Hash)

    multilingual data structure



52
# File 'app/services/partner_service/multilingual_cache_service.rb', line 52

span_method :fetch_multilingual_data