Skip to content

Commit

Permalink
Merge branch 'andypa-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Jul 19, 2020
2 parents 5e820bf + dfad052 commit 74c96c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ In order to serve the static files directly once they've been cached, you need t
}
location / {
try_files $uri $uri/ /page-cache/$uri.html /index.php?$query_string;
try_files $uri $uri/ /page-cache/$uri.html /page-cache/$uri.json /index.php?$query_string;
}
```
Expand All @@ -104,6 +104,8 @@ In order to serve the static files directly once they've been cached, you need t
RewriteRule .? page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
RewriteRule . page-cache%{REQUEST_URI}.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.json -f
RewriteRule . page-cache%{REQUEST_URI}.json [L]
```
### Ignoring the cached files
Expand Down
33 changes: 29 additions & 4 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Container\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class Cache
{
Expand Down Expand Up @@ -150,7 +151,7 @@ public function shouldCache(Request $request, Response $response)
*/
public function cache(Request $request, Response $response)
{
list($path, $file) = $this->getDirectoryAndFileNames($request);
list($path, $file) = $this->getDirectoryAndFileNames($request, $response);

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

Expand All @@ -169,7 +170,10 @@ public function cache(Request $request, Response $response)
*/
public function forget($slug)
{
return $this->files->delete($this->getCachePath($slug.'.html'));
$deletedHtml = $this->files->delete($this->getCachePath($slug.'.html'));
$deletedJson = $this->files->delete($this->getCachePath($slug.'.json'));

return $deletedHtml || $deletedJson;
}

/**
Expand All @@ -186,13 +190,17 @@ public function clear()
* Get the names of the directory and file.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @return array
*/
protected function getDirectoryAndFileNames($request)
protected function getDirectoryAndFileNames($request, $response)
{
$segments = explode('/', ltrim($request->getPathInfo(), '/'));

$file = $this->aliasFilename(array_pop($segments)).'.html';
$filename = $this->aliasFilename(array_pop($segments));
$extension = $this->guessFileExtension($response);

$file = "{$filename}.{$extension}";

return [$this->getCachePath(implode('/', $segments)), $file];
}
Expand All @@ -219,4 +227,21 @@ protected function getDefaultCachePath()
return $this->container->make('path.public').'/page-cache';
}
}

/**
* Guess the correct file extension for the given response.
*
* Currently, only JSON and HTML are supported.
*
* @return string
*/
protected function guessFileExtension($response)
{
if ($response instanceof JsonResponse) {
return 'json';
}

return 'html';
}

}
18 changes: 18 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

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

public function testCachesJsonResponsesWithJsonExtension()
{
$content = ['this' => 'is', 'json' => [1, 2, 3]];

$this->files->shouldReceive('makeDirectory')->once()
->with('page-cache', 0775, true, true);

$this->files->shouldReceive('put')->once()
->with('page-cache/get-json.json', json_encode($content), true);

$this->cache->setCachePath('page-cache');
$this->cache->cache(
Request::create('get-json', 'GET'),
JsonResponse::create($content)
);
}

/**
* Assert that the given request/response pair are cached.
*
Expand Down

0 comments on commit 74c96c1

Please sign in to comment.