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 91ba974
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions perllib/FixMyStreet/DB/ResultSet/State.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ sub _hardcoded_states {
# we cache these in the package on first use, and clear on update.

sub clear {
my $rs = shift;
Memcached::set('states', '');
my $cache = $rs->result_source->schema->cache;
delete $cache->{states};
}

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 +52,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 91ba974

Please sign in to comment.