Skip to content

Commit

Permalink
Store states in schema cache.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dracos committed Dec 6, 2023
1 parent 711bca4 commit 11dd75c
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions perllib/FixMyStreet/DB/ResultSet/State.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ 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
# uncoverable branch true
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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 11dd75c

Please sign in to comment.