Skip to content

Commit

Permalink
Added the ability to work with tags without keys
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jan 15, 2024
1 parent 3eea010 commit 4646632
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
```json
{
"require": {
"dragon-code/laravel-cache": "^3.9"
"dragon-code/laravel-cache": "^3.11"
}
}
```
Expand Down Expand Up @@ -201,6 +201,9 @@ $cache->doesntHave();

$cache->forget();
// Will remove the key from the cache.

$cache->flush();
// Clears keys or tags by value
```

```php
Expand Down Expand Up @@ -237,6 +240,9 @@ $cache->doesntHave();

$cache->forget();
// Will remove the key from the cache.

$cache->flush();
// Clears keys or tags by value
```

#### Method Call Chain
Expand Down Expand Up @@ -428,6 +434,9 @@ $cache->doesntHave();

$cache->forget();
// Will remove the key from the cache.

$cache->flush();
// Clears keys or tags by value
```

To retrieve a tagged cache item, pass the same ordered list of tags to the tags method and then call the get method with
Expand All @@ -451,6 +460,9 @@ $cache->tags('actor')->get();

$cache->tags('author')->get();
// Returns `null`

$cache->tags('author')->flush();
// Clears keys or tags by value
```

> See the official Laravel [documentation](https://laravel.com/docs/cache#accessing-tagged-cache-items).
Expand Down
7 changes: 7 additions & 0 deletions src/Services/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public function forget(): static
return $this;
}

public function flush(): static
{
$this->manager()->flush();

return $this;
}

public function has(): bool
{
return $this->manager()->has($this->getKey());
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Storages/Disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public function has(string $key): bool
{
return false;
}

public function flush(): void {}
}
5 changes: 5 additions & 0 deletions src/Services/Storages/MainStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function has(string $key): bool
{
return Cache::has($key);
}

public function flush(): void
{
Cache::flush();
}
}
5 changes: 5 additions & 0 deletions src/Services/Storages/TaggedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function has(string $key): bool
return $this->cache()->has($key);
}

public function flush(): void
{
$this->cache()->flush();
}

protected function cache(): TaggedCache
{
return Cache::tags($this->tags);
Expand Down
5 changes: 5 additions & 0 deletions src/Support/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function doesntHave(string $key): bool
return ! $this->has($key);
}

public function flush(): void
{
$this->instance()->flush();
}

protected function instance(): Store
{
return match (true) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/NotWhen/Simple/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\NotWhen\Simple;

use DragonCode\Cache\Services\Cache;
use Tests\Cache\NotWhen\Base;

class ArrayTest extends Base
Expand Down Expand Up @@ -51,6 +52,22 @@ public function testForget()
$this->assertNull($this->cache()->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/NotWhen/Simple/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\NotWhen\Simple;

use DragonCode\Cache\Services\Cache;
use Tests\Cache\NotWhen\Base;

class FileTest extends Base
Expand Down Expand Up @@ -51,6 +52,22 @@ public function testForget()
$this->assertNull($this->cache()->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/NotWhen/Simple/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\NotWhen\Simple;

use DragonCode\Cache\Services\Cache;
use Illuminate\Support\Facades\Auth;
use Tests\Cache\NotWhen\Base;
use Tests\Fixtures\Models\User;
Expand Down Expand Up @@ -73,6 +74,22 @@ public function testForget()
$this->assertNull($this->cache(['cache'])->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/When/Simple/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\When\Simple;

use DragonCode\Cache\Services\Cache;
use Tests\Cache\When\Base;

class ArrayTest extends Base
Expand Down Expand Up @@ -61,6 +62,22 @@ public function testForget()
$this->assertNull($this->cache()->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/When/Simple/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\When\Simple;

use DragonCode\Cache\Services\Cache;
use Tests\Cache\When\Base;

class FileTest extends Base
Expand Down Expand Up @@ -63,6 +64,22 @@ public function testForget()
$this->assertNull($this->cache()->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/When/Simple/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Cache\When\Simple;

use DragonCode\Cache\Services\Cache;
use Illuminate\Support\Facades\Auth;
use Tests\Cache\When\Base;
use Tests\Fixtures\Models\User;
Expand Down Expand Up @@ -72,6 +73,22 @@ public function testForget()
$this->assertNull($this->cache(['cache'])->get());
}

public function testFlush()
{
$tags = Cache::make()->tags('foo', 'bar');
$cache = (clone $tags)->key('qwerty');

$this->assertNull($cache->get());

$cache->put('asd');

$this->assertSame('asd', $cache->get());

$tags->flush();

$this->assertNull($cache->get());
}

public function testHas()
{
$this->assertFalse($this->cache()->has());
Expand Down

0 comments on commit 4646632

Please sign in to comment.