Skip to content

Commit 79b09be

Browse files
committed
Warm up the cache with the latest exercises daily
1 parent c48c878 commit 79b09be

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
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
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)