modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-06-28 14:40:49 +04:00
parent 6fcb32bd25
commit d50932612f
4 changed files with 73 additions and 57 deletions

View File

@ -25,7 +25,7 @@ class CacheFile extends SharedFile {
throw ValueException::invalid_type($source, CacheData::class); throw ValueException::invalid_type($source, CacheData::class);
} }
function __construct($file, ?array $params=null) { function __construct($file, $data=null, ?array $params=null) {
if ($file === null) { if ($file === null) {
$rand = bin2hex(random_bytes(8)); $rand = bin2hex(random_bytes(8));
$file = path::join(sys_get_temp_dir(), $rand); $file = path::join(sys_get_temp_dir(), $rand);
@ -38,7 +38,7 @@ class CacheFile extends SharedFile {
$this->overrideDuration = $params["override_duration"] ?? false; $this->overrideDuration = $params["override_duration"] ?? false;
$this->readonly = $params["readonly"] ?? false; $this->readonly = $params["readonly"] ?? false;
$this->cacheNull = $params["cache_null"] ?? false; $this->cacheNull = $params["cache_null"] ?? false;
$data = $params["data"] ?? null; $data ??= $params["data"] ?? null;
$this->sources = null; $this->sources = null;
if ($data === null) { if ($data === null) {
$this->source = null; $this->source = null;
@ -243,51 +243,70 @@ class CacheFile extends SharedFile {
return null; return null;
} }
function get($data=null, bool $noCache=false) { protected function refreshData($data, bool $noCache) {
return $this->action(function () use ($data, $noCache) { $source = $this->getSource($data);
$source = $this->getSource($data); $dataname = $source->getName();
$dataname = $source->getName(); $datafilename = ".{$this->basename}.{$dataname}".self::EXT;
$datafilename = ".{$this->basename}.{$dataname}".self::EXT; $datafile = path::join($this->basedir, $datafilename);
$datafile = path::join($this->basedir, $datafilename);
$updateMetadata = $this->shouldUpdate($noCache); $updateMetadata = $this->shouldUpdate($noCache);
$updateData = !array_key_exists($datafilename, $this->datafilenames); $updateData = !array_key_exists($datafilename, $this->datafilenames);
if (!$this->readonly && ($updateMetadata || $updateData)) { if (!$this->readonly && ($updateMetadata || $updateData)) {
$this->lockWrite(); $this->lockWrite();
if ($updateMetadata) { if ($updateMetadata) {
# il faut refaire tout le cache # il faut refaire tout le cache
$this->unlinkFiles(true); $this->unlinkFiles(true);
$this->start = null; $this->start = null;
$this->duration = null; $this->duration = null;
$updateData = true; $updateData = true;
} }
if ($updateData) { if ($updateData) {
# calculer un fichier # calculer un fichier
try { try {
$data = $source->get(); $data = $source->get();
} catch (Exception $e) { } catch (Exception $e) {
# ne pas garder le fichier en cas d'exception # ne pas garder le fichier en cas d'exception
$this->unlinkDatafile($datafile);
throw $e;
}
} elseif (file_exists($datafile)) {
$data = $this->loadData($datafile);
} 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); $this->unlinkDatafile($datafile);
throw $e;
} }
} elseif (file_exists($datafile)) { } elseif (file_exists($datafile)) {
$data = $this->loadData($datafile); $data = $this->loadData($datafile);
} else { } else {
$data = null; $data = null;
} }
return $data; 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);
}
} elseif (file_exists($datafile)) {
$data = $this->loadData($datafile);
} else {
$data = null;
}
return $data;
}
/**
* s'assurer que le cache est à jour avec les données les plus récentes. si
* les données sont déjà présentes dans le cache et n'ont pas encore expirées
* 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);
}
return $this;
}
function get($data=null, bool $noCache=false) {
return $this->action(function () use ($data, $noCache) {
return $this->refreshData($data, $noCache);
}); });
} }

View File

@ -98,7 +98,7 @@ abstract class AbstractStorageApp extends Application {
} }
} }
$first = true; $first = true;
$capacitor->each($filter, function ($item, $row) use (&$first) { $capacitor->each($filter, function ($row) use (&$first) {
if ($first) $first = false; if ($first) $first = false;
else echo "---\n"; else echo "---\n";
yaml::dump($row); yaml::dump($row);

View File

@ -87,7 +87,7 @@ class NucacheApp extends Application {
switch ($this->action) { switch ($this->action) {
case self::ACTION_READ: case self::ACTION_READ:
if ($showSection) msg::section($file); if ($showSection) msg::section($file);
$cache = new CacheFile($file, [ $cache = new CacheFile($file, null, [
"readonly" => true, "readonly" => true,
"duration" => "INF", "duration" => "INF",
"override_duration" => true, "override_duration" => true,
@ -96,7 +96,7 @@ class NucacheApp extends Application {
break; break;
case self::ACTION_INFOS: case self::ACTION_INFOS:
if ($showSection) msg::section($file); if ($showSection) msg::section($file);
$cache = new CacheFile($file, [ $cache = new CacheFile($file, null, [
"readonly" => true, "readonly" => true,
]); ]);
yaml::dump($cache->getInfos()); yaml::dump($cache->getInfos());

View File

@ -17,22 +17,22 @@ function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void {
//system("rm -f *.cache .*.cache"); //system("rm -f *.cache .*.cache");
$what = [ $what = [
//"null", "null",
"one", "one",
//"two", "two",
//"three", "three",
]; ];
$duration = 10; $duration = 10;
if (in_array("null", $what)) { if (in_array("null", $what)) {
$null = new CacheFile("null", [ $null = new CacheFile("null", null, [
"duration" => $duration, "duration" => $duration,
]); ]);
show("null", $null); show("null", $null);
} }
if (in_array("one", $what)) { if (in_array("one", $what)) {
$one = new class("one", [ $one = new class("one", null, [
"duration" => $duration, "duration" => $duration,
]) extends CacheFile { ]) extends CacheFile {
protected function compute() { protected function compute() {
@ -43,11 +43,10 @@ if (in_array("one", $what)) {
} }
if (in_array("two", $what)) { if (in_array("two", $what)) {
$two = new CacheFile("two", [ $two = new CacheFile("two", new CacheData(function () {
return 2;
}), [
"duration" => $duration, "duration" => $duration,
"data" => new CacheData(function () {
return 2;
}),
]); ]);
show("two", $two); show("two", $two);
} }
@ -62,12 +61,10 @@ if (in_array("three", $what)) {
}); });
$three = new CacheFile("three", [ $three = new CacheFile("three", [
"data" => [ "data31" => $data31,
"data31" => $data31, $data31, # name=data31name
$data31, # name=data31name "data32" => $data32,
"data32" => $data32, $data32, # name=""
$data32, # name=""
],
]); ]);
Txx("three.0=", $three->get("data31")); Txx("three.0=", $three->get("data31"));
Txx("three.1=", $three->get("data31name")); Txx("three.1=", $three->get("data31name"));