From 66bd7ccf12bb2197249691d690e8f29c2442cfcb Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 23 May 2025 21:20:29 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/{db => }/cache/CacheChannel.php | 2 +- src/{file => }/cache/CacheData.php | 2 +- .../CacheDataChannel.php} | 8 +- src/{file => }/cache/CacheFile.php | 96 +++++++++++-------- .../cache.php => cache/storage_cache.php} | 10 +- src/tools/NucacheApp.php | 11 ++- tbin/test-nucache.php | 36 ++++--- 7 files changed, 102 insertions(+), 63 deletions(-) rename src/{db => }/cache/CacheChannel.php (99%) rename src/{file => }/cache/CacheData.php (97%) rename src/{db/cache/RowsChannel.php => cache/CacheDataChannel.php} (86%) rename src/{file => }/cache/CacheFile.php (80%) rename src/{db/cache/cache.php => cache/storage_cache.php} (75%) diff --git a/src/db/cache/CacheChannel.php b/src/cache/CacheChannel.php similarity index 99% rename from src/db/cache/CacheChannel.php rename to src/cache/CacheChannel.php index b1f8619..f222b09 100644 --- a/src/db/cache/CacheChannel.php +++ b/src/cache/CacheChannel.php @@ -1,5 +1,5 @@ "varchar(128) primary key not null", "all_values" => "mediumtext", @@ -38,7 +40,7 @@ class RowsChannel extends CapacitorChannel implements IteratorAggregate { } function getIterator(): Traversable { - $cm = cache::get(); + $cm = storage_cache::get(); if ($cm->shouldUpdate($this->cacheIds)) { $this->capacitor->reset(); foreach (($this->builder)() as $key => $row) { diff --git a/src/file/cache/CacheFile.php b/src/cache/CacheFile.php similarity index 80% rename from src/file/cache/CacheFile.php rename to src/cache/CacheFile.php index 1a47182..3feb150 100644 --- a/src/file/cache/CacheFile.php +++ b/src/cache/CacheFile.php @@ -1,12 +1,12 @@ basedir = path::dirname($file); @@ -32,34 +39,28 @@ class CacheFile extends SharedFile { $this->readonly = $params["readonly"] ?? false; $this->cacheNull = $params["cache_null"] ?? false; $data = $params["data"] ?? null; + $this->sources = null; if ($data === null) { - $this->dataDefs = null; - $this->data = null; + $this->source = null; } elseif ($data instanceof CacheData) { - $this->dataDefs = null; - $this->data = $data; + $this->source = $data; + } elseif (func::is_callable($data)) { + $this->source = new CacheData($data); } elseif (!is_array($data)) { - $this->dataDefs = null; - $this->data = $this->getData($data); + $this->source = self::ensure_source($data); } else { - $dataDefs = []; - $tmpdefs = $data; + $sources = []; $index = 0; - foreach ($tmpdefs as $key => $data) { - $odef = $data; - if ($data instanceof CacheData) { - } elseif (cv::subclass_of($data, CacheData::class)) { - $data = new $data(); - } else { - throw ValueException::invalid_type($odef, CacheData::class); - } + foreach ($data as $key => $source) { + $source = self::ensure_source($source); if ($key === $index) { $index++; - $key = $data->getName(); + $key = $source->getName(); } - $dataDefs[$key] = $data; + $sources[$key] = $source; } - $this->dataDefs = $dataDefs; + $this->sources = $sources; + $this->source = null; } parent::__construct($file); } @@ -78,23 +79,23 @@ class CacheFile extends SharedFile { protected bool $cacheNull; - protected ?array $dataDefs; + protected ?array $sources; - protected ?CacheData $data; + protected ?CacheData $source; - protected function getData($data): CacheData { + protected function getSource($data): CacheData { if ($data === null) { - return $this->data ??= new CacheData(function() { + return $this->source ??= new CacheData(function() { return $this->compute(); }); } - $odata = $data; - if (is_string($data) || is_int($data)) { - $data = $this->dataDefs[$data] ?? null; - if ($data === null) throw ValueException::invalid_key($odata); + $source = $data; + if (is_string($source) || is_int($source)) { + $source = $this->sources[$source] ?? null; + if ($source === null) throw ValueException::invalid_key($data); } - if ($data instanceof CacheData) return $data; - throw ValueException::invalid_type($odata, CacheData::class); + if ($source instanceof CacheData) return $source; + throw ValueException::invalid_type($data, CacheData::class); } /** @@ -102,7 +103,7 @@ class CacheFile extends SharedFile { * * on assume que le fichier existe, vu qu'il a été ouvert en c+b */ - protected function isValid(): bool { + function isValid(): bool { # considèrer que le fichier est invalide s'il est de taille nulle return $this->getSize() > 0; } @@ -244,8 +245,8 @@ class CacheFile extends SharedFile { function get($data=null, bool $noCache=false) { return $this->action(function () use ($data, $noCache) { - $data = $this->getData($data); - $dataname = $data->getName(); + $source = $this->getSource($data); + $dataname = $source->getName(); $datafilename = ".{$this->basename}.{$dataname}".self::EXT; $datafile = path::join($this->basedir, $datafilename); @@ -256,12 +257,14 @@ class CacheFile extends SharedFile { 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 = $data->get(); + $data = $source->get(); } catch (Exception $e) { # ne pas garder le fichier en cas d'exception $this->unlinkDatafile($datafile); @@ -288,6 +291,19 @@ class CacheFile extends SharedFile { }); } + function all($data=null, bool $noCache=false): ?iterable { + $data = $this->get($data, $noCache); + if ($data !== null && !is_iterable($data)) $data = [$data]; + return $data; + } + + function delete($data=null): void { + $source = $this->getSource($data); + $dataname = $source->getName(); + $datafilename = ".{$this->basename}.{$dataname}".self::EXT; + $this->unlinkDatafile($datafilename); + } + /** obtenir les informations sur le fichier */ function getInfos(): array { return $this->action(function () { @@ -323,8 +339,12 @@ class CacheFile extends SharedFile { const UPDATE_SUB = -1, UPDATE_SET = 0, UPDATE_ADD = 1; - /** mettre à jour la durée de validité du fichier */ - function updateDuration($nduration, int $action=1): void { + /** + * mettre à jour la durée de validité du fichier + * + * XXX UPDATE_SET n'est pas implémenté + */ + function updateDuration($nduration, int $action=self::UPDATE_ADD): void { if ($this->readonly) return; $this->action(function () use ($nduration, $action) { if (!$this->isValid()) return; diff --git a/src/db/cache/cache.php b/src/cache/storage_cache.php similarity index 75% rename from src/db/cache/cache.php rename to src/cache/storage_cache.php index 401fb19..1fc6814 100644 --- a/src/db/cache/cache.php +++ b/src/cache/storage_cache.php @@ -1,11 +1,13 @@ [null, "->setActionUpdate", self::ACTION_UPDATE_SUB], "help" => "Enlever le nombre de secondes spécifié à la durée du cache", ], - ["-s", "--set-duration", "args" => 1, - "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SET], - "help" => "Mettre à jour la durée du cache à la valeur spécifiée", - ], + #XXX pas encore implémenté + //["-s", "--set-duration", "args" => 1, + // "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SET], + // "help" => "Mettre à jour la durée du cache à la valeur spécifiée", + //], ]; protected $action = self::ACTION_READ; diff --git a/tbin/test-nucache.php b/tbin/test-nucache.php index ddaf641..802926f 100755 --- a/tbin/test-nucache.php +++ b/tbin/test-nucache.php @@ -2,40 +2,54 @@ get()); + if ($dumpInfos) { + yaml::dump($cache->getInfos()); + } +} + //system("rm -f *.cache .*.cache"); $what = [ - "null", + //"null", "one", - "two", - "three", + //"two", + //"three", ]; +$duration = 10; if (in_array("null", $what)) { - $null = new CacheFile("null"); - Txx("null=", $null->get()); + $null = new CacheFile("null", [ + "duration" => $duration, + ]); + show("null", $null); } if (in_array("one", $what)) { - $one = new class("one") extends CacheFile { + $one = new class("one", [ + "duration" => $duration, + ]) extends CacheFile { protected function compute() { return 1; } }; - Txx("one=", $one->get()); + show("one", $one); } if (in_array("two", $what)) { $two = new CacheFile("two", [ + "duration" => $duration, "data" => new CacheData(function () { return 2; }), ]); - Txx("two=", $two->get()); + show("two", $two); } if (in_array("three", $what)) { @@ -59,4 +73,4 @@ if (in_array("three", $what)) { Txx("three.1=", $three->get("data31name")); Txx("three.2=", $three->get("data32")); Txx("three.3=", $three->get("")); -} \ No newline at end of file +}