Skip to content

Commit 8e72d2f

Browse files
committed
Cache navigation header counts
This updates `TeamCachedCounts` to cache the values in the header to ensure a more performant page load instead of needing to query the latest information from the database each time. Jira-Issue: MAV-2006
1 parent 3680d45 commit 8e72d2f

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

app/controllers/parent_interface/consent_forms_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def record
5555

5656
session.delete(:consent_form_id)
5757

58+
TeamCachedCounts.new(@team).reset_unmatched_consent_responses!
59+
5860
send_consent_form_confirmation(@consent_form)
5961

6062
ConsentFormMatchingJob.perform_later(@consent_form)

app/jobs/commit_patient_changesets_job.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def perform(import)
2929

3030
import.postprocess_rows!
3131

32+
reset_counts(import)
33+
3234
import.update_columns(
3335
processed_at: Time.zone.now,
3436
status: :processed,
@@ -165,4 +167,10 @@ def has_auto_confirmable_school_move?(school_move, import)
165167
academic_year: import.academic_year
166168
) || school_move.patient.archived?(team: import.team)
167169
end
170+
171+
def reset_counts(import)
172+
cached_counts = TeamCachedCounts.new(import.team)
173+
cached_counts.reset_import_issues!
174+
cached_counts.reset_school_moves!
175+
end
168176
end

app/jobs/consent_form_matching_job.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def match_with_exact_nhs_number
5959
patient.update_from_pds!(pds_patient)
6060
send_parental_contact_warning_if_needed(patient, @consent_form)
6161
@consent_form.match_with_patient!(patient, current_user: nil)
62+
reset_counts
63+
true
6264
end
6365

6466
def session_patients
@@ -95,5 +97,10 @@ def match_patient(patient)
9597

9698
send_parental_contact_warning_if_needed(patient, @consent_form)
9799
@consent_form.match_with_patient!(patient, current_user: nil)
100+
reset_counts
101+
end
102+
103+
def reset_counts
104+
TeamCachedCounts.new(@consent_form.team).reset_unmatched_consent_responses!
98105
end
99106
end

app/lib/team_cached_counts.rb

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,75 @@ def initialize(team)
88
def import_issues
99
return nil if current_user.nil?
1010

11-
vaccination_records_with_issues =
12-
VaccinationRecordPolicy::Scope
13-
.new(current_user, VaccinationRecord)
14-
.resolve
15-
.with_pending_changes
16-
.pluck(:patient_id)
11+
Rails
12+
.cache
13+
.fetch(import_issues_key) do
14+
vaccination_records_with_issues =
15+
VaccinationRecordPolicy::Scope
16+
.new(current_user, VaccinationRecord)
17+
.resolve
18+
.with_pending_changes
19+
.pluck(:patient_id)
1720

18-
patients_with_issues =
19-
PatientPolicy::Scope
20-
.new(current_user, Patient)
21-
.resolve
22-
.with_pending_changes
23-
.pluck(:id)
21+
patients_with_issues =
22+
PatientPolicy::Scope
23+
.new(current_user, Patient)
24+
.resolve
25+
.with_pending_changes
26+
.pluck(:id)
2427

25-
(vaccination_records_with_issues + patients_with_issues).uniq.length
28+
(vaccination_records_with_issues + patients_with_issues).uniq.length
29+
end
30+
end
31+
32+
def reset_import_issues!
33+
Rails.cache.delete(import_issues_key)
2634
end
2735

2836
def school_moves
2937
return nil if current_user.nil?
3038

31-
SchoolMovePolicy::Scope.new(current_user, SchoolMove).resolve.count
39+
Rails
40+
.cache
41+
.fetch(school_moves_key) do
42+
SchoolMovePolicy::Scope.new(current_user, SchoolMove).resolve.count
43+
end
44+
end
45+
46+
def reset_school_moves!
47+
Rails.cache.delete(school_moves_key)
3248
end
3349

3450
def unmatched_consent_responses
3551
return nil if current_user.nil?
3652

37-
ConsentFormPolicy::Scope
38-
.new(current_user, ConsentForm)
39-
.resolve
40-
.unmatched
41-
.recorded
42-
.not_archived
43-
.count
53+
Rails
54+
.cache
55+
.fetch(unmatched_consent_responses_key) do
56+
ConsentFormPolicy::Scope
57+
.new(current_user, ConsentForm)
58+
.resolve
59+
.unmatched
60+
.recorded
61+
.not_archived
62+
.count
63+
end
64+
end
65+
66+
def reset_unmatched_consent_responses!
67+
Rails.cache.delete(unmatched_consent_responses_key)
4468
end
4569

4670
private
4771

4872
attr_reader :team
4973

74+
def import_issues_key = cache_key("import-issues")
75+
76+
def school_moves_key = cache_key("school-moves")
77+
78+
def unmatched_consent_responses_key = cache_key("unmatched-consent-responses")
79+
5080
def current_user
5181
# We can't use the policy_scope helper here as we're not in a controller.
5282
# Instead, we can mock what a `User` looks like from the perspective of a
@@ -56,4 +86,6 @@ def current_user
5686
OpenStruct.new(selected_team: team, selected_organisation: organisation)
5787
end
5888
end
89+
90+
def cache_key(type) = "cached-counts/#{type}/#{team.id}"
5991
end

0 commit comments

Comments
 (0)