modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									3ea51e70b4
								
							
						
					
					
						commit
						66bd7ccf12
					
				| @ -1,5 +1,5 @@ | ||||
| <?php | ||||
| namespace nulib\db\cache; | ||||
| namespace nulib\cache; | ||||
| 
 | ||||
| use nulib\cl; | ||||
| use nulib\db\CapacitorChannel; | ||||
| @ -1,5 +1,5 @@ | ||||
| <?php | ||||
| namespace nulib\file\cache; | ||||
| namespace nulib\cache; | ||||
| 
 | ||||
| use nulib\cl; | ||||
| use nulib\php\func; | ||||
| @ -1,13 +1,15 @@ | ||||
| <?php | ||||
| namespace nulib\db\cache; | ||||
| namespace nulib\cache; | ||||
| 
 | ||||
| use Closure; | ||||
| use IteratorAggregate; | ||||
| use nulib\cache\CacheChannel; | ||||
| use nulib\cache\storage_cache; | ||||
| use nulib\cl; | ||||
| use nulib\db\CapacitorChannel; | ||||
| use Traversable; | ||||
| 
 | ||||
| class RowsChannel extends CapacitorChannel implements IteratorAggregate { | ||||
| class CacheDataChannel extends CapacitorChannel implements IteratorAggregate { | ||||
|   const COLUMN_DEFINITIONS = [ | ||||
|     "key" => "varchar(128) primary key not null", | ||||
|     "all_values" => "mediumtext", | ||||
| @ -38,7 +40,7 @@ class RowsChannel extends CapacitorChannel implements IteratorAggregate { | ||||
|   } | ||||
| 
 | ||||
|   function getIterator(): Traversable { | ||||
|     $cm = cache::get(); | ||||
|     $cm = storage_cache::get(); | ||||
|     if ($cm->shouldUpdate($this->cacheIds)) { | ||||
|       $this->capacitor->reset(); | ||||
|       foreach (($this->builder)() as $key => $row) { | ||||
| @ -1,12 +1,12 @@ | ||||
| <?php | ||||
| namespace nulib\file\cache; | ||||
| namespace nulib\cache; | ||||
| 
 | ||||
| use Exception; | ||||
| use nulib\cl; | ||||
| use nulib\cv; | ||||
| use nulib\file; | ||||
| use nulib\file\SharedFile; | ||||
| use nulib\os\path; | ||||
| use nulib\php\func; | ||||
| use nulib\php\time\DateTime; | ||||
| use nulib\php\time\Delay; | ||||
| use nulib\str; | ||||
| @ -18,10 +18,17 @@ class CacheFile extends SharedFile { | ||||
| 
 | ||||
|   const EXT = ".cache"; | ||||
| 
 | ||||
|   protected function ensure_source($source): CacheData { | ||||
|     if ($source instanceof CacheData) return $source; | ||||
|     if (cv::subclass_of($source, CacheData::class)) return new $source(); | ||||
|     if (func::is_callable($source)) return new CacheData($source); | ||||
|     throw ValueException::invalid_type($source, CacheData::class); | ||||
|   } | ||||
| 
 | ||||
|   function __construct($file, ?array $params=null) { | ||||
|     if ($file === null) { | ||||
|       $rand = bin2hex(random_bytes(8)); | ||||
|       $file = sys_get_temp_dir()."/$rand"; | ||||
|       $file = path::join(sys_get_temp_dir(), $rand); | ||||
|     } | ||||
|     $file = path::ensure_ext($file, self::EXT); | ||||
|     $this->basedir = path::dirname($file); | ||||
| @ -32,34 +39,28 @@ class CacheFile extends SharedFile { | ||||
|     $this->readonly = $params["readonly"] ?? false; | ||||
|     $this->cacheNull = $params["cache_null"] ?? false; | ||||
|     $data = $params["data"] ?? null; | ||||
|     $this->sources = null; | ||||
|     if ($data === null) { | ||||
|       $this->dataDefs = null; | ||||
|       $this->data = null; | ||||
|       $this->source = null; | ||||
|     } elseif ($data instanceof CacheData) { | ||||
|       $this->dataDefs = null; | ||||
|       $this->data = $data; | ||||
|       $this->source = $data; | ||||
|     } elseif (func::is_callable($data)) { | ||||
|       $this->source = new CacheData($data); | ||||
|     } elseif (!is_array($data)) { | ||||
|       $this->dataDefs = null; | ||||
|       $this->data = $this->getData($data); | ||||
|       $this->source = self::ensure_source($data); | ||||
|     } else { | ||||
|       $dataDefs = []; | ||||
|       $tmpdefs = $data; | ||||
|       $sources = []; | ||||
|       $index = 0; | ||||
|       foreach ($tmpdefs as $key => $data) { | ||||
|         $odef = $data; | ||||
|         if ($data instanceof CacheData) { | ||||
|         } elseif (cv::subclass_of($data, CacheData::class)) { | ||||
|           $data = new $data(); | ||||
|         } else { | ||||
|           throw ValueException::invalid_type($odef, CacheData::class); | ||||
|         } | ||||
|       foreach ($data as $key => $source) { | ||||
|         $source = self::ensure_source($source); | ||||
|         if ($key === $index) { | ||||
|           $index++; | ||||
|           $key = $data->getName(); | ||||
|           $key = $source->getName(); | ||||
|         } | ||||
|         $dataDefs[$key] = $data; | ||||
|         $sources[$key] = $source; | ||||
|       } | ||||
|       $this->dataDefs = $dataDefs; | ||||
|       $this->sources = $sources; | ||||
|       $this->source = null; | ||||
|     } | ||||
|     parent::__construct($file); | ||||
|   } | ||||
| @ -78,23 +79,23 @@ class CacheFile extends SharedFile { | ||||
| 
 | ||||
|   protected bool $cacheNull; | ||||
| 
 | ||||
|   protected ?array $dataDefs; | ||||
|   protected ?array $sources; | ||||
| 
 | ||||
|   protected ?CacheData $data; | ||||
|   protected ?CacheData $source; | ||||
| 
 | ||||
|   protected function getData($data): CacheData { | ||||
|   protected function getSource($data): CacheData { | ||||
|     if ($data === null) { | ||||
|       return $this->data ??= new CacheData(function() { | ||||
|       return $this->source ??= new CacheData(function() { | ||||
|         return $this->compute(); | ||||
|       }); | ||||
|     } | ||||
|     $odata = $data; | ||||
|     if (is_string($data) || is_int($data)) { | ||||
|       $data = $this->dataDefs[$data] ?? null; | ||||
|       if ($data === null) throw ValueException::invalid_key($odata); | ||||
|     $source = $data; | ||||
|     if (is_string($source) || is_int($source)) { | ||||
|       $source = $this->sources[$source] ?? null; | ||||
|       if ($source === null) throw ValueException::invalid_key($data); | ||||
|     } | ||||
|     if ($data instanceof CacheData) return $data; | ||||
|     throw ValueException::invalid_type($odata, CacheData::class); | ||||
|     if ($source instanceof CacheData) return $source; | ||||
|     throw ValueException::invalid_type($data, CacheData::class); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
| @ -102,7 +103,7 @@ class CacheFile extends SharedFile { | ||||
|    * | ||||
|    * on assume que le fichier existe, vu qu'il a été ouvert en c+b | ||||
|    */ | ||||
|   protected function isValid(): bool { | ||||
|   function isValid(): bool { | ||||
|     # considèrer que le fichier est invalide s'il est de taille nulle
 | ||||
|     return $this->getSize() > 0; | ||||
|   } | ||||
| @ -244,8 +245,8 @@ class CacheFile extends SharedFile { | ||||
| 
 | ||||
|   function get($data=null, bool $noCache=false) { | ||||
|     return $this->action(function () use ($data, $noCache) { | ||||
|       $data = $this->getData($data); | ||||
|       $dataname = $data->getName(); | ||||
|       $source = $this->getSource($data); | ||||
|       $dataname = $source->getName(); | ||||
|       $datafilename = ".{$this->basename}.{$dataname}".self::EXT; | ||||
|       $datafile = path::join($this->basedir, $datafilename); | ||||
| 
 | ||||
| @ -256,12 +257,14 @@ class CacheFile extends SharedFile { | ||||
|         if ($updateMetadata) { | ||||
|           # il faut refaire tout le cache
 | ||||
|           $this->unlinkFiles(true); | ||||
|           $this->start = null; | ||||
|           $this->duration = null; | ||||
|           $updateData = true; | ||||
|         } | ||||
|         if ($updateData) { | ||||
|           # calculer un fichier
 | ||||
|           try { | ||||
|             $data = $data->get(); | ||||
|             $data = $source->get(); | ||||
|           } catch (Exception $e) { | ||||
|             # ne pas garder le fichier en cas d'exception
 | ||||
|             $this->unlinkDatafile($datafile); | ||||
| @ -288,6 +291,19 @@ class CacheFile extends SharedFile { | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   function all($data=null, bool $noCache=false): ?iterable { | ||||
|     $data = $this->get($data, $noCache); | ||||
|     if ($data !== null && !is_iterable($data)) $data = [$data]; | ||||
|     return $data; | ||||
|   } | ||||
| 
 | ||||
|   function delete($data=null): void { | ||||
|     $source = $this->getSource($data); | ||||
|     $dataname = $source->getName(); | ||||
|     $datafilename = ".{$this->basename}.{$dataname}".self::EXT; | ||||
|     $this->unlinkDatafile($datafilename); | ||||
|   } | ||||
| 
 | ||||
|   /** obtenir les informations sur le fichier */ | ||||
|   function getInfos(): array { | ||||
|     return $this->action(function () { | ||||
| @ -323,8 +339,12 @@ class CacheFile extends SharedFile { | ||||
| 
 | ||||
|   const UPDATE_SUB = -1, UPDATE_SET = 0, UPDATE_ADD = 1; | ||||
| 
 | ||||
|   /** mettre à jour la durée de validité du fichier */ | ||||
|   function updateDuration($nduration, int $action=1): void { | ||||
|   /** | ||||
|    * mettre à jour la durée de validité du fichier | ||||
|    * | ||||
|    * XXX UPDATE_SET n'est pas implémenté | ||||
|    */ | ||||
|   function updateDuration($nduration, int $action=self::UPDATE_ADD): void { | ||||
|     if ($this->readonly) return; | ||||
|     $this->action(function () use ($nduration, $action) { | ||||
|       if (!$this->isValid()) return; | ||||
| @ -1,11 +1,13 @@ | ||||
| <?php | ||||
| namespace nulib\db\cache; | ||||
| namespace nulib\cache; | ||||
| 
 | ||||
| use nulib\cache\CacheChannel; | ||||
| use nulib\cache\CacheDataChannel; | ||||
| use nulib\db\Capacitor; | ||||
| use nulib\db\CapacitorStorage; | ||||
| use nulib\db\sqlite\SqliteStorage; | ||||
| 
 | ||||
| class cache { | ||||
| class storage_cache { | ||||
|   protected static ?CapacitorStorage $storage = null; | ||||
| 
 | ||||
|   static function set_storage(CapacitorStorage $storage): CapacitorStorage { | ||||
| @ -29,8 +31,8 @@ class cache { | ||||
|     else return self::set(null); | ||||
|   } | ||||
| 
 | ||||
|   static function new(?RowsChannel $channel, $id=null, ?callable $builder=null): RowsChannel { | ||||
|     $channel ??= new RowsChannel($id, $builder); | ||||
|   static function new(?CacheDataChannel $channel, $id=null, ?callable $builder=null): CacheDataChannel { | ||||
|     $channel ??= new CacheDataChannel($id, $builder); | ||||
|     new Capacitor(self::get_storage(), $channel); | ||||
|     return $channel; | ||||
|   } | ||||
| @ -4,7 +4,7 @@ namespace nulib\tools; | ||||
| use Exception; | ||||
| use nulib\app\cli\Application; | ||||
| use nulib\ext\yaml; | ||||
| use nulib\file\cache\CacheFile; | ||||
| use nulib\cache\CacheFile; | ||||
| use nulib\os\path; | ||||
| use nulib\output\msg; | ||||
| 
 | ||||
| @ -32,10 +32,11 @@ class NucacheApp extends Application { | ||||
|       "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SUB], | ||||
|       "help" => "Enlever le nombre de secondes spécifié à la durée du cache", | ||||
|     ], | ||||
|     ["-s", "--set-duration", "args" => 1, | ||||
|       "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SET], | ||||
|       "help" => "Mettre à jour la durée du cache à la valeur spécifiée", | ||||
|     ], | ||||
|     #XXX pas encore implémenté
 | ||||
|     //["-s", "--set-duration", "args" => 1,
 | ||||
|     //  "action" => [null, "->setActionUpdate", self::ACTION_UPDATE_SET],
 | ||||
|     //  "help" => "Mettre à jour la durée du cache à la valeur spécifiée",
 | ||||
|     //],
 | ||||
|   ]; | ||||
| 
 | ||||
|   protected $action = self::ACTION_READ; | ||||
|  | ||||
| @ -2,40 +2,54 @@ | ||||
| <?php | ||||
| require __DIR__.'/../vendor/autoload.php'; | ||||
| 
 | ||||
| use nulib\file\cache\CacheData; | ||||
| use nulib\file\cache\CacheFile; | ||||
| use nulib\cache\CacheData; | ||||
| use nulib\cache\CacheFile; | ||||
| use nulib\ext\yaml; | ||||
| use nulib\os\sh; | ||||
| 
 | ||||
| function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void { | ||||
|   Txx("$prefix=", $cache->get()); | ||||
|   if ($dumpInfos) { | ||||
|     yaml::dump($cache->getInfos()); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| //system("rm -f *.cache .*.cache");
 | ||||
| 
 | ||||
| $what = [ | ||||
|   "null", | ||||
|   //"null",
 | ||||
|   "one", | ||||
|   "two", | ||||
|   "three", | ||||
|   //"two",
 | ||||
|   //"three",
 | ||||
| ]; | ||||
| $duration = 10; | ||||
| 
 | ||||
| if (in_array("null", $what)) { | ||||
|   $null = new CacheFile("null"); | ||||
|   Txx("null=", $null->get()); | ||||
|   $null = new CacheFile("null", [ | ||||
|     "duration" => $duration, | ||||
|   ]); | ||||
|   show("null", $null); | ||||
| } | ||||
| 
 | ||||
| if (in_array("one", $what)) { | ||||
|   $one = new class("one") extends CacheFile { | ||||
|   $one = new class("one", [ | ||||
|     "duration" => $duration, | ||||
|   ]) extends CacheFile { | ||||
|     protected function compute() { | ||||
|       return 1; | ||||
|     } | ||||
|   }; | ||||
|   Txx("one=", $one->get()); | ||||
|   show("one", $one); | ||||
| } | ||||
| 
 | ||||
| if (in_array("two", $what)) { | ||||
|   $two = new CacheFile("two", [ | ||||
|     "duration" => $duration, | ||||
|     "data" => new CacheData(function () { | ||||
|       return 2; | ||||
|     }), | ||||
|   ]); | ||||
|   Txx("two=", $two->get()); | ||||
|   show("two", $two); | ||||
| } | ||||
| 
 | ||||
| if (in_array("three", $what)) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user