Skip to content

Commit 4a6e27a

Browse files
author
Andrey Helldar
authored
Merge pull request #10 from TheDragonCode/2.x
[2.x] Added the ability to pass non-callable values
2 parents 5a091cc + 503ef7b commit 4a6e27a

24 files changed

+545
-31
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ use DragonCode\Cache\Services\Cache;
3939
$cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']);
4040

4141
$cache->put(static fn() => 'Some value');
42+
// or
43+
$cache->put('Some value');
4244
// Contains cached `Some value`
4345

4446
$cache->get();
@@ -63,6 +65,8 @@ $cache = Cache::make()
6365
->key('foo', 'bar', ['baz', 'baq']);
6466

6567
$cache->put(static fn() => 'Some value');
68+
// or
69+
$cache->put('Some value');
6670
// Contains cached `Some value`
6771

6872
$cache->get();
@@ -87,6 +91,8 @@ $cache = Cache::make()
8791
->key('foo', 'bar', ['baz', 'baq']);
8892

8993
$cache->put(static fn() => 'Some value');
94+
// or
95+
$cache->put('Some value');
9096
// Contains cached `Some value`
9197

9298
$cache->get();
@@ -107,6 +113,8 @@ use DragonCode\Cache\Services\Cache;
107113
$cache = Cache::make()->key('foo', 'bar');
108114

109115
$cache->tags('actor', 'author')->put(static fn() => 'Some value');
116+
// or
117+
$cache->tags('actor', 'author')->put('Some value');
110118
// Contains cached `Some value`
111119

112120
$cache->tags('actor', 'author')->get();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"require": {
2525
"php": "^7.3|^8.0",
26-
"dragon-code/contracts": "^2.3.5",
26+
"dragon-code/contracts": "^2.5.1",
2727
"dragon-code/support": "^5.0",
2828
"illuminate/support": "^6.0|^7.0|^8.0"
2929
},

src/Concerns/Call.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Cache\Concerns;
6+
7+
trait Call
8+
{
9+
/**
10+
* @param mixed $callback
11+
*
12+
* @return mixed
13+
*/
14+
protected function call($callback = null)
15+
{
16+
return is_callable($callback) ? $callback() : $callback;
17+
}
18+
19+
protected function makeCallable($value): callable
20+
{
21+
if (! is_callable($value)) {
22+
return function () use ($value) {
23+
return $value;
24+
};
25+
}
26+
27+
return $value;
28+
}
29+
}

src/Services/Cache.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DragonCode\Cache\Services;
66

7+
use DragonCode\Cache\Concerns\Call;
78
use DragonCode\Cache\Facades\Support\CacheManager;
89
use DragonCode\Cache\Facades\Support\Key;
910
use DragonCode\Cache\Facades\Support\Tag;
@@ -15,6 +16,7 @@
1516
*/
1617
class Cache
1718
{
19+
use Call;
1820
use Makeable;
1921

2022
protected $ttl = 86400;
@@ -62,13 +64,18 @@ public function get()
6264
return null;
6365
}
6466

65-
public function put(callable $callback)
67+
/**
68+
* @param mixed $value
69+
*
70+
* @return mixed
71+
*/
72+
public function put($value)
6673
{
6774
if ($this->when) {
68-
return $this->manager()->put($this->key, $callback, $this->ttl);
75+
return $this->manager()->put($this->key, $value, $this->ttl);
6976
}
7077

71-
return $callback();
78+
return $this->call($value);
7279
}
7380

7481
public function forget(): void

src/Services/Storages/MainStore.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class MainStore extends Store
1010
{
11-
public function get(string $key, callable $default = null)
11+
public function get(string $key, $default = null)
1212
{
1313
if ($this->has($key)) {
1414
return Cache::get($key);
@@ -17,9 +17,11 @@ public function get(string $key, callable $default = null)
1717
return $this->call($default);
1818
}
1919

20-
public function put(string $key, callable $callback, int $seconds)
20+
public function put(string $key, $value, int $seconds)
2121
{
22-
return Cache::remember($key, $seconds, $callback);
22+
$value = $this->makeCallable($value);
23+
24+
return Cache::remember($key, $seconds, $value);
2325
}
2426

2527
public function forget(string $key): void

src/Services/Storages/Store.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44

55
namespace DragonCode\Cache\Services\Storages;
66

7+
use DragonCode\Cache\Concerns\Call;
78
use DragonCode\Contracts\Cache\Store as BaseStore;
89
use DragonCode\Support\Concerns\Makeable;
910

1011
abstract class Store implements BaseStore
1112
{
13+
use Call;
1214
use Makeable;
13-
14-
/**
15-
* @param callable|null $callback
16-
*
17-
* @return mixed
18-
*/
19-
protected function call(callable $callback = null)
20-
{
21-
return is_callable($callback) ? $callback() : $callback;
22-
}
2315
}

src/Services/Storages/TaggedStore.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function tags(array $tags): self
1818
return $this;
1919
}
2020

21-
public function get(string $key, callable $default = null)
21+
public function get(string $key, $default = null)
2222
{
2323
if ($this->has($key)) {
2424
return $this->cache()->get($key);
@@ -27,9 +27,11 @@ public function get(string $key, callable $default = null)
2727
return $this->call($default);
2828
}
2929

30-
public function put(string $key, callable $callback, int $seconds)
30+
public function put(string $key, $value, int $seconds)
3131
{
32-
return $this->cache()->remember($key, $seconds, $callback);
32+
$value = $this->makeCallable($value);
33+
34+
return $this->cache()->remember($key, $seconds, $value);
3335
}
3436

3537
public function forget(string $key): void

src/Support/CacheManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public function tags(array $tags): CacheManager
2020
return $this;
2121
}
2222

23-
public function get(string $key, callable $default = null)
23+
public function get(string $key, $default = null)
2424
{
2525
return $this->instance()->get($key, $default);
2626
}
2727

28-
public function put(string $key, callable $callback, int $seconds)
28+
public function put(string $key, $value, int $seconds)
2929
{
30-
return $this->instance()->put($key, $callback, $seconds);
30+
return $this->instance()->put($key, $value, $seconds);
3131
}
3232

3333
public function forget(string $key): void

tests/Cache/NotWhen/ArrayTest.php renamed to tests/Cache/NotWhen/Callables/ArrayTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Cache\NotWhen;
5+
namespace Tests\Cache\NotWhen\Callables;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
68

79
class ArrayTest extends BaseTest
810
{

tests/Cache/NotWhen/FileTest.php renamed to tests/Cache/NotWhen/Callables/FileTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Cache\NotWhen;
5+
namespace Tests\Cache\NotWhen\Callables;
6+
7+
use Tests\Cache\NotWhen\BaseTest;
68

79
class FileTest extends BaseTest
810
{

0 commit comments

Comments
 (0)