Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split checking of static cache vs getting it #3255

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/StaticCaching/Cacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ interface Cacher
*/
public function cachePage(Request $request, $content);

/**
* Check if a page has been cached.
*
* @param Request $request
* @return bool
*/
public function hasCachedPage(Request $request);

/**
* Get a cached page.
*
Expand Down
5 changes: 5 additions & 0 deletions src/StaticCaching/Cachers/AbstractCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,9 @@ protected function getUrlsCacheKey($domain = null)

return $this->normalizeKey($this->makeHash($domain).'.urls');
}

public function hasCachedPage(Request $request)
{
return $this->getCachedPage($request) !== null;
}
}
21 changes: 21 additions & 0 deletions src/StaticCaching/Cachers/ApplicationCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class ApplicationCacher extends AbstractCacher
*/
protected $cache;

/**
* @var string|null
*/
private $cached;

/**
* Cache a page.
*
Expand Down Expand Up @@ -42,13 +47,29 @@ public function cachePage(Request $request, $content)
}
}

/**
* Check if a page has been cached.
*
* @param Request $request
* @return bool
*/
public function hasCachedPage(Request $request)
{
return (bool) $this->cached = $this->getFromCache($request);
}

/**
* Get a cached page.
*
* @param Request $request
* @return string
*/
public function getCachedPage(Request $request)
{
return $this->cached ?? $this->getFromCache($request);
}

private function getFromCache(Request $request)
{
$url = $this->getUrl($request);

Expand Down
7 changes: 7 additions & 0 deletions src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function getCachedPage(Request $request)
return File::get($this->getFilePath($url));
}

public function hasCachedPage(Request $request)
{
$url = $this->getUrl($request);

return File::exists($this->getFilePath($url));
}

/**
* Flush out the entire static cache.
*
Expand Down
5 changes: 5 additions & 0 deletions src/StaticCaching/Cachers/NullCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function cachePage(Request $request, $content)
//
}

public function hasCachedPage(Request $request)
{
return false;
}

public function getCachedPage(Request $request)
{
//
Expand Down
4 changes: 2 additions & 2 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __construct(Cacher $cacher)
*/
public function handle($request, Closure $next)
{
if ($this->canBeCached($request) && ($cached = $this->cacher->getCachedPage($request))) {
return response($cached);
if ($this->canBeCached($request) && $this->cacher->hasCachedPage($request)) {
return response($this->cacher->getCachedPage($request));
}

$response = $next($request);
Expand Down
36 changes: 34 additions & 2 deletions tests/StaticCaching/ApplicationCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,46 @@

class ApplicationCacherTest extends TestCase
{
/** @test */
public function it_checks_if_a_page_is_cached()
{
$key = 'static-cache:responses:'.md5('http://example.com/test?foo=bar');
$cache = $this->mock(Repository::class);
$cache->shouldReceive('get')->with($key)->times(2)->andReturn(null, 'html content');
$cache->shouldNotReceive('has');

$cacher = new ApplicationCacher($cache, []);
$request = Request::create('http://example.com/test', 'GET', ['foo' => 'bar']);

$this->assertFalse($cacher->hasCachedPage($request));
$this->assertTrue($cacher->hasCachedPage($request));
}

/** @test */
public function gets_cached_page()
{
$cache = app(Repository::class);
$key = 'static-cache:responses:'.md5('http://example.com/test?foo=bar');
$cache = $this->mock(Repository::class);
$cache->shouldReceive('get')->with($key)->once()->andReturn('html content');

$cacher = new ApplicationCacher($cache, []);
$request = Request::create('http://example.com/test', 'GET', ['foo' => 'bar']);

$this->assertEquals('html content', $cacher->getCachedPage($request));
}

/** @test */
public function checking_if_page_is_cached_then_retrieving_it_will_only_hit_the_cache_once()
{
$key = 'static-cache:responses:'.md5('http://example.com/test?foo=bar');
$cache = $this->mock(Repository::class);
$cache->shouldReceive('get')->with($key)->once()->andReturn('html content');
$cache->shouldNotReceive('has');

$cacher = new ApplicationCacher($cache, []);
$cache->forever('static-cache:responses:'.md5('http://example.com/test?foo=bar'), 'html content');
$request = Request::create('http://example.com/test', 'GET', ['foo' => 'bar']);

$this->assertTrue($cacher->hasCachedPage($request));
$this->assertEquals('html content', $cacher->getCachedPage($request));
}

Expand Down