Skip to content

Commit 109a7f6

Browse files
Merge pull request #104 from TheDragonCode/4.x
Added new `flexible()` method
2 parents 9d23621 + 82d4862 commit 109a7f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+907
-16
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ For example, `App\Models\Employee`, `App\Models\User`.
176176
```php
177177
use DragonCode\Cache\Services\Cache;
178178

179-
$cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']);
179+
$cache = Cache::make()
180+
->key('foo', 'bar', ['baz', 'baq'])
181+
->ttl(200, true);
182+
// When `true` is equal to 200 minutes.
183+
// When `false` is equal to 200 seconds.
184+
// By default, `true`
180185

181186
$cache->put(static fn() => 'Some value');
182187
// or
@@ -193,6 +198,21 @@ $cache->rememberForever(static fn() => 'Some value');
193198
$cache->rememberForever('Some value');
194199
// Contains cached `Some value`
195200

201+
// Uses the functionality of the `Cache::flexible()` method
202+
$cache->flexible(50)->remember('Some value');
203+
// equals `Cache::flexible($key, [50, 200], fn () => 'Some value')`
204+
205+
$cache->flexible(-50)->remember('Some value');
206+
// equals `Cache::flexible($key, [150, 200], fn () => 'Some value')`
207+
208+
$cache->flexible(0)->remember('Some value'); // By default, `0`
209+
// equals `Cache::flexible($key, [170, 200], fn () => 'Some value')`
210+
// (200 - 15%) = 170
211+
212+
$cache->flexible(50, true); // 50 minutes
213+
$cache->flexible(50, false); // 50 seconds
214+
$cache->flexible(0, false); // the `true/false` modifier is not used
215+
196216
$cache->get();
197217
// Returns cached `Some value`
198218

@@ -232,6 +252,9 @@ $cache->rememberForever(static fn() => $user);
232252
$cache->rememberForever($user);
233253
// Contains cached `$user`
234254

255+
$cache->flexible()->remember($user);
256+
// Returns User model with flexibility
257+
235258
$cache->get();
236259
// Returns User model
237260

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"require": {
3636
"php": "^8.2",
3737
"dragon-code/support": "^6.11.3",
38-
"illuminate/contracts": "^11.0 || ^12.0",
39-
"illuminate/support": "^11.0 || ^12.0"
38+
"illuminate/contracts": "^11.23 || ^12.0",
39+
"illuminate/support": "^11.23 || ^12.0"
4040
},
4141
"require-dev": {
4242
"nesbot/carbon": "^2.62 || ^3.0",

src/Concerns/Call.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected function call(mixed $callback = null): mixed
1717
return is_callable($callback) && ! $this->isFunction($callback) ? $callback() : $callback;
1818
}
1919

20-
protected function makeCallable($value): callable
20+
protected function makeCallable(mixed $value): callable
2121
{
2222
if (is_callable($value) && ! $this->isFunction($value)) {
2323
return $value;

src/Services/Cache.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Cache
2828

2929
protected int $ttl = 86400;
3030

31+
protected ?int $ttlInterval = null;
32+
3133
protected array $tags = [];
3234

3335
protected mixed $key;
@@ -88,6 +90,44 @@ public function key(...$values): Cache
8890
return $this;
8991
}
9092

93+
/**
94+
* It sets the time for stale of the key for its background update.
95+
*
96+
* > Note:
97+
* >
98+
* > This method works only in tandem with the `remember()` method.
99+
*
100+
* A positive value indicates the lifetime of the key before it stale.
101+
* For example,
102+
* ```
103+
* Cache::flexible('some', [60, 300], fn () => ...)
104+
* ```
105+
*
106+
* A negative value indicates the time the key will expire.
107+
* For example,
108+
* ```
109+
* Cache::flexible('some', [300 - 60, 300], fn () => ...)
110+
* ```
111+
*
112+
* If you specify 0, then 15% of the remaining time will be taken as the interval.
113+
* For example,
114+
* ```
115+
* Cache::flexible('some', [(300 - 15% = 255), 300], fn () => ...)
116+
* ```
117+
*
118+
* By default, `0`.
119+
*
120+
* @see https://laravel.com/docs/12.x/cache#swr
121+
*
122+
* @return $this
123+
*/
124+
public function flexible(int $interval = 0, bool $isMinutes = true): Cache
125+
{
126+
$this->ttlInterval = $isMinutes ? $interval * 60 : $interval;
127+
128+
return $this;
129+
}
130+
91131
public function get(): mixed
92132
{
93133
return $this->manager()->get($this->getKey());
@@ -100,7 +140,9 @@ public function put(mixed $value): mixed
100140

101141
public function remember(mixed $value): mixed
102142
{
103-
return $this->manager()->remember($this->getKey(), $value, $this->ttl);
143+
return $this->ttlInterval === null
144+
? $this->manager()->remember($this->getKey(), $value, $this->ttl)
145+
: $this->manager()->flexible($this->getKey(), $value, $this->ttl, $this->ttlInterval);
104146
}
105147

106148
public function rememberForever(mixed $value): mixed

src/Services/Storages/Disabled.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public function put(string $key, $value, int $seconds): mixed
1616
return $this->get($key, $value);
1717
}
1818

19+
public function flexible(string $key, $value, int $seconds, int $interval): mixed
20+
{
21+
return $this->get($key, $value);
22+
}
23+
1924
public function remember(string $key, $value, int $seconds): mixed
2025
{
2126
return $this->get($key, $value);

src/Services/Storages/MainStore.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public function put(string $key, $value, int $seconds): mixed
2626
return $value;
2727
}
2828

29+
public function flexible(string $key, $value, int $seconds, int $interval): mixed
30+
{
31+
$value = $this->makeCallable($value);
32+
33+
return Cache::flexible($key, [$seconds, $interval], $value);
34+
}
35+
2936
public function remember(string $key, $value, int $seconds): mixed
3037
{
3138
$value = $this->makeCallable($value);

src/Services/Storages/Store.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ abstract class Store
1212
use Call;
1313
use Makeable;
1414

15+
abstract public function flexible(string $key, $value, int $seconds, int $interval): mixed;
16+
17+
abstract public function flush(): void;
18+
19+
abstract public function forget(string $key): void;
20+
21+
abstract public function get(string $key, $default = null): mixed;
22+
23+
abstract public function has(string $key): bool;
24+
25+
abstract public function put(string $key, $value, int $seconds): mixed;
26+
27+
abstract public function remember(string $key, $value, int $seconds): mixed;
28+
29+
abstract public function rememberForever(string $key, $value): mixed;
30+
1531
public function doesntHave(string $key): bool
1632
{
1733
return ! $this->has($key);

src/Services/Storages/TaggedStore.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ public function tags(array $tags): self
2020

2121
public function get(string $key, $default = null): mixed
2222
{
23-
if ($this->has($key)) {
24-
return $this->cache()->get($key);
25-
}
26-
27-
return $this->call($default);
23+
return $this->cache()->get($key)
24+
?? $this->call($default);
2825
}
2926

3027
public function put(string $key, $value, int $seconds): mixed
@@ -36,18 +33,19 @@ public function put(string $key, $value, int $seconds): mixed
3633
return $value;
3734
}
3835

39-
public function remember(string $key, $value, int $seconds): mixed
36+
public function flexible(string $key, $value, int $seconds, int $interval): mixed
4037
{
41-
$value = $this->makeCallable($value);
38+
return $this->cache()->flexible($key, [$interval, $seconds], $this->makeCallable($value));
39+
}
4240

43-
return $this->cache()->remember($key, $seconds, $value);
41+
public function remember(string $key, $value, int $seconds): mixed
42+
{
43+
return $this->cache()->remember($key, $seconds, $this->makeCallable($value));
4444
}
4545

4646
public function rememberForever(string $key, $value): mixed
4747
{
48-
$value = $this->makeCallable($value);
49-
50-
return $this->cache()->rememberForever($key, $value);
48+
return $this->cache()->rememberForever($key, $this->makeCallable($value));
5149
}
5250

5351
public function forget(string $key): void

src/Support/CacheManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ public function put(string $key, $value, int $seconds): mixed
4343
return $this->instance()->put($key, $value, $seconds);
4444
}
4545

46+
public function flexible(string $key, $value, int $seconds, int $interval): mixed
47+
{
48+
if ($interval < 0) {
49+
$interval = $seconds - $interval;
50+
}
51+
elseif ($interval === 0) {
52+
$interval = (int) (300 * 0.85);
53+
}
54+
55+
return $this->instance()->flexible($key, $value, $seconds, $interval);
56+
}
57+
4658
public function remember(string $key, $value, int $seconds): mixed
4759
{
4860
return $this->instance()->remember($key, $value, $seconds);

tests/Cache/NotWhen/Arrayables/Many/Arr/DragonCodeTest.php

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

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

7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use Tests\Cache\NotWhen\Base;
89
use Tests\Fixtures\Many\DragonCodeArrayable;
910

@@ -36,6 +37,20 @@ public function testPut()
3637
$this->assertNull($this->cache()->get());
3738
}
3839

40+
#[DataProvider('booleanData')]
41+
public function testFlexible(bool $isTrue)
42+
{
43+
$item = new DragonCodeArrayable();
44+
45+
$interval = $isTrue
46+
? $this->positiveTtlInterval
47+
: $this->negativeTtlInterval;
48+
49+
$this->assertSame($item, $this->cache()->flexible($interval)->remember($item));
50+
51+
$this->assertNull($this->cache()->get());
52+
}
53+
3954
public function testRemember()
4055
{
4156
$item = new DragonCodeArrayable();

0 commit comments

Comments
 (0)