nur-ture/src/cache/cache.php

36 lines
1.0 KiB
PHP

<?php
namespace nulib\cache;
use nulib\app;
use nulib\db\CapacitorStorage;
use nulib\db\sqlite\SqliteStorage;
use nulib\php\func;
class cache {
protected static ?string $dbfile = null;
protected static function dbfile(): ?string {
return self::$dbfile ??= app::get()->getVarfile("cache.db");
}
protected static ?CapacitorStorage $storage = null;
protected static function storage(): CapacitorStorage {
return self::$storage ??= new SqliteStorage(self::dbfile());
}
static function set_storage(CapacitorStorage $storage): CapacitorStorage {
return self::$storage = $storage;
}
static function all(?iterable $rows, $cursorId=null): iterable {
CacheChannel::verifix_id($cursorId);
$file = "row-{$cursorId["group_id"]}-{$cursorId["id"]}";
$cache = new CacheFile($file, function() use ($rows, $cursorId) {
CacheChannel::with(self::storage(), null, $cursorId)->build($rows);
return $cursorId;
});
return CacheChannel::with(self::storage(), null, $cache->get());
}
}