Class: Inventory::InvCheckerHungryHubService::DataStore

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
app/services/inventory/inv_checker_hungry_hub_service/data_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#cache_key_hash_by_restaurant, #cache_key_hash_by_restaurant_package, #clear_restaurant_cache_by_dates, #clear_restaurant_package_cache, #fetch, #fetch_from_local_cache, #inv_redis_pool, #inv_redis_pool_replica, #inventories_start_times, #invert_slugs_by_start_times, #namespace_default, #namespace_raw

Constructor Details

#initialize(restaurant_id, time_zone) ⇒ DataStore

Returns a new instance of DataStore.



6
7
8
9
# File 'app/services/inventory/inv_checker_hungry_hub_service/data_store.rb', line 6

def initialize(restaurant_id, time_zone)
  @restaurant_id = restaurant_id
  @time_zone = time_zone
end

Instance Attribute Details

#restaurant_idObject (readonly)

Returns the value of attribute restaurant_id.



4
5
6
# File 'app/services/inventory/inv_checker_hungry_hub_service/data_store.rb', line 4

def restaurant_id
  @restaurant_id
end

#time_zoneObject (readonly)

Returns the value of attribute time_zone.



4
5
6
# File 'app/services/inventory/inv_checker_hungry_hub_service/data_store.rb', line 4

def time_zone
  @time_zone
end

Instance Method Details

#fetch_mapped_hmget(namespace, hash_keys, default_value: []) { ... } ⇒ Object

Returns hash.

Parameters:

  • namespace

    String

  • hash_keys

    Array

Yields:

  • Hash

Returns:

  • hash



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/inventory/inv_checker_hungry_hub_service/data_store.rb', line 15

def fetch_mapped_hmget(namespace, hash_keys, default_value: [])
  return {} if hash_keys.blank?

  namespace = "#{namespace}:by_hmset"

  data = inv_redis_pool_replica.with { |conn| conn.mapped_hmget(namespace, *hash_keys) }

  existing_data = {}
  missing_data = {}
  data.each do |key, value|
    if value.nil?
      missing_data[key] = default_value.dup
    else
      existing_data[key] = if value.is_a?(String)
                             parse_data(value)
                           else
                             value
                           end
    end
  end

  return existing_data if existing_data.keys.count == hash_keys.count

  if existing_data.present?
    missing_data.each do |k, _v|
      missing_data.delete(k) if existing_data.key?(k)
    end
  end

  # it means all expected keys already exist
  return existing_data if missing_data.blank?

  yield missing_data if block_given?

  result = missing_data.merge(existing_data)
  missing_data.each do |key, value|
    missing_data[key] = dump_data(value)
  end
  if missing_data.present?
    inv_redis_pool.with do |redis|
      redis.mapped_hmset(namespace, missing_data)
    end
  end
  result
end

#mapped_hmset(namespace, _hash_keys, date = nil) {|data| ... } ⇒ Object

Yields:

  • (data)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/inventory/inv_checker_hungry_hub_service/data_store.rb', line 61

def mapped_hmset(namespace, _hash_keys, date = nil)
  namespace = "#{namespace}:by_hmset"

  data = {}

  yield(data) if block_given?

  if data.present?
    data.each do |key, value|
      data[key] = dump_data(value)
    end

    inv_redis_pool.with do |redis|
      redis.mapped_hmset(namespace, data)

      # OPTIMIZATION: Set TTL for dated keys to prevent accumulation
      # Keys expire at midnight after the specified date
      if date.present?
        expire_at = (date.to_date + 1.day).end_of_day
        ttl_seconds = (expire_at - Time.current).to_i
        redis.expire(namespace, ttl_seconds) if ttl_seconds > 0
      end
    end
  end
  data
end