Browse Source

HTTP Cache

Simon Vieille 3 months ago
parent
commit
b7e6d8c69c
No known key found for this signature in database

+ 1
- 0
.gitignore View File

@@ -11,3 +11,4 @@
11 11
 /app/config/propel/
12 12
 /data/
13 13
 /trans/
14
+/cache/

+ 1
- 0
Makefile View File

@@ -26,6 +26,7 @@ update:
26 26
 
27 27
 	$(GIT) pull origin master
28 28
 	${MKDIR} -p data/git
29
+	${MKDIR} -p data/cache
29 30
 	$(COMPOSER) update
30 31
 	$(NPM) install
31 32
 

+ 1
- 1
app/bootstrap.php.d/20-twig.php View File

@@ -9,6 +9,6 @@ $app->register(new TwigServiceProvider(), array(
9 9
 $app->extend('twig', function ($twig, $app) {
10 10
     $base = str_replace($app['request']->server->get('SCRIPT_NAME'), '', $app['request']->getBaseUrl());
11 11
     $twig->addGlobal('web_path', $base.'/');
12
-    
12
+
13 13
     return $twig;
14 14
 });

+ 7
- 0
app/bootstrap.php.d/80-cache.php View File

@@ -0,0 +1,7 @@
1
+<?php
2
+
3
+use Silex\Provider\HttpCacheServiceProvider;
4
+
5
+$app->register(new HttpCacheServiceProvider(), array(
6
+    'http_cache.cache_dir' => $app['root_path'].'/cache/',
7
+));

+ 9
- 1
src/Gist/Controller/Controller.php View File

@@ -175,9 +175,17 @@ abstract class Controller
175 175
             $params['user'] = $this->getUser();
176 176
         }
177 177
 
178
-        return $app['twig']->render(
178
+        $body = $app['twig']->render(
179 179
             $template,
180 180
             $params
181 181
         );
182
+
183
+        $response = new Response($body);
184
+
185
+        if (empty($params['no_cache'])) {
186
+            $response->setTtl(3600 * 24 * 7);
187
+        }
188
+
189
+        return $response;
182 190
     }
183 191
 }

+ 1
- 1
web/index.php View File

@@ -4,4 +4,4 @@ $app = require __DIR__.'/../app/bootstrap.php';
4 4
 
5 5
 $app['env'] = 'prod';
6 6
 
7
-$app->run();
7
+$app['http_cache']->run();