modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-07-02 10:52:03 +04:00
parent 8079d111cc
commit 94d6d5cf3e
7 changed files with 90 additions and 56 deletions

View File

@ -10,19 +10,6 @@ use nulib\php\func;
use Traversable;
class CacheChannel extends CapacitorChannel implements IteratorAggregate {
const NAME = "cache";
const TABLE_NAME = "cache";
const COLUMN_DEFINITIONS = [
"group_id" => "varchar(32) not null", // groupe de curseur
"id" => "varchar(128) not null", // nom du curseur
"key_index" => "integer not null",
"key" => "varchar(128) not null",
"search" => "varchar(255)",
"primary key (group_id, id, key_index)",
];
static function with(?iterable $rows=null, $cursorId=null, ?CapacitorStorage $storage=null): self {
$storage ??= cache::storage();
$channel = (new static($cursorId))->initStorage($storage);
@ -30,23 +17,53 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
return $channel;
}
const NAME = "cache";
const TABLE_NAME = "cache";
const COLUMN_DEFINITIONS = [
"group_id_" => "varchar(32) not null", // groupe de curseur
"id_" => "varchar(128) not null", // nom du curseur
"key_index_" => "integer not null",
"key_" => "varchar(128) not null",
"search_" => "varchar(255)",
"primary key (group_id_, id_, key_index_)",
];
const ADD_COLUMNS = null;
protected function COLUMN_DEFINITIONS(): ?array {
return cl::merge(self::COLUMN_DEFINITIONS, static::ADD_COLUMNS);
}
/**
* @param array|string $cursorId
*/
function __construct($cursorId) {
parent::__construct();
cache::verifix_id($cursorId);
$this->cursorId = $cursorId;
[
"group_id" => $this->groupId,
"id" => $this->id,
] = $cursorId;
}
protected array $cursorId;
protected string $groupId;
protected string $id;
function getCursorId(): array {
return $this->cursorId;
return [
"group_id" => $this->groupId,
"id" => $this->id,
];
}
function getBaseFilter(): ?array {
return $this->cursorId;
return [
"group_id_" => $this->groupId,
"id_" => $this->id,
];
}
protected int $index = 0;
@ -61,10 +78,17 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
$index = $this->index++;
$key = $key ?? $index;
$key = substr(strval($key), 0, 128);
return cl::merge($this->cursorId, [
"key_index" => $index,
"key" => $key,
"search" => $this->getSearch($item),
$addColumns = static::ADD_COLUMNS ?? [];
$addColumns = cl::select($item,
array_filter(array_keys($addColumns), function ($key) {
return is_string($key);
}));
return cl::merge($addColumns, [
"group_id_" => $this->groupId,
"id_" => $this->id,
"key_index_" => $index,
"key_" => $key,
"search_" => $this->getSearch($item),
]);
}
@ -85,18 +109,18 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
}
function build(?iterable $items): self {
$this->reset(true);
$this->delete(null);
$this->chargeAll($items);
return $this;
}
function getIterator(): Traversable {
$rows = $this->dbAll([
"cols" => ["key", "item__"],
"cols" => ["key_", "item__"],
"where" => $this->getBaseFilter(),
]);
foreach ($rows as $row) {
$key = $row["key"];
$key = $row["key_"];
$item = $this->unserialize($row["item__"]);
yield $key => $item;
}

6
src/cache/TODO.md vendored
View File

@ -1,6 +0,0 @@
# nulib\cache
* CacheChannel
* spécifier des clés supplémentaires utilisable dans la recherche
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary

4
src/cache/cache.php vendored
View File

@ -64,14 +64,14 @@ class cache {
static function get(callable $compute, $dataId=null, ?string $file=null) {
self::verifix_id($dataId);
$file ??= "{$dataId["group_id"]}-{$dataId["id"]}";
$file ??= "{$dataId["group_id"]}_{$dataId["id"]}";
$noCache = !self::should_cache($dataId["id"], $dataId["group_id"]);
return CacheFile::with($compute, $file)->get(null, $noCache);
}
static function all(?iterable $rows, $cursorId=null, ?string $file=null): iterable {
self::verifix_id($cursorId);
$file ??= "{$cursorId["group_id"]}-{$cursorId["id"]}--rows";
$file ??= "{$cursorId["group_id"]}_{$cursorId["id"]}_rows";
$ccursorId = new CacheFile($file, function() use ($rows, $cursorId) {
CacheChannel::with(null, $cursorId)->build($rows);
return $cursorId;

2
tests/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.db*
*.cache

View File

@ -1,2 +0,0 @@
/*.db*
/*.cache

38
tests/cache/CacheChannelTest.php vendored Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace nulib\cache;
use nulib\output\msg;
class CacheChannelTest extends _TestCase {
const DATA = [
"fr" => ["a" => "un", "b" => "deux"],
"eng" => ["a" => "one", "b" => "two"],
["a" => 1, "b" => 2],
];
function testUsage() {
$channel = CacheChannel::with(self::DATA, "numbers", self::$storage);
$count = 0;
foreach ($channel as $key => $item) {
msg::info("one: $key => {$item["a"]}");
$count++;
}
self::assertSame(3, $count);
}
function testAddColumns() {
$channel = (new class("numbers") extends CacheChannel {
const NAME = "numbersac";
const TABLE_NAME = self::NAME;
const ADD_COLUMNS = [
"a" => "varchar(30)",
];
})->initStorage(self::$storage)->build(self::DATA);
$count = 0;
foreach ($channel as $key => $item) {
msg::info("one: $key => {$item["a"]}");
$count++;
}
self::assertSame(3, $count);
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace nulib\cache;
use nulib\output\msg;
class CursorChannelTest extends _TestCase {
function testUsage() {
$data = [
"fr" => ["a" => "un", "b" => "deux"],
"eng" => ["a" => "one", "b" => "two"],
["a" => 1, "b" => 2],
];
$channel = CacheChannel::with($data, "numbers", self::$storage);
$count = 0;
foreach ($channel as $key => $item) {
msg::info("one: $key => {$item["a"]}");
$count++;
}
self::assertSame(3, $count);
}
}