Skip to content

Commit 74c96c1

Browse files
committed
Merge branch 'andypa-master'
2 parents 5e820bf + dfad052 commit 74c96c1

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ In order to serve the static files directly once they've been cached, you need t
8989
}
9090
9191
location / {
92-
try_files $uri $uri/ /page-cache/$uri.html /index.php?$query_string;
92+
try_files $uri $uri/ /page-cache/$uri.html /page-cache/$uri.json /index.php?$query_string;
9393
}
9494
```
9595
@@ -104,6 +104,8 @@ In order to serve the static files directly once they've been cached, you need t
104104
RewriteRule .? page-cache/pc__index__pc.html [L]
105105
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
106106
RewriteRule . page-cache%{REQUEST_URI}.html [L]
107+
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.json -f
108+
RewriteRule . page-cache%{REQUEST_URI}.json [L]
107109
```
108110
109111
### Ignoring the cached files

src/Cache.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Container\Container;
88
use Symfony\Component\HttpFoundation\Request;
99
use Symfony\Component\HttpFoundation\Response;
10+
use Symfony\Component\HttpFoundation\JsonResponse;
1011

1112
class Cache
1213
{
@@ -150,7 +151,7 @@ public function shouldCache(Request $request, Response $response)
150151
*/
151152
public function cache(Request $request, Response $response)
152153
{
153-
list($path, $file) = $this->getDirectoryAndFileNames($request);
154+
list($path, $file) = $this->getDirectoryAndFileNames($request, $response);
154155

155156
$this->files->makeDirectory($path, 0775, true, true);
156157

@@ -169,7 +170,10 @@ public function cache(Request $request, Response $response)
169170
*/
170171
public function forget($slug)
171172
{
172-
return $this->files->delete($this->getCachePath($slug.'.html'));
173+
$deletedHtml = $this->files->delete($this->getCachePath($slug.'.html'));
174+
$deletedJson = $this->files->delete($this->getCachePath($slug.'.json'));
175+
176+
return $deletedHtml || $deletedJson;
173177
}
174178

175179
/**
@@ -186,13 +190,17 @@ public function clear()
186190
* Get the names of the directory and file.
187191
*
188192
* @param \Illuminate\Http\Request $request
193+
* @param \Illuminate\Http\Response $response
189194
* @return array
190195
*/
191-
protected function getDirectoryAndFileNames($request)
196+
protected function getDirectoryAndFileNames($request, $response)
192197
{
193198
$segments = explode('/', ltrim($request->getPathInfo(), '/'));
194199

195-
$file = $this->aliasFilename(array_pop($segments)).'.html';
200+
$filename = $this->aliasFilename(array_pop($segments));
201+
$extension = $this->guessFileExtension($response);
202+
203+
$file = "{$filename}.{$extension}";
196204

197205
return [$this->getCachePath(implode('/', $segments)), $file];
198206
}
@@ -219,4 +227,21 @@ protected function getDefaultCachePath()
219227
return $this->container->make('path.public').'/page-cache';
220228
}
221229
}
230+
231+
/**
232+
* Guess the correct file extension for the given response.
233+
*
234+
* Currently, only JSON and HTML are supported.
235+
*
236+
* @return string
237+
*/
238+
protected function guessFileExtension($response)
239+
{
240+
if ($response instanceof JsonResponse) {
241+
return 'json';
242+
}
243+
244+
return 'html';
245+
}
246+
222247
}

tests/CacheTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Filesystem\Filesystem;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\Response;
13+
use Symfony\Component\HttpFoundation\JsonResponse;
1314

1415
class CacheTest extends TestCase
1516
{
@@ -140,6 +141,23 @@ public function testCachesRootToSpecialFilename()
140141
$this->cache->cache(Request::create('/', 'GET'), Response::create('content'));
141142
}
142143

144+
public function testCachesJsonResponsesWithJsonExtension()
145+
{
146+
$content = ['this' => 'is', 'json' => [1, 2, 3]];
147+
148+
$this->files->shouldReceive('makeDirectory')->once()
149+
->with('page-cache', 0775, true, true);
150+
151+
$this->files->shouldReceive('put')->once()
152+
->with('page-cache/get-json.json', json_encode($content), true);
153+
154+
$this->cache->setCachePath('page-cache');
155+
$this->cache->cache(
156+
Request::create('get-json', 'GET'),
157+
JsonResponse::create($content)
158+
);
159+
}
160+
143161
/**
144162
* Assert that the given request/response pair are cached.
145163
*

0 commit comments

Comments
 (0)