diff --git a/src/cache/CacheChannel.php b/src/cache/CacheChannel.php index 91f021d..68fde79 100644 --- a/src/cache/CacheChannel.php +++ b/src/cache/CacheChannel.php @@ -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; } diff --git a/src/cache/TODO.md b/src/cache/TODO.md deleted file mode 100644 index 4f8f006..0000000 --- a/src/cache/TODO.md +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/cache/cache.php b/src/cache/cache.php index fa0991f..b0a25c2 100644 --- a/src/cache/cache.php +++ b/src/cache/cache.php @@ -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; diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..69bea44 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,2 @@ +*.db* +*.cache diff --git a/tests/cache/.gitignore b/tests/cache/.gitignore deleted file mode 100644 index d540dae..0000000 --- a/tests/cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/*.db* -/*.cache diff --git a/tests/cache/CacheChannelTest.php b/tests/cache/CacheChannelTest.php new file mode 100644 index 0000000..2de3842 --- /dev/null +++ b/tests/cache/CacheChannelTest.php @@ -0,0 +1,38 @@ + ["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); + } +} diff --git a/tests/cache/CursorChannelTest.php b/tests/cache/CursorChannelTest.php deleted file mode 100644 index 6a1aeee..0000000 --- a/tests/cache/CursorChannelTest.php +++ /dev/null @@ -1,22 +0,0 @@ - ["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); - } -}