-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Hkp22\CacheLaraViewFragments; | ||
|
||
use Illuminate\Contracts\Cache\Repository as Cache; | ||
|
||
class FragmentCaching | ||
{ | ||
/** | ||
* The cache repository. | ||
* | ||
* @var Cache | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* Create a new class instance. | ||
* | ||
* @param Cache $cache | ||
*/ | ||
public function __construct(Cache $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* Put to the cache. | ||
* | ||
* @param mixed $key | ||
* @param string $fragment | ||
*/ | ||
public function put($key, $fragment) | ||
{ | ||
$key = $this->normalizeCacheKey($key); | ||
|
||
return $this->cache | ||
->tags('views') | ||
->rememberForever($key, function () use ($fragment) { | ||
return $fragment; | ||
}); | ||
} | ||
|
||
/** | ||
* Check if the given key exists in the cache. | ||
* | ||
* @param mixed $key | ||
*/ | ||
public function has($key) | ||
{ | ||
$key = $this->normalizeCacheKey($key); | ||
|
||
return $this->cache | ||
->tags('views') | ||
->has($key); | ||
} | ||
|
||
/** | ||
* Normalize the cache key. | ||
* | ||
* @param mixed $key | ||
*/ | ||
protected function normalizeCacheKey($key) | ||
{ | ||
if (is_object($key) && method_exists($key, 'getCacheKey')) { | ||
return $key->getCacheKey(); | ||
} | ||
|
||
return $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Hkp22\Tests\CacheLaraViewFragments; | ||
|
||
use Hkp22\CacheLaraViewFragments\FragmentCaching; | ||
use Hkp22\Tests\CacheLaraViewFragments\Stubs\Models\Post; | ||
|
||
class FragmentCachingTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_caches_the_given_key() | ||
{ | ||
$post = $this->makePost(); | ||
|
||
$cache = new \Illuminate\Cache\Repository( | ||
new \Illuminate\Cache\ArrayStore | ||
); | ||
$cache = new FragmentCaching($cache); | ||
|
||
$cache->put($post->getCacheKey(), '<div>view fragment</div>'); | ||
|
||
$this->assertTrue($cache->has($post->getCacheKey())); | ||
$this->assertTrue($cache->has($post)); | ||
} | ||
} |