Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Prevent DB Cache::get() occur race condition #49031

Merged
Next Next commit
[10.x] Prevent Cache::get() occur race condition
  • Loading branch information
xdevor committed Nov 16, 2023
commit 03a5192de993c1fb053782c7ea4345bd14dd1713
18 changes: 17 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function get($key)
// item from the cache. Then we will return a null value since the cache is
// expired. We will use "Carbon" to make this comparison with the column.
if ($this->currentTime() >= $cache->expiration) {
$this->forget($key);
$this->forgetIfExpired($key);

return;
}
Expand Down Expand Up @@ -316,6 +316,22 @@ public function forget($key)
return true;
}

/**
* Remove an item from the cache if expired.
*
* @param string $key
* @return bool
*/
public function forgetIfExpired($key)
{
$this->table()
->where('key', '=', $this->prefix.$key)
->where('expiration', '<=', $this->getTime())
->delete();

return true;
}

/**
* Remove all items from the cache.
*
Expand Down
Loading