Skip to content

Commit 25344e2

Browse files
authored
Merge pull request #365 from openstax/cron_caching
Warm up the cache with the latest exercises daily
2 parents c48c878 + 53fc047 commit 25344e2

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

app/routines/warm_up_cache.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class WarmUpCache
2+
lev_routine transaction: :no_transaction
3+
4+
protected
5+
6+
def exec
7+
Exercise.chainable_latest.find_in_batches(batch_size: 100) do |exercises|
8+
Api::V1::Exercises::SearchRepresenter.new(items: exercises).to_hash(
9+
user_options: { can_view_solutions: true }
10+
)
11+
end
12+
13+
VocabTerm.chainable_latest.find_in_batches(batch_size: 100) do |vocab_terms|
14+
Api::V1::Vocabs::TermSearchRepresenter.new(items: vocab_terms).to_hash
15+
end
16+
end
17+
end

lib/tasks/cron/day.rake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace :cron do
55
Rails.logger.info 'UpdateSlugs.call'
66
OpenStax::RescueFrom.this { UpdateSlugs.call }
77

8+
Rails.logger.info 'WarmUpCache.call'
9+
OpenStax::RescueFrom.this { WarmUpCache.call }
10+
811
Rails.logger.debug 'Finished daily cron'
912
end
1013
end

spec/lib/tasks/cron/day.rake_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
before { Rake::Task['cron:day'].reenable }
1111

12-
it 'calls the UpdateSlugs lev routine' do
12+
it 'calls the UpdateSlugs and WarmUpCache lev routines' do
1313
expect(UpdateSlugs).to receive(:call)
14+
expect(WarmUpCache).to receive(:call)
1415

1516
Rake.application.invoke_task 'cron:day'
1617
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe WarmUpCache, type: :routine do
4+
before do
5+
5.times do
6+
FactoryBot.create :exercise
7+
FactoryBot.create :exercise, :published
8+
FactoryBot.create :vocab_term, vocab_distractors_count: 1
9+
FactoryBot.create :vocab_term, :published, vocab_distractors_count: 1
10+
end
11+
end
12+
13+
it 'caches all exercises and vocab terms' do
14+
expect { described_class.call }.to(
15+
change { Rails.cache.instance_variable_get(:@data).count }.by(60)
16+
)
17+
18+
expect { described_class.call }.not_to(
19+
change { Rails.cache.instance_variable_get(:@data).count }
20+
)
21+
end
22+
end

0 commit comments

Comments
 (0)