modifs.mineures sans commentaires
This commit is contained in:
parent
6fcb32bd25
commit
d50932612f
97
src/cache/CacheFile.php
vendored
97
src/cache/CacheFile.php
vendored
@ -25,7 +25,7 @@ class CacheFile extends SharedFile {
|
||||
throw ValueException::invalid_type($source, CacheData::class);
|
||||
}
|
||||
|
||||
function __construct($file, ?array $params=null) {
|
||||
function __construct($file, $data=null, ?array $params=null) {
|
||||
if ($file === null) {
|
||||
$rand = bin2hex(random_bytes(8));
|
||||
$file = path::join(sys_get_temp_dir(), $rand);
|
||||
@ -38,7 +38,7 @@ class CacheFile extends SharedFile {
|
||||
$this->overrideDuration = $params["override_duration"] ?? false;
|
||||
$this->readonly = $params["readonly"] ?? false;
|
||||
$this->cacheNull = $params["cache_null"] ?? false;
|
||||
$data = $params["data"] ?? null;
|
||||
$data ??= $params["data"] ?? null;
|
||||
$this->sources = null;
|
||||
if ($data === null) {
|
||||
$this->source = null;
|
||||
@ -243,51 +243,70 @@ class CacheFile extends SharedFile {
|
||||
return null;
|
||||
}
|
||||
|
||||
function get($data=null, bool $noCache=false) {
|
||||
return $this->action(function () use ($data, $noCache) {
|
||||
$source = $this->getSource($data);
|
||||
$dataname = $source->getName();
|
||||
$datafilename = ".{$this->basename}.{$dataname}".self::EXT;
|
||||
$datafile = path::join($this->basedir, $datafilename);
|
||||
protected function refreshData($data, bool $noCache) {
|
||||
$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 (!$this->readonly && ($updateMetadata || $updateData)) {
|
||||
$this->lockWrite();
|
||||
if ($updateMetadata) {
|
||||
# il faut refaire tout le cache
|
||||
$this->unlinkFiles(true);
|
||||
$this->start = null;
|
||||
$this->duration = 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;
|
||||
}
|
||||
} 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
|
||||
$updateMetadata = $this->shouldUpdate($noCache);
|
||||
$updateData = !array_key_exists($datafilename, $this->datafilenames);
|
||||
if (!$this->readonly && ($updateMetadata || $updateData)) {
|
||||
$this->lockWrite();
|
||||
if ($updateMetadata) {
|
||||
# il faut refaire tout le cache
|
||||
$this->unlinkFiles(true);
|
||||
$this->start = null;
|
||||
$this->duration = 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;
|
||||
}
|
||||
} elseif (file_exists($datafile)) {
|
||||
$data = $this->loadData($datafile);
|
||||
} else {
|
||||
$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);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,7 @@ abstract class AbstractStorageApp extends Application {
|
||||
}
|
||||
}
|
||||
$first = true;
|
||||
$capacitor->each($filter, function ($item, $row) use (&$first) {
|
||||
$capacitor->each($filter, function ($row) use (&$first) {
|
||||
if ($first) $first = false;
|
||||
else echo "---\n";
|
||||
yaml::dump($row);
|
||||
|
@ -87,7 +87,7 @@ class NucacheApp extends Application {
|
||||
switch ($this->action) {
|
||||
case self::ACTION_READ:
|
||||
if ($showSection) msg::section($file);
|
||||
$cache = new CacheFile($file, [
|
||||
$cache = new CacheFile($file, null, [
|
||||
"readonly" => true,
|
||||
"duration" => "INF",
|
||||
"override_duration" => true,
|
||||
@ -96,7 +96,7 @@ class NucacheApp extends Application {
|
||||
break;
|
||||
case self::ACTION_INFOS:
|
||||
if ($showSection) msg::section($file);
|
||||
$cache = new CacheFile($file, [
|
||||
$cache = new CacheFile($file, null, [
|
||||
"readonly" => true,
|
||||
]);
|
||||
yaml::dump($cache->getInfos());
|
||||
|
@ -17,22 +17,22 @@ function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void {
|
||||
//system("rm -f *.cache .*.cache");
|
||||
|
||||
$what = [
|
||||
//"null",
|
||||
"null",
|
||||
"one",
|
||||
//"two",
|
||||
//"three",
|
||||
"two",
|
||||
"three",
|
||||
];
|
||||
$duration = 10;
|
||||
|
||||
if (in_array("null", $what)) {
|
||||
$null = new CacheFile("null", [
|
||||
$null = new CacheFile("null", null, [
|
||||
"duration" => $duration,
|
||||
]);
|
||||
show("null", $null);
|
||||
}
|
||||
|
||||
if (in_array("one", $what)) {
|
||||
$one = new class("one", [
|
||||
$one = new class("one", null, [
|
||||
"duration" => $duration,
|
||||
]) extends CacheFile {
|
||||
protected function compute() {
|
||||
@ -43,11 +43,10 @@ if (in_array("one", $what)) {
|
||||
}
|
||||
|
||||
if (in_array("two", $what)) {
|
||||
$two = new CacheFile("two", [
|
||||
$two = new CacheFile("two", new CacheData(function () {
|
||||
return 2;
|
||||
}), [
|
||||
"duration" => $duration,
|
||||
"data" => new CacheData(function () {
|
||||
return 2;
|
||||
}),
|
||||
]);
|
||||
show("two", $two);
|
||||
}
|
||||
@ -62,12 +61,10 @@ if (in_array("three", $what)) {
|
||||
});
|
||||
|
||||
$three = new CacheFile("three", [
|
||||
"data" => [
|
||||
"data31" => $data31,
|
||||
$data31, # name=data31name
|
||||
"data32" => $data32,
|
||||
$data32, # name=""
|
||||
],
|
||||
"data31" => $data31,
|
||||
$data31, # name=data31name
|
||||
"data32" => $data32,
|
||||
$data32, # name=""
|
||||
]);
|
||||
Txx("three.0=", $three->get("data31"));
|
||||
Txx("three.1=", $three->get("data31name"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user