Skip to content

Commit

Permalink
Cache available icons for front page
Browse files Browse the repository at this point in the history
This saves hammering the FS for every front page request
  • Loading branch information
davea committed Jan 31, 2020
1 parent 5a7a9aa commit edb09b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
2 changes: 2 additions & 0 deletions perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sub form {

if ($c->get_param('delete_theme')) {
$c->forward('_delete_all_manifest_icons');
$c->forward('/offline/_clear_manifest_icons_cache', [ $theme->cobrand ]);
$theme->delete;
$c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', 'delete' ]);
$c->response->redirect($c->uri_for($self->action_for('index')));
Expand All @@ -82,6 +83,7 @@ sub form {
return unless $form->validated;

$c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', $action ]);
$c->forward('/offline/_clear_manifest_icons_cache', [ $theme->cobrand ]);
$c->response->redirect($c->uri_for($self->action_for('index')));
}

Expand Down
55 changes: 36 additions & 19 deletions perllib/FixMyStreet/App/Controller/Offline.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,47 @@ sub manifest: Path("/.well-known/manifest.webmanifest") {
}

sub _stash_manifest_icons : Private {
my ($self, $c, $cobrand, $skip_defaults) = @_;

my @icons;
my $uri = '/theme/' . $cobrand;
my $theme_path = path(FixMyStreet->path_to('web' . $uri));
$theme_path->visit(
sub {
my ($x, $y, $typ) = Image::Size::imgsize($_->stringify);
push @icons, {
src => join('/', $uri, $_->basename),
sizes => join('x', $x, $y),
type => $typ eq 'PNG' ? 'image/png' : $typ eq 'GIF' ? 'image/gif' : $typ eq 'JPG' ? 'image/jpeg' : '',
};
my ($self, $c, $cobrand, $ignore_cache_and_defaults) = @_;

my $key = "manifest_icons:$cobrand";
# ignore_cache_and_defaults is only used in the admin, so no harm bypassing cache
my $icons = $ignore_cache_and_defaults ? undef : Memcached::get($key);

unless ( $icons ) {
my @icons;
my $uri = '/theme/' . $cobrand;
my $theme_path = path(FixMyStreet->path_to('web' . $uri));
$theme_path->visit(
sub {
my ($x, $y, $typ) = Image::Size::imgsize($_->stringify);
push @icons, {
src => join('/', $uri, $_->basename),
sizes => join('x', $x, $y),
type => $typ eq 'PNG' ? 'image/png' : $typ eq 'GIF' ? 'image/gif' : $typ eq 'JPG' ? 'image/jpeg' : '',
};
}
);

unless (@icons || $ignore_cache_and_defaults) {
push @icons,
{ src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" },
{ src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" };
}
);

unless (@icons || $skip_defaults) {
push @icons,
{ src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" },
{ src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" };
$icons = \@icons;

unless ($ignore_cache_and_defaults) {
Memcached::set($key, $icons);
}
}

$c->stash->{manifest_icons} = \@icons;
$c->stash->{manifest_icons} = $icons;
}

sub _clear_manifest_icons_cache : Private {
my ($self, $c, $cobrand ) = @_;

Memcached::set("manifest_icons:$cobrand", "");
}

__PACKAGE__->meta->make_immutable;
Expand Down
2 changes: 0 additions & 2 deletions perllib/FixMyStreet/App/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ sub index : Path : Args(0) {
$c->detach;
}

# TODO: Not sure we want to hammer the FS for every front page request,
# might need a smarter way to tell iOS about the icons
$c->forward('/offline/_stash_manifest_icons', [ $c->cobrand->moniker ]);

$c->forward('/auth/get_csrf_token');
Expand Down

0 comments on commit edb09b5

Please sign in to comment.