Module: Inventory::InvCheckerHungryHubService::Helper

Extended by:
ActiveSupport::Concern
Included in:
Inventory::InvCheckerHungryHubService, DataStore
Defined in:
app/services/inventory/inv_checker_hungry_hub_service/helper.rb

Instance Method Summary collapse

Instance Method Details

#cache_key_hash_by_restaurantObject



26
27
28
29
30
31
32
33
34
35
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 26

def cache_key_hash_by_restaurant
  {
    'Inventory::Constants::Restaurants' => Inventory::Constants::Restaurants.constants.select { |const|
      const.to_s.include?('CACHE_KEY_HASH')
    },
    'Inventory::Constants::Reservations' => Inventory::Constants::Reservations.constants.select { |const|
      const.to_s.include?('CACHE_KEY_HASH') && !const.to_s.include?('RESTAURANT_PACKAGE')
    }
  }
end

#cache_key_hash_by_restaurant_packageObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 12

def cache_key_hash_by_restaurant_package
  {
    'Inventory::Constants::Restaurants' => Inventory::Constants::Restaurants.constants.select { |const|
     const.to_s.include?('CACHE_KEY_HASH') && const.to_s.include?('RESTAURANT_PACKAGE')
    },
    'Inventory::Constants::RestaurantPackages' => Inventory::Constants::RestaurantPackages.constants.select { |const|
      const.to_s.include?('CACHE_KEY_HASH')
    },
    'Inventory::Constants::Reservations' => Inventory::Constants::Reservations.constants.select { |const|
      const.to_s.include?('CACHE_KEY_HASH') && const.to_s.include?('RESTAURANT_PACKAGE')
    }
  }
end

#clear_restaurant_cache_by_dates(dates, version = cache_key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 55

def clear_restaurant_cache_by_dates(dates, version = cache_key)
  date_keys = []
  dates.each do |date|
    cache_key_hash_by_restaurant.each do |const_namespace, const_values|
      const_values.each do |const|
        date_keys << (const_namespace.constantize.const_get(const) % { namespace: "#{namespace_raw}:#{version}", date: date })
      end
    end
  end

  inv_redis_pool.with do |redis|
    redis.pipelined do
      date_keys.each do |date_key|
        redis.unlink("#{date_key}:by_hmset")
      end
    end
  end
end

#clear_restaurant_package_cache(restaurant_package, date, version = cache_key) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 37

def clear_restaurant_package_cache(restaurant_package, date, version = cache_key)
  hash_keys = []
  cache_key_hash_by_restaurant_package.each do |const_namespace, const_values|
    const_values.each do |const|
      hash_keys.push(const_namespace.constantize.const_get(const) % { namespace: "#{namespace_raw}:#{version}", slug: restaurant_package.slug, date: date })
    end
  end

  inv_redis_pool.with do |redis|
    redis.pipelined do
      # delete the hash keys
      hash_keys.each do |hash_key|
        redis.unlink("#{hash_key}:by_hmset")
      end
    end
  end
end

#fetch(key_param, default_value, scope = nil) ⇒ Object

Parameters:

  • key_param (Array)

    to be a cache key suffix

  • default_value (Any)

    Default value of the yield when something went wrong

  • scope (Symbol) (defaults to: nil)

    :restaurant or Package's Slug



131
132
133
134
135
136
137
138
139
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 131

def fetch(key_param, default_value, scope = nil)
  namespace = scope.presence || :restaurant_scope
  key = key_param.join('__')

  @local_cache["#{namespace_default}__#{key}"] ||= yield
rescue StandardError => e
  APMErrorHandler.report e
  default_value
end

#fetch_from_local_cache(local_cache_namespace:, local_cache_key:, default_value: nil, &block) ⇒ Object

wrap #fetch_or_store method



121
122
123
124
125
126
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 121

def fetch_from_local_cache(local_cache_namespace:, local_cache_key:, default_value: nil, &block)
  @local_cache["#{cache_key}:#{local_cache_namespace}:#{Array(local_cache_key).join('__')}"] ||= yield
rescue StandardError => error
  APMErrorHandler.report error
  default_value
end

#inv_redis_poolObject



141
142
143
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 141

def inv_redis_pool
  $inv_redis
end

#inv_redis_pool_replicaObject



145
146
147
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 145

def inv_redis_pool_replica
  $inv_redis_ro
end

#inventories_start_times(invs) ⇒ Object



74
75
76
77
78
79
80
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 74

def inventories_start_times(invs)
  if restaurant.start_time_interval.include?('30min')
    invs.map { |inv| inv['start_time_format'] }.select { |time| time.end_with?('00', '30') }
  else
    invs.map { |inv| inv['start_time_format'] }
  end
end

#invert_slugs_by_start_times(data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 82

def invert_slugs_by_start_times(data)
  inverted_slugs_by_start_times_data = {}
  data.each do |start_time, slugs|
    slugs.each do |slug|
      inverted_slugs_by_start_times_data[slug] ||= Set.new
      inverted_slugs_by_start_times_data[slug].add start_time
    end
  end
  inverted_slugs_by_start_times_data
end

#namespace_defaultObject

use the active cache key



116
117
118
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 116

def namespace_default
  "#{namespace_raw}:#{cache_key}"
end

#namespace_rawString

top level namespace @example:

InvChecker__{restaurant_id}:dine_in
InvChecker__{restaurant_id}:take_away

Returns:

  • (String)

    the namespace for the inventory checker



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/services/inventory/inv_checker_hungry_hub_service/helper.rb', line 98

def namespace_raw
  # `{}` is required in redis cluster to make sure the keys is in the same node
  key = "InvChecker__{#{restaurant_id}}"

  if for_dine_in && for_delivery
    raise 'for_dine_in and for_delivery cannot be true at the same time'
  elsif for_dine_in
    key += ':dine_in'
  elsif for_delivery
    key += ':take_away'
  else
    raise NotImplementedError, 'for_dine_in or for_delivery must be true'
  end

  key
end