Skip to content

Commit 61e8e94

Browse files
author
Andrey Helldar
authored
Merge pull request #14 from TheDragonCode/2.x
Changed behavior when caching is disabled
2 parents e69a699 + c54fd4b commit 61e8e94

File tree

24 files changed

+1188
-23
lines changed

24 files changed

+1188
-23
lines changed

src/Concerns/Call.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ trait Call
2121
*/
2222
protected function call($callback = null)
2323
{
24+
$callback = $this->resolve($callback);
25+
2426
return $this->isCallable($callback) ? $callback() : $callback;
2527
}
2628

src/Services/Cache.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ public function key(...$values): Cache
5656

5757
public function get()
5858
{
59-
if ($this->when) {
60-
return $this->manager()->get($this->key);
61-
}
62-
63-
return null;
59+
return $this->manager()->get($this->key);
6460
}
6561

6662
/**
@@ -70,31 +66,22 @@ public function get()
7066
*/
7167
public function put($value)
7268
{
73-
if ($this->when) {
74-
return $this->manager()->put($this->key, $value, $this->ttl);
75-
}
76-
77-
return $this->call($value);
69+
return $this->manager()->put($this->key, $value, $this->ttl);
7870
}
7971

8072
public function forget(): void
8173
{
82-
if ($this->when) {
83-
$this->manager()->forget($this->key);
84-
}
74+
$this->manager()->forget($this->key);
8575
}
8676

8777
public function has(): bool
8878
{
89-
if ($this->when) {
90-
return $this->manager()->has($this->key);
91-
}
92-
93-
return false;
79+
return $this->manager()->has($this->key);
9480
}
9581

9682
protected function manager(): CacheManager
9783
{
98-
return CacheManager::make()->tags($this->tags);
84+
return CacheManager::make($this->when)
85+
->tags($this->tags);
9986
}
10087
}

src/Services/Storages/Disabled.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Cache\Services\Storages;
6+
7+
class Disabled extends Store
8+
{
9+
public function get(string $key, $default = null)
10+
{
11+
return $this->call($default);
12+
}
13+
14+
public function put(string $key, $value, int $seconds)
15+
{
16+
return $this->get($key, $value);
17+
}
18+
19+
public function forget(string $key): void
20+
{
21+
}
22+
23+
public function has(string $key): bool
24+
{
25+
return false;
26+
}
27+
}

src/Support/CacheManager.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44

55
namespace DragonCode\Cache\Support;
66

7+
use DragonCode\Cache\Services\Storages\Disabled;
78
use DragonCode\Cache\Services\Storages\MainStore;
89
use DragonCode\Cache\Services\Storages\TaggedStore;
910
use DragonCode\Contracts\Cache\Store;
1011
use DragonCode\Support\Concerns\Makeable;
1112
use Illuminate\Support\Facades\Cache;
1213

