Skip to content

Commit

Permalink
Split checking of static cache vs getting it (#3255)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Feb 16, 2021
1 parent e1fc288 commit 84c0829
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
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

0 comments on commit 84c0829

Please sign in to comment.