diff --git a/app/routines/warm_up_cache.rb b/app/routines/warm_up_cache.rb new file mode 100644 index 00000000..7b274cc8 --- /dev/null +++ b/app/routines/warm_up_cache.rb @@ -0,0 +1,17 @@ +class WarmUpCache + lev_routine transaction: :no_transaction + + protected + + def exec + Exercise.chainable_latest.find_in_batches(batch_size: 100) do |exercises| + Api::V1::Exercises::SearchRepresenter.new(items: exercises).to_hash( + user_options: { can_view_solutions: true } + ) + end + + VocabTerm.chainable_latest.find_in_batches(batch_size: 100) do |vocab_terms| + Api::V1::Vocabs::TermSearchRepresenter.new(items: vocab_terms).to_hash + end + end +end diff --git a/lib/tasks/cron/day.rake b/lib/tasks/cron/day.rake index 889bdbdf..c13ead86 100644 --- a/lib/tasks/cron/day.rake +++ b/lib/tasks/cron/day.rake @@ -5,6 +5,9 @@ namespace :cron do Rails.logger.info 'UpdateSlugs.call' OpenStax::RescueFrom.this { UpdateSlugs.call } + Rails.logger.info 'WarmUpCache.call' + OpenStax::RescueFrom.this { WarmUpCache.call } + Rails.logger.debug 'Finished daily cron' end end diff --git a/spec/lib/tasks/cron/day.rake_spec.rb b/spec/lib/tasks/cron/day.rake_spec.rb index c5bdbadc..8f62ad8b 100644 --- a/spec/lib/tasks/cron/day.rake_spec.rb +++ b/spec/lib/tasks/cron/day.rake_spec.rb @@ -9,8 +9,9 @@ before { Rake::Task['cron:day'].reenable } - it 'calls the UpdateSlugs lev routine' do + it 'calls the UpdateSlugs and WarmUpCache lev routines' do expect(UpdateSlugs).to receive(:call) + expect(WarmUpCache).to receive(:call) Rake.application.invoke_task 'cron:day' end diff --git a/spec/routines/warm_up_cache_spec.rb b/spec/routines/warm_up_cache_spec.rb new file mode 100644 index 00000000..e088b007 --- /dev/null +++ b/spec/routines/warm_up_cache_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe WarmUpCache, type: :routine do + before do + 5.times do + FactoryBot.create :exercise + FactoryBot.create :exercise, :published + FactoryBot.create :vocab_term, vocab_distractors_count: 1 + FactoryBot.create :vocab_term, :published, vocab_distractors_count: 1 + end + end + + it 'caches all exercises and vocab terms' do + expect { described_class.call }.to( + change { Rails.cache.instance_variable_get(:@data).count }.by(60) + ) + + expect { described_class.call }.not_to( + change { Rails.cache.instance_variable_get(:@data).count } + ) + end +end