1314
/**
14-
* @method static CacheManager make()
15+
* @method static CacheManager make(bool $when = true)
1516
*/
1617
class CacheManager implements Store
1718
{
1819
use Makeable;
1920

2021
protected $tags = [];
2122

23+
protected $when = true;
24+
25+
public function __construct(bool $when = true)
26+
{
27+
$this->when = $when;
28+
}
29+
2230
public function tags(array $tags): CacheManager
2331
{
2432
$this->tags = $tags;
@@ -48,9 +56,16 @@ public function has(string $key): bool
4856

4957
protected function instance(): Store
5058
{
51-
return $this->allowTags()
52-
? TaggedStore::make()->tags($this->tags)
53-
: MainStore::make();
59+
switch (true) {
60+
case ! $this->when:
61+
return Disabled::make();
62+
63+
case $this->allowTags():
64+
return TaggedStore::make()->tags($this->tags);
65+
66+
default:
67+
return MainStore::make();
68+
}
5469
}
5570

5671
protected function allowTags(): bool
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
8+
use Tests\Fixtures\Many\DragonCodeArrayable;
9+
10+
class DragonCodeTest extends BaseTest
11+
{
12+
protected $value = [
13+
'foo' => 'Foo',
14+
'bar' => 'Bar',
15+
'baz' => [
16+
'foo' => 'Foo',
17+
'bar' => 'Bar',
18+
],
19+
];
20+
21+
public function testGet()
22+
{
23+
$this->assertNull($this->cache()->get());
24+
25+
$this->cache()->put(new DragonCodeArrayable());
26+
27+
$this->assertNull($this->cache()->get());
28+
}
29+
30+
public function testPut()
31+
{
32+
$this->assertSame($this->value, $this->cache()->put(new DragonCodeArrayable()));
33+
34+
$this->assertNull($this->cache()->get());
35+
}
36+
37+
public function testForget()
38+
{
39+
$this->assertNull($this->cache()->get());
40+
41+
$this->cache()->put(new DragonCodeArrayable());
42+
43+
$this->cache()->forget();
44+
45+
$this->assertNull($this->cache()->get());
46+
}
47+
48+
public function testHas()
49+
{
50+
$this->assertFalse($this->cache()->has());
51+
52+
$this->cache()->put(new DragonCodeArrayable());
53+
54+
$this->assertFalse($this->cache()->has());
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
8+
use Tests\Fixtures\Many\IlluminateArrayable;
9+
10+
class IlluminateTest extends BaseTest
11+
{
12+
protected $value = [
13+
'foo' => 'Foo',
14+
'bar' => 'Bar',
15+
'baz' => [
16+
'foo' => 'Foo',
17+
'bar' => 'Bar',
18+
],
19+
];
20+
21+
public function testGet()
22+
{
23+
$this->assertNull($this->cache()->get());
24+
25+
$this->cache()->put(new IlluminateArrayable());
26+
27+
$this->assertNull($this->cache()->get());
28+
}
29+
30+
public function testPut()
31+
{
32+
$this->assertSame($this->value, $this->cache()->put(new IlluminateArrayable()));
33+
34+
$this->assertNull($this->cache()->get());
35+
}
36+
37+
public function testForget()
38+
{
39+
$this->assertNull($this->cache()->get());
40+
41+
$this->cache()->put(new IlluminateArrayable());
42+
43+
$this->cache()->forget();
44+
45+
$this->assertNull($this->cache()->get());
46+
}
47+
48+
public function testHas()
49+
{
50+
$this->assertFalse($this->cache()->has());
51+
52+
$this->cache()->put(new IlluminateArrayable());
53+
54+
$this->assertFalse($this->cache()->has());
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\NotWhen\Arrayables\Many\Arr;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
8+
use Tests\Fixtures\Many\MixedArrayable;
9+
10+
class MixedTest extends BaseTest
11+
{
12+
protected $value = [
13+
'foo' => 'Foo',
14+
'bar' => 'Bar',
15+
'baz' => [
16+
'foo' => 'Foo',
17+
'bar' => 'Bar',
18+
],
19+
'baq' => [
20+
'foo' => 'Foo',
21+
'bar' => 'Bar',
22+
],
23+
];
24+
25+
public function testGet()
26+
{
27+
$this->assertNull($this->cache()->get());
28+
29+
$this->cache()->put(new MixedArrayable());
30+
31+
$this->assertNull($this->cache()->get());
32+
}
33+
34+
public function testPut()
35+
{
36+
$this->assertSame($this->value, $this->cache()->put(new MixedArrayable()));
37+
38+
$this->assertNull($this->cache()->get());
39+
}
40+
41+
public function testForget()
42+
{
43+
$this->assertNull($this->cache()->get());
44+
45+
$this->cache()->put(new MixedArrayable());
46+
47+
$this->cache()->forget();
48+
49+
$this->assertNull($this->cache()->get());
50+
}
51+
52+
public function testHas()
53+
{
54+
$this->assertFalse($this->cache()->has());
55+
56+
$this->cache()->put(new MixedArrayable());
57+
58+
$this->assertFalse($this->cache()->has());
59+
}
60+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\NotWhen\Arrayables\Many\Files;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
8+
use Tests\Fixtures\Many\DragonCodeArrayable;
9+
10+
class DragonCodeTest extends BaseTest
11+
{
12+
protected $cache = 'file';
13+
14+
protected $value = [
15+
'foo' => 'Foo',
16+
'bar' => 'Bar',
17+
'baz' => [
18+
'foo' => 'Foo',
19+
'bar' => 'Bar',
20+
],
21+
];
22+
23+
public function testGet()
24+
{
25+
$this->assertNull($this->cache()->get());
26+
27+
$this->cache()->put(new DragonCodeArrayable());
28+
29+
$this->assertNull($this->cache()->get());
30+
}
31+
32+
public function testPut()
33+
{
34+
$this->assertSame($this->value, $this->cache()->put(new DragonCodeArrayable()));
35+
36+
$this->assertNull($this->cache()->get());
37+
}
38+
39+
public function testForget()
40+
{
41+
$this->assertNull($this->cache()->get());
42+
43+
$this->cache()->put(new DragonCodeArrayable());
44+
45+
$this->cache()->forget();
46+
47+
$this->assertNull($this->cache()->get());
48+
}
49+
50+
public function testHas()
51+
{
52+
$this->assertFalse($this->cache()->has());
53+
54+
$this->cache()->put(new DragonCodeArrayable());
55+
56+
$this->assertFalse($this->cache()->has());
57+
}
58+
}

0 commit comments

Comments
 (0)