Skip to content

Commit 1951f90

Browse files
Added new flexible() method
1 parent b59e021 commit 1951f90

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

src/Services/Cache.php

+38-1
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,39 @@ 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+
* @see https://laravel.com/docs/12.x/cache#swr
113+
*
114+
* @param int $interval
115+
* @param bool $isMinutes
116+
*
117+
* @return $this
118+
*/
119+
public function flexible(int $interval, bool $isMinutes = true): Cache
120+
{
121+
$this->ttlInterval = $isMinutes ? $interval * 60 : $interval;
122+
123+
return $this;
124+
}
125+
91126
public function get(): mixed
92127
{
93128
return $this->manager()->get($this->getKey());
@@ -100,7 +135,9 @@ public function put(mixed $value): mixed
100135

101136
public function remember(mixed $value): mixed
102137
{
103-
return $this->manager()->remember($this->getKey(), $value, $this->ttl);
138+
return $this->ttlInterval === null
139+
? $this->manager()->remember($this->getKey(), $value, $this->ttl)
140+
: $this->manager()->flexible($this->getKey(), $value, $this->ttl, $this->ttlInterval);
104141
}
105142

106143
public function rememberForever(mixed $value): mixed

src/Services/Storages/Disabled.php

+5
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

+7
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

+16
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

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public function put(string $key, $value, int $seconds): mixed
3636
return $value;
3737
}
3838

39+
public function flexible(string $key, $value, int $seconds, int $interval): mixed
40+
{
41+
$value = $this->makeCallable($value);
42+
43+
return Cache::flexible($key, [$seconds, $interval], $value);
44+
}
45+
3946
public function remember(string $key, $value, int $seconds): mixed
4047
{
4148
$value = $this->makeCallable($value);

src/Support/CacheManager.php

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ 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+
52+
return $this->instance()->flexible($key, $value, $seconds, $interval);
53+
}
54+
4655
public function remember(string $key, $value, int $seconds): mixed
4756
{
4857
return $this->instance()->remember($key, $value, $seconds);

0 commit comments

Comments
 (0)