[deploy] Optimization:Store Podcast Ep Data in Redis, Rather than Sending as Sidekiq Argument (#10331)
This commit is contained in:
parent
b67a750319
commit
13fb5c4278
5 changed files with 30 additions and 12 deletions
|
|
@ -12,7 +12,9 @@ module Podcasts
|
|||
if episode
|
||||
try_update_media_url(episode: episode, item_data: item_data, force_update: force_update)
|
||||
else
|
||||
PodcastEpisodes::CreateWorker.perform_async(podcast.id, item_data.to_h)
|
||||
episode_cache_key = cache_key(item_data)
|
||||
cache_episode_data(episode_cache_key, item_data)
|
||||
PodcastEpisodes::CreateWorker.perform_async(podcast.id, episode_cache_key)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -36,5 +38,13 @@ module Podcasts
|
|||
rescue ArgumentError, NoMethodError => e
|
||||
Rails.logger.error("not a valid date: #{e}")
|
||||
end
|
||||
|
||||
def cache_episode_data(episode_cache_key, item_data)
|
||||
Rails.cache.write(episode_cache_key, item_data.to_h)
|
||||
end
|
||||
|
||||
def cache_key(item_data)
|
||||
Digest::SHA1.hexdigest(item_data.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,7 +4,10 @@ module PodcastEpisodes
|
|||
|
||||
sidekiq_options queue: :high_priority
|
||||
|
||||
def perform(podcast_id, item)
|
||||
def perform(podcast_id, episode_cache_key)
|
||||
item = Rails.cache.read(episode_cache_key)
|
||||
return unless item
|
||||
|
||||
Podcasts::CreateEpisode.call(podcast_id, item.with_indifferent_access)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -96,7 +96,10 @@ RSpec.describe Podcasts::Feed, type: :service, vcr: vcr_option do
|
|||
end
|
||||
|
||||
context "when creating" do
|
||||
let(:cache_store) { ActiveSupport::Cache.lookup_store(:redis_cache_store) }
|
||||
|
||||
before do
|
||||
allow(Rails).to receive(:cache).and_return(cache_store)
|
||||
stub_request(:head, "https://traffic.libsyn.com/sedaily/AnalyseAsia.mp3").to_return(status: 200)
|
||||
stub_request(:head, "https://traffic.libsyn.com/sedaily/IFTTT.mp3").to_return(status: 200)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -82,9 +82,10 @@ RSpec.describe Podcasts::GetEpisode, type: :service do
|
|||
end
|
||||
|
||||
it "enqueues a worker to create an episode when it doesn't exist" do
|
||||
cache_key = Digest::SHA1.hexdigest(item.to_s)
|
||||
allow(podcast).to receive(:existing_episode).and_return(nil)
|
||||
|
||||
sidekiq_assert_enqueued_with(job: PodcastEpisodes::CreateWorker, args: [podcast.id, item.to_h]) do
|
||||
sidekiq_assert_enqueued_with(job: PodcastEpisodes::CreateWorker, args: [podcast.id, cache_key]) do
|
||||
described_class.new(podcast).call(item: item)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,30 +1,31 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe PodcastEpisodes::CreateWorker, type: :worker do
|
||||
include_examples "#enqueues_on_correct_queue", "high_priority", [123, {}]
|
||||
include_examples "#enqueues_on_correct_queue", "high_priority", [123, "cache_key"]
|
||||
|
||||
describe "#perform" do
|
||||
let(:worker) { subject }
|
||||
|
||||
let(:podcast_id) { 781 }
|
||||
let(:item) { { foo: "bar" } }
|
||||
let(:cache_key) { "ep_cache_key" }
|
||||
|
||||
before do
|
||||
allow(Podcasts::CreateEpisode).to receive(:call)
|
||||
end
|
||||
|
||||
it "creates a podcast episode" do
|
||||
worker.perform(podcast_id, item)
|
||||
allow(Rails.cache).to receive(:read).and_return({})
|
||||
worker.perform(podcast_id, cache_key)
|
||||
|
||||
expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, item).once
|
||||
expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, {}).once
|
||||
expect(Rails.cache).to have_received(:read).with(cache_key).once
|
||||
end
|
||||
|
||||
context "when item has string keys" do
|
||||
it "creates a podcast episode regardless of whether item has string or symbol keys" do
|
||||
worker.perform(podcast_id, item.stringify_keys)
|
||||
it "does not create a podcast episode if item is not found" do
|
||||
allow(Rails.cache).to receive(:read)
|
||||
worker.perform(podcast_id, cache_key)
|
||||
|
||||
expect(Podcasts::CreateEpisode).to have_received(:call).with(podcast_id, item).once
|
||||
end
|
||||
expect(Podcasts::CreateEpisode).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue