From 11dd75ccc4e5721cdd08da48f11f5618cf5eac4c Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 6 Dec 2023 10:34:46 +0000 Subject: [PATCH] Store states in schema cache. Even though these are saved in memcache, if we need to look it up a lot of times in one request, is quicker to cache it locally as well. --- perllib/FixMyStreet/DB/ResultSet/State.pm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/perllib/FixMyStreet/DB/ResultSet/State.pm b/perllib/FixMyStreet/DB/ResultSet/State.pm index 4f98efbf24a..386ec0dc85d 100644 --- a/perllib/FixMyStreet/DB/ResultSet/State.pm +++ b/perllib/FixMyStreet/DB/ResultSet/State.pm @@ -24,6 +24,9 @@ sub clear { sub states { my $rs = shift; + my $cache = $rs->result_source->schema->cache; + return $cache->{states} if $cache->{states}; + my $states = Memcached::get('states'); # If tests are run in parallel, the cached state in Memcached could be # corrupted by multiple tests changing it at the same time @@ -31,6 +34,7 @@ sub states { if ($states && !FixMyStreet->test_mode) { # Need to reattach schema $states->[0]->result_source->schema( $rs->result_source->schema ) if $states->[0]; + $cache->{states} = $states; return $states; } @@ -45,6 +49,7 @@ sub states { my @states = ($rs->_hardcoded_states, $rs->search(undef, { order_by => 'label' })->all); $_->translated->{name} = $trans{$_->id} || {} foreach @states; $states = \@states; + $cache->{states} = $states; Memcached::set('states', $states); return $states; }