Skip to content

Commit

Permalink
[8.x] Implement supportsTags() on the Cache Repository (#34820)
Browse files Browse the repository at this point in the history
* Implement supportsTags() on the Cache Repository in order to find out more elegantly if the currently chosen cache store is taggable.

* fixed test variable names

* Update Repository.php

Co-authored-by: Mathias Onea <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2020
1 parent 8e72778 commit d004392
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public function clear()
*/
public function tags($names)
{
if (! method_exists($this->store, 'tags')) {
if (! $this->supportsTags()) {
throw new BadMethodCallException('This cache store does not support tagging.');
}

Expand All @@ -501,6 +501,33 @@ protected function itemKey($key)
return $key;
}

/**
* Calculate the number of seconds for the given TTL.
*
* @param \DateTimeInterface|\DateInterval|int $ttl
* @return int
*/
protected function getSeconds($ttl)
{
$duration = $this->parseDateInterval($ttl);

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInRealSeconds($duration, false);
}

return (int) $duration > 0 ? $duration : 0;
}

/**
* Determine if the current store supports tags.
*
* @return bool
*/
public function supportsTags()
{
return method_exists($this->store, 'tags');
}

/**
* Get the default cache time.
*
Expand Down Expand Up @@ -613,23 +640,6 @@ public function offsetUnset($key)
$this->forget($key);
}

/**
* Calculate the number of seconds for the given TTL.
*
* @param \DateTimeInterface|\DateInterval|int $ttl
* @return int
*/
protected function getSeconds($ttl)
{
$duration = $this->parseDateInterval($ttl);

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInRealSeconds($duration, false);
}

return (int) $duration > 0 ? $duration : 0;
}

/**
* Handle dynamic calls into macros or pass missing methods to the store.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use DateTime;
use DateTimeImmutable;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\FileStore;
use Illuminate\Cache\RedisStore;
use Illuminate\Cache\Repository;
use Illuminate\Cache\TaggableStore;
use Illuminate\Container\Container;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Events\Dispatcher;
Expand Down Expand Up @@ -312,6 +314,22 @@ public function testAllTagsArePassedToTaggableStore()
$repo->tags('foo', 'bar', 'baz');
}

public function testTaggableRepositoriesSupportTags()
{
$taggable = m::mock(TaggableStore::class);
$taggableRepo = new Repository($taggable);

$this->assertTrue($taggableRepo->supportsTags());
}

public function testNonTaggableRepositoryDoesNotSupportTags()
{
$nonTaggable = m::mock(FileStore::class);
$nonTaggableRepo = new Repository($nonTaggable);

$this->assertFalse($nonTaggableRepo->supportsTags());
}

protected function getRepository()
{
$dispatcher = new Dispatcher(m::mock(Container::class));
Expand Down

0 comments on commit d004392

Please sign in to comment.