modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-10-04 13:03:04 +04:00
parent 81b14073a4
commit a34bad04c3
5 changed files with 47 additions and 5 deletions

View File

@ -12,7 +12,7 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
static function with(?iterable $rows=null, $cursorId=null, ?CapacitorStorage $storage=null): self {
$storage ??= cache::storage();
$channel = (new static($cursorId))->initStorage($storage);
if ($rows !== null) $channel->build($rows);
if ($rows !== null) $channel->rechargeAll($rows);
return $channel;
}
@ -97,7 +97,6 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
}
function chargeAll(?iterable $items, $func=null, ?array $args=null): int {
$this->index = 0;
if ($items === null) return 0;
$count = 0;
if ($func !== null) $func = func::with($func, $args)->bind($this);
@ -107,8 +106,9 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate {
return $count;
}
function build(?iterable $items): self {
function rechargeAll(?iterable $items): self {
$this->delete(null);
$this->index = 0;
$this->chargeAll($items);
return $this;
}

2
src/cache/cache.php vendored
View File

@ -72,7 +72,7 @@ class cache {
self::verifix_id($cursorId);
$file ??= "{$cursorId["group_id"]}_{$cursorId["id"]}_rows";
$ccursorId = new CacheFile($file, function() use ($rows, $cursorId) {
CacheChannel::with(null, $cursorId)->build($rows);
CacheChannel::with(null, $cursorId)->rechargeAll($rows);
return $cursorId;
});
$noCache = !self::should_cache($cursorId["id"], $cursorId["group_id"]);

View File

@ -27,7 +27,7 @@ class CacheChannelTest extends _TestCase {
const ADD_COLUMNS = [
"a" => "varchar(30)",
];
})->initStorage(self::$storage)->build(self::DATA);
})->initStorage(self::$storage)->rechargeAll(self::DATA);
$count = 0;
foreach ($channel as $key => $item) {
msg::info("one: $key => {$item["a"]}");

22
tests/cache/SourceDb.php vendored Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace nulib\cache;
use nulib\db\sqlite\Sqlite;
class SourceDb extends Sqlite {
const MIGRATION = [
"create table source (pk integer primary key autoincrement, s varchar(255), i integer, b boolean)",
[self::class, "fill_data"],
];
static function fill_data(Sqlite $db): void {
$db->exec("insert into source (s, i, b) values (null, null, null)");
$db->exec("insert into source (s, i, b) values ('false', 0, 0)");
$db->exec("insert into source (s, i, b) values ('first', 1, 1)");
$db->exec("insert into source (s, i, b) values ('second', 2, 1)");
}
public function __construct() {
parent::__construct(__DIR__."/source.db");
}
}

20
tests/cache/SourceTest.php vendored Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace nulib\cache;
use nulib\db\Capacitor;
use nulib\db\CapacitorStorage;
use nulib\db\sqlite\Sqlite;
use nulib\db\sqlite\SqliteStorage;
use nulib\tests\TestCase;
class SourceTest extends TestCase {
function test() {
$destStorage = new SqliteStorage(new Sqlite(__DIR__.'/dest.db'));
new Capacitor($destStorage, $channel = new CacheChannel("source"));
$sourceDb = new SourceDb();
$channel->rechargeAll($sourceDb->all("select * from source"));
self::assertTrue(true);
}
}