modifs.mineures sans commentaires
This commit is contained in:
parent
79b8c33d4f
commit
8612a9dae4
108
src/cache/CacheFile.php
vendored
108
src/cache/CacheFile.php
vendored
@ -110,6 +110,8 @@ class CacheFile extends SharedFile {
|
||||
|
||||
protected ?Delay $duration;
|
||||
|
||||
protected $data;
|
||||
|
||||
protected ?array $datafilenames;
|
||||
|
||||
/** charger les données. le fichier a déjà été verrouillé en lecture */
|
||||
@ -119,6 +121,7 @@ class CacheFile extends SharedFile {
|
||||
[
|
||||
"start" => $start,
|
||||
"duration" => $duration,
|
||||
"data" => $data,
|
||||
"datafiles" => $datafilenames,
|
||||
] = $this->unserialize(null, false, true);
|
||||
if ($this->overrideDuration) {
|
||||
@ -128,10 +131,12 @@ class CacheFile extends SharedFile {
|
||||
} else {
|
||||
$start = null;
|
||||
$duration = null;
|
||||
$data = null;
|
||||
$datafilenames = [];
|
||||
}
|
||||
$this->start = $start;
|
||||
$this->duration = $duration;
|
||||
$this->data = $data;
|
||||
$this->datafilenames = $datafilenames;
|
||||
}
|
||||
|
||||
@ -161,15 +166,16 @@ class CacheFile extends SharedFile {
|
||||
$this->serialize([
|
||||
"start" => $this->start,
|
||||
"duration" => $this->duration,
|
||||
"data" => $this->data,
|
||||
"datafiles" => $datafilenames,
|
||||
], false, true);
|
||||
}
|
||||
|
||||
protected function loadData(string $datafile) {
|
||||
protected function loadDatafile(string $datafile) {
|
||||
return file::reader($datafile)->unserialize();
|
||||
}
|
||||
|
||||
protected function saveData(string $datafile, $data): void {
|
||||
protected function saveDatafile(string $datafile, $data): void {
|
||||
file::writer($datafile)->serialize($data);
|
||||
}
|
||||
|
||||
@ -180,10 +186,10 @@ class CacheFile extends SharedFile {
|
||||
}
|
||||
|
||||
protected function unlinkFiles(bool $datafilesOnly=false): void {
|
||||
if (!$datafilesOnly) @unlink($this->file);
|
||||
foreach ($this->datafilenames as $datafilename) {
|
||||
$this->unlinkDatafile($datafilename);
|
||||
}
|
||||
if (!$datafilesOnly) @unlink($this->file);
|
||||
}
|
||||
|
||||
/** tester si $value peut être mis en cache */
|
||||
@ -195,26 +201,28 @@ class CacheFile extends SharedFile {
|
||||
|
||||
protected ?Delay $oduration;
|
||||
|
||||
protected $odata;
|
||||
|
||||
protected ?array $odatafilenames;
|
||||
|
||||
protected function beforeAction() {
|
||||
$this->loadMetadata();
|
||||
$this->ostart = cv::clone($this->start);
|
||||
$this->oduration = cv::clone($this->duration);
|
||||
$this->odata = cv::clone($this->data);
|
||||
$this->odatafilenames = cv::clone($this->datafilenames);
|
||||
}
|
||||
|
||||
protected function afterAction() {
|
||||
$start = $this->start;
|
||||
$modified = false;
|
||||
if ($this->start != $this->ostart) $modified = true;
|
||||
$duration = $this->duration;
|
||||
$oduration = $this->oduration;
|
||||
$datafilenames = $this->datafilenames;
|
||||
$modified = false;
|
||||
if ($start != $this->ostart) $modified = true;
|
||||
if ($duration === null || $oduration === null) $modified = true;
|
||||
elseif ($duration->getDest() != $oduration->getDest()) $modified = true;
|
||||
# égalité stricte uniquement pour $datafiles qui est un array
|
||||
if ($datafilenames !== $this->odatafilenames) $modified = true;
|
||||
# égalité stricte uniquement pour $data et $datafiles
|
||||
if ($this->data !== $this->odata) $modified = true;
|
||||
if ($this->datafilenames !== $this->odatafilenames) $modified = true;
|
||||
if ($modified && !$this->readonly) {
|
||||
$this->lockWrite();
|
||||
$this->saveMetadata();
|
||||
@ -230,8 +238,13 @@ class CacheFile extends SharedFile {
|
||||
$this->afterAction();
|
||||
return $result;
|
||||
} finally {
|
||||
$this->ostart = null;
|
||||
$this->oduration = null;
|
||||
$this->odata = null;
|
||||
$this->odatafilenames = null;
|
||||
$this->start = null;
|
||||
$this->duration = null;
|
||||
$this->data = null;
|
||||
$this->datafilenames = null;
|
||||
$this->unlock(true);
|
||||
}
|
||||
@ -242,13 +255,15 @@ class CacheFile extends SharedFile {
|
||||
}
|
||||
|
||||
protected function refreshData($data, bool $noCache) {
|
||||
$defaultSource = $data === null;
|
||||
$source = $this->getSource($data);
|
||||
$dataname = $source->getName();
|
||||
$datafilename = ".{$this->basename}.{$dataname}".self::EXT;
|
||||
$datafile = path::join($this->basedir, $datafilename);
|
||||
|
||||
$updateMetadata = $this->shouldUpdate($noCache);
|
||||
$updateData = !array_key_exists($datafilename, $this->datafilenames);
|
||||
if ($defaultSource) $updateData = $this->data === null;
|
||||
else $updateData = !array_key_exists($datafilename, $this->datafilenames);
|
||||
if (!$this->readonly && ($updateMetadata || $updateData)) {
|
||||
$this->lockWrite();
|
||||
if ($updateMetadata) {
|
||||
@ -256,31 +271,52 @@ class CacheFile extends SharedFile {
|
||||
$this->unlinkFiles(true);
|
||||
$this->start = null;
|
||||
$this->duration = null;
|
||||
$this->data = null;
|
||||
$updateData = true;
|
||||
}
|
||||
if ($updateData) {
|
||||
# calculer un fichier
|
||||
try {
|
||||
$data = $source->get();
|
||||
} catch (Exception $e) {
|
||||
# ne pas garder le fichier en cas d'exception
|
||||
$this->unlinkDatafile($datafile);
|
||||
throw $e;
|
||||
if ($defaultSource) {
|
||||
if ($updateData) {
|
||||
# calculer la valeur
|
||||
try {
|
||||
$data = $source->get();
|
||||
} catch (Exception $e) {
|
||||
# le fichier n'est pas mis à jour, mais ce n'est pas gênant: lors
|
||||
# des futurs appels, l'exception continuera d'être lancée ou la
|
||||
# valeur sera finalement mise à jour
|
||||
throw $e;
|
||||
}
|
||||
} else {
|
||||
$data = $this->data;
|
||||
}
|
||||
} elseif (file_exists($datafile)) {
|
||||
$data = $this->loadData($datafile);
|
||||
if ($this->shouldCache($data)) $this->data = $data;
|
||||
else $this->data = $data = null;
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
if ($this->shouldCache($data)) {
|
||||
$this->saveData($datafile, $data);
|
||||
$this->datafilenames[$datafilename] = true;
|
||||
} else {
|
||||
# ne pas garder le fichier s'il ne faut pas mettre en cache
|
||||
$this->unlinkDatafile($datafile);
|
||||
if ($updateData) {
|
||||
# calculer la valeur
|
||||
try {
|
||||
$data = $source->get();
|
||||
} catch (Exception $e) {
|
||||
# ne pas garder le fichier destination en cas d'exception
|
||||
$this->unlinkDatafile($datafile);
|
||||
throw $e;
|
||||
}
|
||||
} elseif (file_exists($datafile)) {
|
||||
$data = $this->loadDatafile($datafile);
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
if ($this->shouldCache($data)) {
|
||||
$this->saveDatafile($datafile, $data);
|
||||
$this->datafilenames[$datafilename] = true;
|
||||
} else {
|
||||
# ne pas garder le fichier s'il ne faut pas mettre en cache
|
||||
$this->unlinkDatafile($datafile);
|
||||
}
|
||||
}
|
||||
} elseif ($defaultSource) {
|
||||
$data = $this->data;
|
||||
} elseif (file_exists($datafile)) {
|
||||
$data = $this->loadData($datafile);
|
||||
$data = $this->loadDatafile($datafile);
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
@ -293,12 +329,14 @@ class CacheFile extends SharedFile {
|
||||
* cette méthode est un NOP
|
||||
*/
|
||||
function refresh(bool $noCache=false): self {
|
||||
$source = $this->source;
|
||||
if ($source !== null) $datas = [null];
|
||||
else $datas = array_keys($this->sources);
|
||||
foreach ($datas as $data) {
|
||||
$this->refreshData($data, $noCache);
|
||||
}
|
||||
$this->action(function() use ($noCache) {
|
||||
$source = $this->source;
|
||||
if ($source !== null) $datas = [null];
|
||||
else $datas = array_keys($this->sources);
|
||||
foreach ($datas as $data) {
|
||||
$this->refreshData($data, $noCache);
|
||||
}
|
||||
});
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
2
src/cache/TODO.md
vendored
2
src/cache/TODO.md
vendored
@ -2,7 +2,5 @@
|
||||
|
||||
* CacheChannel
|
||||
* spécifier des clés supplémentaires utilisable dans la recherche
|
||||
* CacheFile
|
||||
* pour $data===null, stocker dans le fichier de cache directement
|
||||
|
||||
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary
|
Loading…
x
Reference in New Issue
Block a user