Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1951f90

Browse files
Added newflexible() method
1 parentb59e021 commit1951f90

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

‎src/Services/Cache.php‎

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

2929
protectedint$ttl =86400;
3030

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

3335
protectedmixed$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+
publicfunctionflexible(int$interval,bool$isMinutes =true):Cache
120+
{
121+
$this->ttlInterval =$isMinutes ?$interval *60 :$interval;
122+
123+
return$this;
124+
}
125+
91126
publicfunctionget():mixed
92127
{
93128
return$this->manager()->get($this->getKey());
@@ -100,7 +135,9 @@ public function put(mixed $value): mixed
100135

101136
publicfunctionremember(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
publicfunctionrememberForever(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+
publicfunctionflexible(string$key,$value,int$seconds,int$interval):mixed
20+
{
21+
return$this->get($key,$value);
22+
}
23+
1924
publicfunctionremember(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+
publicfunctionflexible(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
publicfunctionremember(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+
abstractpublicfunctionflexible(string$key,$value,int$seconds,int$interval):mixed;
16+
17+
abstractpublicfunctionflush():void;
18+
19+
abstractpublicfunctionforget(string$key):void;
20+
21+
abstractpublicfunctionget(string$key,$default =null):mixed;
22+
23+
abstractpublicfunctionhas(string$key):bool;
24+
25+
abstractpublicfunctionput(string$key,$value,int$seconds):mixed;
26+
27+
abstractpublicfunctionremember(string$key,$value,int$seconds):mixed;
28+
29+
abstractpublicfunctionrememberForever(string$key,$value):mixed;
30+
1531
publicfunctiondoesntHave(string$key):bool
1632
{
1733
return !$this->has($key);

‎src/Services/Storages/TaggedStore.php‎

Lines changed: 7 additions & 0 deletions
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+
publicfunctionflexible(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
publicfunctionremember(string$key,$value,int$seconds):mixed
4047
{
4148
$value =$this->makeCallable($value);

‎src/Support/CacheManager.php‎

Lines changed: 9 additions & 0 deletions
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+
publicfunctionflexible(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
publicfunctionremember(string$key,$value,int$seconds):mixed
4756
{
4857
return$this->instance()->remember($key,$value,$seconds);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp