@@ -28,6 +28,8 @@ class Cache
28
28
29
29
protected int $ ttl = 86400 ;
30
30
31
+ protected ?int $ ttlInterval = null ;
32
+
31
33
protected array $ tags = [];
32
34
33
35
protected mixed $ key ;
@@ -88,6 +90,39 @@ public function key(...$values): Cache
88
90
return $ this ;
89
91
}
90
92
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
+
91
126
public function get (): mixed
92
127
{
93
128
return $ this ->manager ()->get ($ this ->getKey ());
@@ -100,7 +135,9 @@ public function put(mixed $value): mixed
100
135
101
136
public function remember (mixed $ value ): mixed
102
137
{
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 );
104
141
}
105
142
106
143
public function rememberForever (mixed $ value ): mixed
0 commit comments