diff --git a/.idea/php.xml b/.idea/php.xml
index 428268c..36ca569 100644
--- a/.idea/php.xml
+++ b/.idea/php.xml
@@ -23,13 +23,18 @@
+
+
+
+
+
@@ -38,6 +43,7 @@
+
@@ -45,9 +51,13 @@
+
+
+
+
@@ -65,28 +75,18 @@
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/cache/TODO.md b/src/cache/TODO.md
index 0deecd1..5d181f1 100644
--- a/src/cache/TODO.md
+++ b/src/cache/TODO.md
@@ -2,4 +2,20 @@
* [ ] CacheChannel: stocker aussi la clé primaire, ce qui permet de récupérer la donnée correspondante dans la source
+* Cache:
+ * une méthode calcule + stocke la valeur
+ * valeur scalaire: compute() puis écriture dans un fichier cache
+ * valeur itérable: compute() puis enregistre dans un channel
+ * une méthode récupère la valeur
+ * valeur scalaire: lecture du fichier cache
+ * valeur itérable: parcours des valeurs du channel
+
+ ainsi, il faudra peut-être deux classes: DataCache et CursorCache qui
+ implémentent ces deux fonctionnalités
+ * cache::get() utilise DataCache
+ * cache::all() utilise CursorCache
+
+peut-être qu'on peut garder CacheFile et le fonctionnement ci-dessus est
+implémenté dans CacheData? à voir
+
-*- 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 1f7b8f0..7ad22e3 100644
--- a/src/cache/cache.php
+++ b/src/cache/cache.php
@@ -61,20 +61,20 @@ class cache {
$cacheId = ["group_id" => $groupId, "id" => $id];
}
- static function get(callable $compute, $dataId=null, ?string $file=null) {
+ static function get($dataId, callable $compute, ?array $params=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);
+ return (new CacheFile($file, $compute, $params))->get(null, $noCache);
}
- static function all(?iterable $rows, $cursorId=null, ?string $file=null): iterable {
+ static function all($cursorId, ?iterable $rows, ?array $params=null): iterable {
self::verifix_id($cursorId);
- $file ??= "{$cursorId["group_id"]}_{$cursorId["id"]}_rows";
- $ccursorId = new CacheFile($file, function() use ($rows, $cursorId) {
- CursorChannel::with($cursorId, null)->rechargeAll($rows);
+ $file = "{$cursorId["group_id"]}_{$cursorId["id"]}_rows";
+ $ccursorId = new CacheFile($file, function() use ($cursorId, $rows) {
+ CursorChannel::with($cursorId)->rechargeAll($rows);
return $cursorId;
- });
+ }, $params);
$noCache = !self::should_cache($cursorId["id"], $cursorId["group_id"]);
return CursorChannel::with($ccursorId->get(null, $noCache));
}
diff --git a/tests/cache/SourceTest.php b/tests/cache/SourceTest.php
index 4adece6..b35d3f6 100644
--- a/tests/cache/SourceTest.php
+++ b/tests/cache/SourceTest.php
@@ -2,19 +2,65 @@
namespace nulib\cache;
use nulib\db\Capacitor;
-use nulib\db\CapacitorStorage;
use nulib\db\sqlite\Sqlite;
use nulib\db\sqlite\SqliteStorage;
+use nulib\output\msg;
+use nulib\output\std\StdMessenger;
use nulib\tests\TestCase;
+use Traversable;
class SourceTest extends TestCase {
- function test() {
+ static function setUpBeforeClass(): void {
+ msg::set_messenger_class(StdMessenger::class);
+ }
+
+ /** @param Traversable|iterable $dest */
+ protected static function assertSource($dest): void {
+ self::assertSame([
+ ["pk" => 1, "s" => null, "i" => null, "b" => null],
+ ["pk" => 2, "s" => "false", "i" => 0, "b" => 0],
+ ["pk" => 3, "s" => "first", "i" => 1, "b" => 1],
+ ["pk" => 4, "s" => "second", "i" => 2, "b" => 1],
+ ], iterator_to_array($dest));
+ }
+
+ function testDirect() {
$destStorage = new SqliteStorage(new Sqlite(__DIR__.'/dest.db'));
- new Capacitor($destStorage, $channel = new CursorChannel("source"));
+ new Capacitor($destStorage, $dest = new CursorChannel("source"));
$sourceDb = new SourceDb();
- $channel->rechargeAll($sourceDb->all("select * from source"));
+ $dest->rechargeAll($sourceDb->all("select * from source"));
- self::assertTrue(true);
+ $this->assertSource($dest);
+ }
+
+ function testCache() {
+ $getSource = function() {
+ $db = new SourceDb();
+ msg::info("query source");
+ yield from $db->all("select * from source");
+ };
+
+ $params = [
+ "duration" => 2,
+ "override_duration" => true,
+ ];
+
+ msg::info("initial");
+ cache::nc();
+ $dest = cache::all("source", $getSource(), $params);
+ $this->assertSource($dest);
+
+ msg::info("cached");
+ $dest = cache::all("source", $getSource(), $params);
+ $this->assertSource($dest);
+
+ sleep(4);
+ msg::info("expired");
+ $dest = cache::all("source", $getSource(), $params);
+ $this->assertSource($dest);
+
+ sleep(4);
+ msg::info("end");
}
}
diff --git a/tests/cache/cacheTest.php b/tests/cache/cacheTest.php
index 9d24daa..9d2ee84 100644
--- a/tests/cache/cacheTest.php
+++ b/tests/cache/cacheTest.php
@@ -31,13 +31,13 @@ class cacheTest extends _TestCase {
function testUsage() {
msg::section("all");
- $rows = cache::all($this->gendata(),"gendata");
+ $rows = cache::all("gendata", $this->gendata());
$this->_testRows($rows);
msg::section("get");
- $rows = cache::get(function() {
+ $rows = cache::get("gendata", function () {
return self::DATA;
- },"gendata");
+ });
$this->_testRows($rows);
}
@@ -45,11 +45,11 @@ class cacheTest extends _TestCase {
cache::nc();
msg::section("first pass");
- $rows = cache::all($this->gendata(),"gendata");
+ $rows = cache::all("gendata", $this->gendata());
$this->_testRows($rows);
msg::section("second pass");
- $rows = cache::all($this->gendata(),"gendata");
+ $rows = cache::all("gendata", $this->gendata());
$this->_testRows($rows);
}
}