diff options
author | Dave Arter <davea@mysociety.org> | 2020-01-31 16:07:26 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2020-02-26 15:25:31 +0000 |
commit | 379410d69f5d2cad7bd0dca45a09e6096b3672d6 (patch) | |
tree | 13100ea0e520bdbfc99a49e7be3634e39b5c303a | |
parent | 8c4b2a45e990dfe2e41d34883f0a4a9171650557 (diff) |
Cache available icons for front page
This saves hammering the FS for every front page request
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Offline.pm | 55 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 2 |
3 files changed, 38 insertions, 21 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm index 66e22b483..19017fc6b 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm @@ -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'))); @@ -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'))); } diff --git a/perllib/FixMyStreet/App/Controller/Offline.pm b/perllib/FixMyStreet/App/Controller/Offline.pm index d5af7e047..75bb7f7ff 100644 --- a/perllib/FixMyStreet/App/Controller/Offline.pm +++ b/perllib/FixMyStreet/App/Controller/Offline.pm @@ -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; diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index 8128d8c9d..f9fefc2ae 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -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'); |