Cache Not Returning Results in Loop When Changed

65 Views Asked by At

In order to reduce memory consumption on an API call (using ruby-grape) I'm attempting to calculate data in a background job (Delayed Job, eventually Sidekiq), populate a Redis cache and return the results.

Here is the DelayedJob that populates the cache

class LotService
  def self.cache_recently_removed_lot_ids
    lot_ids = Lot.ids_removed_from_syndication_in_last(60)
    lot_ids.concat Lot.ids_hidden_in_last(60)
    lot_ids.concat Lot.ids_unpublished_in_last(60)
    Rails.cache.write(:recently_removed_lot_ids, lot_ids, expires_at: DateTime.current + 1.hour)
  end
end

Here is the API endpoint. Within the 10.times iteration the Cache results never return. If I make a subsequent call to the API endpoint the results are there.

lot_ids = []
10.times do |iteration|
  lot_ids = Rails.cache.read(:recently_removed_lot_ids)
  break if lot_ids.present?

  # Run on first iteration and if job isn't already queued
  LotsRecentlyRemovedCacheJob.perform_later if
    !LotsRecentlyRemovedCacheJob.already_queued? && iteration.zero?
  sleep 2
end
content_type 'application/json'
lot_ids.present? ? lot_ids.sort! : []

Why would the iterative calls to Rails.cache.read not return a result once the cache had been populated by the background job?

0

There are 0 best solutions below