Skip to content

Commit

Permalink
Merge pull request #68 from TheDragonCode/3.x
Browse files Browse the repository at this point in the history
Added `Cache::call()` method
  • Loading branch information
andrey-helldar authored May 31, 2023
2 parents c8a4bcb + 1daf955 commit 133b203
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,52 @@ $cache->forget();
// Will remove the key from the cache.
```

#### Method Call Chain

Sometimes in the process of working with a cache, it becomes necessary to call some code between certain actions, and in this case the `call` method will come to the rescue:

```php
use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo');
$warmUp = false;

$cache
->call(fn (Cache $cache) => $cache->forget(), $warmUp)
->call(fn () => $someService->someMethod())
->remember('foo')
```

In addition, the `forget` method now returns an instance of the `Cache` object, so it can be used like this:

```php
use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo');

$cache
->forget()
->call(fn () => $someService->someMethod())
->remember('foo')
```

Previously, you had to use the following sequence:

```php
use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo');
$warmUp = false;

if ($warmUp) {
$cache->forget();
}

$someService->someMethod()

$cache->remember('foo')
```

#### Custom TTL

By default, the cache will be written for 1 day.
Expand Down
15 changes: 14 additions & 1 deletion src/Services/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace DragonCode\Cache\Services;

use Closure;
use DragonCode\Cache\Facades\Support\Key;
use DragonCode\Cache\Facades\Support\Tag;
use DragonCode\Cache\Facades\Support\Ttl;
use DragonCode\Cache\Support\CacheManager;
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Instances\Call;
use Illuminate\Support\Facades\Auth;

/**
Expand Down Expand Up @@ -96,9 +98,11 @@ public function remember(mixed $value): mixed
return $this->manager()->remember($this->getKey(), $value, $this->ttl);
}

public function forget(): void
public function forget(): static
{
$this->manager()->forget($this->getKey());

return $this;
}

public function has(): bool
Expand All @@ -111,6 +115,15 @@ public function doesntHave(): bool
return $this->manager()->doesntHave($this->getKey());
}

public function call(Closure $callback, mixed $when = true): static
{
if (Call::value($when)) {
Call::value($callback, $this);
}

return $this;
}

protected function manager(): CacheManager
{
return CacheManager::make($this->when)
Expand Down
38 changes: 38 additions & 0 deletions tests/Cache/NotWhen/Simple/ChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\NotWhen\Simple;

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

class ChainTest extends Base
{
protected $cache = 'redis';

public function testMain()
{
$this->assertTrue($this->cache()->doesntHave());

$this->cache()->put($this->value);

$this->cache()
->call(fn (Cache $cache) => $this->assertTrue($cache->doesntHave()))
->forget()
->call(fn (Cache $cache) => $this->assertTrue($cache->doesntHave()));
}

public function testWhen()
{
$this->assertTrue($this->cache()->doesntHave());

$this->cache()->put($this->value);

$this->cache()
->call(fn (Cache $cache) => $this->assertTrue(false), false)
->call(fn (Cache $cache) => $this->assertTrue(false), fn () => false)
->call(fn (Cache $cache) => $this->assertTrue(false), 0)
->call(fn (Cache $cache) => $this->assertTrue(false), fn () => 0);
}
}
38 changes: 38 additions & 0 deletions tests/Cache/When/Simple/ChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\When\Simple;

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

class ChainTest extends Base
{
protected $cache = 'redis';

public function testMain()
{
$this->assertTrue($this->cache()->doesntHave());

$this->cache()->put($this->value);

$this->cache()
->call(fn (Cache $cache) => $this->assertTrue($cache->has()))
->forget()
->call(fn (Cache $cache) => $this->assertTrue($cache->doesntHave()));
}

public function testWhen()
{
$this->assertTrue($this->cache()->doesntHave());

$this->cache()->put($this->value);

$this->cache()
->call(fn (Cache $cache) => $this->assertTrue(false), false)
->call(fn (Cache $cache) => $this->assertTrue(false), fn () => false)
->call(fn (Cache $cache) => $this->assertTrue(false), 0)
->call(fn (Cache $cache) => $this->assertTrue(false), fn () => 0);
}
}

0 comments on commit 133b203

Please sign in to comment.