Skip to content

Commit fb77356

Browse files
Small refactoring
1 parent 5893279 commit fb77356

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,19 @@ public function key(...$values): Cache
109109
* Cache::flexible('some', [300 - 60, 300], fn () => ...)
110110
* ```
111111
*
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+
*
112120
* @see https://laravel.com/docs/12.x/cache#swr
113121
*
114122
* @return $this
115123
*/
116-
public function flexible(int $interval, bool $isMinutes = true): Cache
124+
public function flexible(int $interval = 0, bool $isMinutes = true): Cache
117125
{
118126
$this->ttlInterval = $isMinutes ? $interval * 60 : $interval;
119127

src/Services/Storages/TaggedStore.php

Lines changed: 5 additions & 14 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
@@ -38,23 +35,17 @@ public function put(string $key, $value, int $seconds): mixed
3835

3936
public function flexible(string $key, $value, int $seconds, int $interval): mixed
4037
{
41-
$value = $this->makeCallable($value);
42-
43-
return Cache::flexible($key, [$seconds, $interval], $value);
38+
return $this->cache()->flexible($key, [$interval, $seconds], $this->makeCallable($value));
4439
}
4540

4641
public function remember(string $key, $value, int $seconds): mixed
4742
{
48-
$value = $this->makeCallable($value);
49-
50-
return $this->cache()->remember($key, $seconds, $value);
43+
return $this->cache()->remember($key, $seconds, $this->makeCallable($value));
5144
}
5245

5346
public function rememberForever(string $key, $value): mixed
5447
{
55-
$value = $this->makeCallable($value);
56-
57-
return $this->cache()->rememberForever($key, $value);
48+
return $this->cache()->rememberForever($key, $this->makeCallable($value));
5849
}
5950

6051
public function forget(string $key): void

src/Support/CacheManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function flexible(string $key, $value, int $seconds, int $interval): mixe
4848
if ($interval < 0) {
4949
$interval = $seconds - $interval;
5050
}
51+
elseif ($interval === 0) {
52+
$interval = (int) (300 * 0.85);
53+
}
5154

5255
return $this->instance()->flexible($key, $value, $seconds, $interval);
5356
}

0 commit comments

Comments
 (0)