forked from leftypol/leftypol
FsCacheDriver.php: collect expired cache items before operating on the cache
This commit is contained in:
parent
c31f5a4104
commit
39683db736
1 changed files with 8 additions and 3 deletions
|
@ -30,7 +30,10 @@ class FsCacheDriver implements CacheDriver {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectImpl(): int {
|
private function collectImpl(): int {
|
||||||
// A read lock is ok, since it's alright if we delete expired items from under the feet of other processes.
|
/*
|
||||||
|
* A read lock is ok, since it's alright if we delete expired items from under the feet of other processes, and
|
||||||
|
* no other process add new cache items or refresh existing ones.
|
||||||
|
*/
|
||||||
$files = \glob($this->base_path . $this->prefix . '*', \GLOB_NOSORT);
|
$files = \glob($this->base_path . $this->prefix . '*', \GLOB_NOSORT);
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
@ -84,6 +87,9 @@ class FsCacheDriver implements CacheDriver {
|
||||||
|
|
||||||
$this->sharedLockCache();
|
$this->sharedLockCache();
|
||||||
|
|
||||||
|
// Collect expired items first so if the target key is expired we shortcut to failure in the next lines.
|
||||||
|
$this->maybeCollect();
|
||||||
|
|
||||||
$fd = \fopen($this->base_path . $key, 'r');
|
$fd = \fopen($this->base_path . $key, 'r');
|
||||||
if ($fd === false) {
|
if ($fd === false) {
|
||||||
$this->unlockCache();
|
$this->unlockCache();
|
||||||
|
@ -92,7 +98,6 @@ class FsCacheDriver implements CacheDriver {
|
||||||
|
|
||||||
$data = \stream_get_contents($fd);
|
$data = \stream_get_contents($fd);
|
||||||
\fclose($fd);
|
\fclose($fd);
|
||||||
$this->maybeCollect();
|
|
||||||
$this->unlockCache();
|
$this->unlockCache();
|
||||||
$wrapped = \json_decode($data, true, 512, \JSON_THROW_ON_ERROR);
|
$wrapped = \json_decode($data, true, 512, \JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
@ -114,8 +119,8 @@ class FsCacheDriver implements CacheDriver {
|
||||||
|
|
||||||
$data = \json_encode($wrapped);
|
$data = \json_encode($wrapped);
|
||||||
$this->exclusiveLockCache();
|
$this->exclusiveLockCache();
|
||||||
\file_put_contents($this->base_path . $key, $data);
|
|
||||||
$this->maybeCollect();
|
$this->maybeCollect();
|
||||||
|
\file_put_contents($this->base_path . $key, $data);
|
||||||
$this->unlockCache();
|
$this->unlockCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue