chore(faq): cache I/O on showing faq
This commit is contained in:
parent
b0503b4f28
commit
8d99e28da5
2 changed files with 31 additions and 14 deletions
|
@ -15,10 +15,12 @@ class FAQsLoaderService
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(path)
|
def find(path)
|
||||||
|
Rails.cache.fetch(["faq", path, ApplicationVersion.current, substitutions], expires_in: 1.week) do
|
||||||
file_path = @faqs_by_path.fetch(path).fetch(:file_path)
|
file_path = @faqs_by_path.fetch(path).fetch(:file_path)
|
||||||
|
|
||||||
parse_with_substitutions(file_path)
|
parse_with_substitutions(file_path)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def faqs_for_category(category)
|
def faqs_for_category(category)
|
||||||
@faqs_by_path.values
|
@faqs_by_path.values
|
||||||
|
|
|
@ -42,6 +42,23 @@ RSpec.describe FAQsLoaderService do
|
||||||
it 'returns a file with variable substitutions' do
|
it 'returns a file with variable substitutions' do
|
||||||
expect(service.find('usager/faq1').content).to include('Welcome to demarches.gouv.fr')
|
expect(service.find('usager/faq1').content).to include('Welcome to demarches.gouv.fr')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'caches file readings', caching: true do
|
||||||
|
service # this load paths, and create a first hit on file
|
||||||
|
expect(File).to have_received(:read).with('path/to/faq1.md').exactly(1).times
|
||||||
|
|
||||||
|
2.times {
|
||||||
|
service.find('usager/faq1')
|
||||||
|
expect(File).to have_received(:read).with('path/to/faq1.md').exactly(2).times
|
||||||
|
}
|
||||||
|
|
||||||
|
# depends on substitutions and re-hit files
|
||||||
|
service = FAQsLoaderService.new(substitutions.merge(application_name: "other name"))
|
||||||
|
expect(File).to have_received(:read).with('path/to/faq1.md').exactly(3).times
|
||||||
|
|
||||||
|
service.find('usager/faq1')
|
||||||
|
expect(File).to have_received(:read).with('path/to/faq1.md').exactly(4).times
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#all' do
|
describe '#all' do
|
||||||
|
@ -51,19 +68,8 @@ RSpec.describe FAQsLoaderService do
|
||||||
"admin" => { "general" => [{ category: "admin", file_path: "path/to/faq2.md", slug: "faq2", subcategory: "general", title: "FAQ2" }] }
|
"admin" => { "general" => [{ category: "admin", file_path: "path/to/faq2.md", slug: "faq2", subcategory: "general", title: "FAQ2" }] }
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe '#faqs_for_category' do
|
it 'caches file readings', caching: true do
|
||||||
it 'returns FAQs grouped by subcategory for a given category' do
|
|
||||||
result = service.faqs_for_category('usager')
|
|
||||||
expect(result).to eq({
|
|
||||||
'account' => [{ category: 'usager', subcategory: 'account', title: 'FAQ1', slug: 'faq1', file_path: 'path/to/faq1.md' }]
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'caching' do
|
|
||||||
it 'works', caching: true do
|
|
||||||
2.times {
|
2.times {
|
||||||
service = FAQsLoaderService.new(substitutions)
|
service = FAQsLoaderService.new(substitutions)
|
||||||
service.all
|
service.all
|
||||||
|
@ -76,6 +82,15 @@ RSpec.describe FAQsLoaderService do
|
||||||
expect(Dir).to have_received(:glob).twice
|
expect(Dir).to have_received(:glob).twice
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#faqs_for_category' do
|
||||||
|
it 'returns FAQs grouped by subcategory for a given category' do
|
||||||
|
result = service.faqs_for_category('usager')
|
||||||
|
expect(result).to eq({
|
||||||
|
'account' => [{ category: 'usager', subcategory: 'account', title: 'FAQ1', slug: 'faq1', file_path: 'path/to/faq1.md' }]
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with actual files" do
|
context "with actual files" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue