modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-05-23 21:20:29 +04:00
parent 3ea51e70b4
commit 66bd7ccf12
7 changed files with 102 additions and 63 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
namespace nulib\db\cache; namespace nulib\cache;
use nulib\cl; use nulib\cl;
use nulib\db\CapacitorChannel; use nulib\db\CapacitorChannel;

View File

@ -1,5 +1,5 @@
<?php <?php
namespace nulib\file\cache; namespace nulib\cache;
use nulib\cl; use nulib\cl;
use nulib\php\func; use nulib\php\func;

View File

@ -1,13 +1,15 @@
<?php <?php
namespace nulib\db\cache; namespace nulib\cache;
use Closure; use Closure;
use IteratorAggregate; use IteratorAggregate;
use nulib\cache\CacheChannel;
use nulib\cache\storage_cache;
use nulib\cl; use nulib\cl;
use nulib\db\CapacitorChannel; use nulib\db\CapacitorChannel;
use Traversable; use Traversable;
class RowsChannel extends CapacitorChannel implements IteratorAggregate { class CacheDataChannel extends CapacitorChannel implements IteratorAggregate {
const COLUMN_DEFINITIONS = [ const COLUMN_DEFINITIONS = [
"key" => "varchar(128) primary key not null", "key" => "varchar(128) primary key not null",
"all_values" => "mediumtext", "all_values" => "mediumtext",
@ -38,7 +40,7 @@ class RowsChannel extends CapacitorChannel implements IteratorAggregate {
} }
function getIterator(): Traversable { function getIterator(): Traversable {
$cm = cache::get(); $cm = storage_cache::get();
if ($cm->shouldUpdate($this->cacheIds)) { if ($cm->shouldUpdate($this->cacheIds)) {
$this->capacitor->reset(); $this->capacitor->reset();
foreach (($this->builder)() as $key => $row) { foreach (($this->builder)() as $key => $row) {

View File

@ -1,12 +1,12 @@
<?php <?php
namespace nulib\file\cache; namespace nulib\cache;
use Exception; use Exception;
use nulib\cl;
use nulib\cv; use nulib\cv;
use nulib\file; use nulib\file;
use nulib\file\SharedFile; use nulib\file\SharedFile;
use nulib\os\path; use nulib\os\path;
use nulib\php\func;
use nulib\php\time\DateTime; use nulib\php\time\DateTime;
use nulib\php\time\Delay; use nulib\php\time\Delay;
use nulib\str; use nulib\str;
@ -18,10 +18,17 @@ class CacheFile extends SharedFile {
const EXT = ".cache"; const EXT = ".cache";
protected function ensure_source($source): CacheData {
if ($source instanceof CacheData) return $source;
if (cv::subclass_of($source, CacheData::class)) return new $source();
if (func::is_callable($source)) return new CacheData($source);
throw ValueException::invalid_type($source, CacheData::class);
}
function __construct($file, ?array $params=null) { function __construct($file, ?array $params=null) {
if ($file === null) { if ($file === null) {
$rand = bin2hex(random_bytes(8)); $rand = bin2hex(random_bytes(8));
$file = sys_get_temp_dir()."/$rand"; $file = path::join(sys_get_temp_dir(), $rand);
} }
$file = path::ensure_ext($file, self::EXT); $file = path::ensure_ext($file, self::EXT);
$this->basedir = path::dirname($file); $this->basedir = path::dirname($file);
@ -32,34 +39,28 @@ class CacheFile extends SharedFile {
$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;
if ($data === null) { if ($data === null) {
$this->dataDefs = null; $this->source = null;
$this->data = null;
} elseif ($data instanceof CacheData) { } elseif ($data instanceof CacheData) {
$this->dataDefs = null; $this->source = $data;
$this->data = $data; } elseif (func::is_callable($data)) {
$this->source = new CacheData($data);
} elseif (!is_array($data)) { } elseif (!is_array($data)) {
$this->dataDefs = null; $this->source = self::ensure_source($data);
$this->data = $this->getData($data);
} else { } else {
$dataDefs = []; $sources = [];
$tmpdefs = $data;
$index = 0; $index = 0;
foreach ($tmpdefs as $key => $data) { foreach ($data as $key => $source) {
$odef = $data; $source = self::ensure_source($source);
if ($data instanceof CacheData) {
} elseif (cv::subclass_of($data, CacheData::class)) {
$data = new $data();
} else {
throw ValueException::invalid_type($odef, CacheData::class);
}
if ($key === $index) { if ($key === $index) {
$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); parent::__construct($file);
} }
@ -78,23 +79,23 @@ class CacheFile extends SharedFile {
protected bool $cacheNull; 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) { if ($data === null) {
return $this->data ??= new CacheData(function() { return $this->source ??= new CacheData(function() {
return $this->compute(); return $this->compute();
}); });
} }
$odata = $data; $source = $data;
if (is_string($data) || is_int($data)) { if (is_string($source) || is_int($source)) {
$data = $this->dataDefs[$data] ?? null; $source = $this->sources[$source] ?? null;
if ($data === null) throw ValueException::invalid_key($odata); if ($source === null) throw ValueException::invalid_key($data);
} }
if ($data instanceof CacheData) return $data; if ($source instanceof CacheData) return $source;
throw ValueException::invalid_type($odata, CacheData::class); 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 * 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 # considèrer que le fichier est invalide s'il est de taille nulle
return $this->getSize() > 0; return $this->getSize() > 0;
} }
@ -244,8 +245,8 @@ class CacheFile extends SharedFile {
function get($data=null, bool $noCache=false) { function get($data=null, bool $noCache=false) {
return $this->action(function () use ($data, $noCache) { return $this->action(function () use ($data, $noCache) {
$data = $this->getData($data); $source = $this->getSource($data);
$dataname = $data->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);
@ -256,12 +257,14 @@ class CacheFile extends SharedFile {
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->duration = null;
$updateData = true; $updateData = true;
} }
if ($updateData) { if ($updateData) {
# calculer un fichier # calculer un fichier
try { try {
$data = $data->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); $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 */ /** obtenir les informations sur le fichier */
function getInfos(): array { function getInfos(): array {
return $this->action(function () { return $this->action(function () {
@ -323,8 +339,12 @@ class CacheFile extends SharedFile {
const UPDATE_SUB = -1, UPDATE_SET = 0, UPDATE_ADD = 1; 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; if ($this->readonly) return;
$this->action(function () use ($nduration, $action) { $this->action(function () use ($nduration, $action) {
if (!$this->isValid()) return; if (!$this->isValid()) return;

View File

@ -1,11 +1,13 @@
<?php <?php
namespace nulib\db\cache; namespace nulib\cache;
use nulib\cache\CacheChannel;
use nulib\cache\CacheDataChannel;
use nulib\db\Capacitor; use nulib\db\Capacitor;
use nulib\db\CapacitorStorage; use nulib\db\CapacitorStorage;
use nulib\db\sqlite\SqliteStorage; use nulib\db\sqlite\SqliteStorage;
class cache { class storage_cache {
protected static ?CapacitorStorage $storage = null; protected static ?CapacitorStorage $storage = null;
static function set_storage(CapacitorStorage $storage): CapacitorStorage { static function set_storage(CapacitorStorage $storage): CapacitorStorage {
@ -29,8 +31,8 @@ class cache {
else return self::set(null); else return self::set(null);
} }
static function new(?RowsChannel $channel, $id=null, ?callable $builder=null): RowsChannel { static function new(?CacheDataChannel $channel, $id=null, ?callable $builder=null): CacheDataChannel {
$channel ??= new RowsChannel($id, $builder); $channel ??= new CacheDataChannel($id, $builder);
new Capacitor(self::get_storage(), $channel); new Capacitor(self::get_storage(), $channel);
return $channel; return $channel;
} }

View File

@ -4,7 +4,7 @@ namespace nulib\tools;
use Exception; use Exception;
use nulib\app\cli\Application; use nulib\app\cli\Application;
use nulib\ext\yaml; use nulib\ext\yaml;
use nulib\file\cache\CacheFile; use nulib\cache\CacheFile;
use nulib\os\path; use nulib\os\path;
use nulib\output\msg; use nulib\output\msg;
@ -32,10 +32,11 @@ class NucacheApp extends Application {
"action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SUB], "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SUB],
"help" => "Enlever le nombre de secondes spécifié à la durée du cache", "help" => "Enlever le nombre de secondes spécifié à la durée du cache",
], ],
["-s", "--set-duration", "args" => 1, #XXX pas encore implémenté
"action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SET], //["-s", "--set-duration", "args" => 1,
"help" => "Mettre à jour la durée du cache à la valeur spécifiée", // "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; protected $action = self::ACTION_READ;

View File

@ -2,40 +2,54 @@
<?php <?php
require __DIR__.'/../vendor/autoload.php'; require __DIR__.'/../vendor/autoload.php';
use nulib\file\cache\CacheData; use nulib\cache\CacheData;
use nulib\file\cache\CacheFile; use nulib\cache\CacheFile;
use nulib\ext\yaml;
use nulib\os\sh; use nulib\os\sh;
function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void {
Txx("$prefix=", $cache->get());
if ($dumpInfos) {
yaml::dump($cache->getInfos());
}
}
//system("rm -f *.cache .*.cache"); //system("rm -f *.cache .*.cache");
$what = [ $what = [
"null", //"null",
"one", "one",
"two", //"two",
"three", //"three",
]; ];
$duration = 10;
if (in_array("null", $what)) { if (in_array("null", $what)) {
$null = new CacheFile("null"); $null = new CacheFile("null", [
Txx("null=", $null->get()); "duration" => $duration,
]);
show("null", $null);
} }
if (in_array("one", $what)) { if (in_array("one", $what)) {
$one = new class("one") extends CacheFile { $one = new class("one", [
"duration" => $duration,
]) extends CacheFile {
protected function compute() { protected function compute() {
return 1; return 1;
} }
}; };
Txx("one=", $one->get()); show("one", $one);
} }
if (in_array("two", $what)) { if (in_array("two", $what)) {
$two = new CacheFile("two", [ $two = new CacheFile("two", [
"duration" => $duration,
"data" => new CacheData(function () { "data" => new CacheData(function () {
return 2; return 2;
}), }),
]); ]);
Txx("two=", $two->get()); show("two", $two);
} }
if (in_array("three", $what)) { if (in_array("three", $what)) {
@ -59,4 +73,4 @@ if (in_array("three", $what)) {
Txx("three.1=", $three->get("data31name")); Txx("three.1=", $three->get("data31name"));
Txx("three.2=", $three->get("data32")); Txx("three.2=", $three->get("data32"));
Txx("three.3=", $three->get("")); Txx("three.3=", $three->get(""));
} }