@@ -76,6 +76,11 @@ public function select($query, $bindings = [], $useReadPdo = true)
7676 // We will use the prefix to operate on the cache directly.
7777 $ key = $ this ->cachePrefix .'| ' .$ this ->computedKey ;
7878
79+ // If the user is setting an array, we will steer return the results using "flexible".
80+ if (is_array ($ this ->ttl ) && count ($ this ->ttl ) > 1 && method_exists ($ this ->repository , 'flexible ' )) {
81+ return $ this ->returnResultsUsingFlexible ($ query , $ key , $ bindings , $ useReadPdo );
82+ }
83+
7984 return $ this
8085 ->retrieveLock ($ key )
8186 ->block ($ this ->lockWait , function () use ($ query , $ bindings , $ useReadPdo , $ key ): array {
@@ -84,11 +89,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
8489 if ($ results === null ) {
8590 $ results = $ this ->connection ->select ($ query , $ bindings , $ useReadPdo );
8691
87- if (is_array ($ this ->ttl ) && count ($ this ->ttl ) > 1 && method_exists ($ this ->repository , 'flexible ' )) {
88- $ this ->repository ->flexible ($ key , $ results , $ this ->ttl );
89- } else {
90- $ this ->repository ->put ($ key , $ results , $ this ->ttl );
91- }
92+ $ this ->repository ->put ($ key , $ results , $ this ->ttl );
9293
9394 // If the user added a user key, we will append this computed key to it and save it.
9495 if ($ this ->userKey ) {
@@ -130,7 +131,7 @@ protected function getQueryHash(string $query, array $bindings): string
130131 */
131132 protected function retrieveLock (string $ key ): Lock
132133 {
133- if (! $ this ->lockWait ) {
134+ if (!$ this ->lockWait ) {
134135 return new NoLock ($ key , $ this ->lockWait );
135136 }
136137
@@ -178,8 +179,12 @@ protected function addComputedKeyToUserKey(string $key, ?array $list): void
178179 /**
179180 * Gets the timestamp for the expiration time.
180181 */
181- protected function getTimestamp (DateInterval |DateTimeInterface |int $ expiration ): int
182+ protected function getTimestamp (DateInterval |DateTimeInterface |array | int $ expiration ): int
182183 {
184+ if (is_array ($ expiration )) {
185+ $ expiration = $ expiration [1 ];
186+ }
187+
183188 if ($ expiration instanceof DateTimeInterface) {
184189 return $ expiration ->getTimestamp ();
185190 }
@@ -219,6 +224,23 @@ public function __call($method, $arguments)
219224 return $ this ->connection ->{$ method }(...$ arguments );
220225 }
221226
227+ /**
228+ * Returns the results of the query using stale revalidation.
229+ */
230+ protected function returnResultsUsingFlexible (string $ query , string $ key , array $ bindings , bool $ useReadPdo ): mixed
231+ {
232+ return $ this ->repository
233+ ->flexible ($ key , $ this ->ttl , function () use ($ query , $ bindings , $ key , $ useReadPdo ): mixed {
234+ $ results = $ this ->connection ->select ($ query , $ bindings , $ useReadPdo );
235+
236+ if ($ this ->userKey ) {
237+ $ this ->addComputedKeyToUserKey ($ key , $ this ->repository ->get ($ this ->userKey ));
238+ }
239+
240+ return $ results ;
241+ });
242+ }
243+
222244 /**
223245 * Create a new CacheAwareProxy instance.
224246 */
@@ -247,7 +269,7 @@ protected static function store(?string $store, bool $lockable): Repository
247269 {
248270 $ repository = cache ()->store ($ store ?? config ('cache-query.store ' ));
249271
250- if ($ lockable && ! $ repository ->getStore () instanceof LockProvider) {
272+ if ($ lockable && !$ repository ->getStore () instanceof LockProvider) {
251273 $ store ??= cache ()->getDefaultDriver ();
252274
253275 throw new LogicException ("The [ $ store] cache does not support atomic locks. " );
0 commit comments