Skip to content

Commit

Permalink
Merge pull request #14 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Changed behavior when caching is disabled
  • Loading branch information
Andrey Helldar authored Nov 17, 2021
2 parents e69a699 + c54fd4b commit 61e8e94
Show file tree
Hide file tree
Showing 24 changed files with 1,188 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/Concerns/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ trait Call
*/
protected function call($callback = null)
{
$callback = $this->resolve($callback);

return $this->isCallable($callback) ? $callback() : $callback;
}

Expand Down
25 changes: 6 additions & 19 deletions src/Services/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ public function key(...$values): Cache

public function get()
{
if ($this->when) {
return $this->manager()->get($this->key);
}

return null;
return $this->manager()->get($this->key);
}

/**
Expand All @@ -70,31 +66,22 @@ public function get()
*/
public function put($value)
{
if ($this->when) {
return $this->manager()->put($this->key, $value, $this->ttl);
}

return $this->call($value);
return $this->manager()->put($this->key, $value, $this->ttl);
}

public function forget(): void
{
if ($this->when) {
$this->manager()->forget($this->key);
}
$this->manager()->forget($this->key);
}

public function has(): bool
{
if ($this->when) {
return $this->manager()->has($this->key);
}

return false;
return $this->manager()->has($this->key);
}

protected function manager(): CacheManager
{
return CacheManager::make()->tags($this->tags);
return CacheManager::make($this->when)
->tags($this->tags);
}
}
27 changes: 27 additions & 0 deletions src/Services/Storages/Disabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace DragonCode\Cache\Services\Storages;

class Disabled extends Store
{
public function get(string $key, $default = null)
{
return $this->call($default);
}

public function put(string $key, $value, int $seconds)
{
return $this->get($key, $value);
}

public function forget(string $key): void
{
}

public function has(string $key): bool
{
return false;
}
}
23 changes: 19 additions & 4 deletions src/Support/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

namespace DragonCode\Cache\Support;

use DragonCode\Cache\Services\Storages\Disabled;
use DragonCode\Cache\Services\Storages\MainStore;
use DragonCode\Cache\Services\Storages\TaggedStore;
use DragonCode\Contracts\Cache\Store;
use DragonCode\Support\Concerns\Makeable;
use Illuminate\Support\Facades\Cache;

/**
* @method static CacheManager make()
* @method static CacheManager make(bool $when = true)
*/
class CacheManager implements Store
{
use Makeable;

protected $tags = [];

protected $when = true;

public function __construct(bool $when = true)
{
$this->when = $when;
}

public function tags(array $tags): CacheManager
{
$this->tags = $tags;
Expand Down Expand Up @@ -48,9 +56,16 @@ public function has(string $key): bool

protected function instance(): Store
{
return $this->allowTags()
? TaggedStore::make()->tags($this->tags)
: MainStore::make();
switch (true) {
case ! $this->when:
return Disabled::make();

case $this->allowTags():
return TaggedStore::make()->tags($this->tags);

default:
return MainStore::make();
}
}

protected function allowTags(): bool
Expand Down
56 changes: 56 additions & 0 deletions tests/Cache/NotWhen/Arrayables/Many/Arr/DragonCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;

use Tests\Cache\NotWhen\BaseTest;
use Tests\Fixtures\Many\DragonCodeArrayable;

class DragonCodeTest extends BaseTest
{
protected $value = [
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => [
'foo' => 'Foo',
'bar' => 'Bar',
],
];

public function testGet()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new DragonCodeArrayable());

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

public function testPut()
{
$this->assertSame($this->value, $this->cache()->put(new DragonCodeArrayable()));

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

public function testForget()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new DragonCodeArrayable());

$this->cache()->forget();

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

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

$this->cache()->put(new DragonCodeArrayable());

$this->assertFalse($this->cache()->has());
}
}
56 changes: 56 additions & 0 deletions tests/Cache/NotWhen/Arrayables/Many/Arr/IlluminateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;

use Tests\Cache\NotWhen\BaseTest;
use Tests\Fixtures\Many\IlluminateArrayable;

class IlluminateTest extends BaseTest
{
protected $value = [
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => [
'foo' => 'Foo',
'bar' => 'Bar',
],
];

public function testGet()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new IlluminateArrayable());

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

public function testPut()
{
$this->assertSame($this->value, $this->cache()->put(new IlluminateArrayable()));

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

public function testForget()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new IlluminateArrayable());

$this->cache()->forget();

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

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

$this->cache()->put(new IlluminateArrayable());

$this->assertFalse($this->cache()->has());
}
}
60 changes: 60 additions & 0 deletions tests/Cache/NotWhen/Arrayables/Many/Arr/MixedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;

use Tests\Cache\NotWhen\BaseTest;
use Tests\Fixtures\Many\MixedArrayable;

class MixedTest extends BaseTest
{
protected $value = [
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => [
'foo' => 'Foo',
'bar' => 'Bar',
],
'baq' => [
'foo' => 'Foo',
'bar' => 'Bar',
],
];

public function testGet()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new MixedArrayable());

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

public function testPut()
{
$this->assertSame($this->value, $this->cache()->put(new MixedArrayable()));

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

public function testForget()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new MixedArrayable());

$this->cache()->forget();

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

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

$this->cache()->put(new MixedArrayable());

$this->assertFalse($this->cache()->has());
}
}
58 changes: 58 additions & 0 deletions tests/Cache/NotWhen/Arrayables/Many/Files/DragonCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Tests\Cache\NotWhen\Arrayables\Many\Files;

use Tests\Cache\NotWhen\BaseTest;
use Tests\Fixtures\Many\DragonCodeArrayable;

class DragonCodeTest extends BaseTest
{
protected $cache = 'file';

protected $value = [
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => [
'foo' => 'Foo',
'bar' => 'Bar',
],
];

public function testGet()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new DragonCodeArrayable());

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

public function testPut()
{
$this->assertSame($this->value, $this->cache()->put(new DragonCodeArrayable()));

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

public function testForget()
{
$this->assertNull($this->cache()->get());

$this->cache()->put(new DragonCodeArrayable());

$this->cache()->forget();

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

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

$this->cache()->put(new DragonCodeArrayable());

$this->assertFalse($this->cache()->has());
}
}
Loading

0 comments on commit 61e8e94

Please sign in to comment.