Skip to content

Commit

Permalink
let half measure caching continue to only request from cache once
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Feb 16, 2021
1 parent db163dc commit 4d2ba1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
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
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 4d2ba1f

Please sign in to comment.