From b7e6d8c69c9c083ce1981d8749f39e3ef7c68ec2 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 20 Aug 2018 16:40:04 +0200 Subject: [PATCH] HTTP Cache --- .gitignore | 1 + Makefile | 1 + app/bootstrap.php.d/20-twig.php | 2 +- app/bootstrap.php.d/80-cache.php | 7 +++++++ src/Gist/Controller/Controller.php | 10 +++++++++- web/index.php | 2 +- 6 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 app/bootstrap.php.d/80-cache.php diff --git a/.gitignore b/.gitignore index f5e2c76..6d98c4e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ /app/config/propel/ /data/ /trans/ +/cache/ diff --git a/Makefile b/Makefile index 9edb0c5..8bf831b 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ update: $(GIT) pull origin master ${MKDIR} -p data/git + ${MKDIR} -p data/cache $(COMPOSER) update $(NPM) install diff --git a/app/bootstrap.php.d/20-twig.php b/app/bootstrap.php.d/20-twig.php index 8da7180..e69e1c6 100644 --- a/app/bootstrap.php.d/20-twig.php +++ b/app/bootstrap.php.d/20-twig.php @@ -9,6 +9,6 @@ $app->register(new TwigServiceProvider(), array( $app->extend('twig', function ($twig, $app) { $base = str_replace($app['request']->server->get('SCRIPT_NAME'), '', $app['request']->getBaseUrl()); $twig->addGlobal('web_path', $base.'/'); - + return $twig; }); diff --git a/app/bootstrap.php.d/80-cache.php b/app/bootstrap.php.d/80-cache.php new file mode 100644 index 0000000..3013cd6 --- /dev/null +++ b/app/bootstrap.php.d/80-cache.php @@ -0,0 +1,7 @@ +register(new HttpCacheServiceProvider(), array( + 'http_cache.cache_dir' => $app['root_path'].'/cache/', +)); diff --git a/src/Gist/Controller/Controller.php b/src/Gist/Controller/Controller.php index 5e71381..7cbcccd 100644 --- a/src/Gist/Controller/Controller.php +++ b/src/Gist/Controller/Controller.php @@ -175,9 +175,17 @@ abstract class Controller $params['user'] = $this->getUser(); } - return $app['twig']->render( + $body = $app['twig']->render( $template, $params ); + + $response = new Response($body); + + if (empty($params['no_cache'])) { + $response->setTtl(3600 * 24 * 7); + } + + return $response; } } diff --git a/web/index.php b/web/index.php index 5dd7e92..d66b7a8 100644 --- a/web/index.php +++ b/web/index.php @@ -4,4 +4,4 @@ $app = require __DIR__.'/../app/bootstrap.php'; $app['env'] = 'prod'; -$app->run(); +$app['http_cache']->run();