modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									8079d111cc
								
							
						
					
					
						commit
						94d6d5cf3e
					
				
							
								
								
									
										72
									
								
								src/cache/CacheChannel.php
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										72
									
								
								src/cache/CacheChannel.php
									
									
									
									
										vendored
									
									
								
							| @ -10,19 +10,6 @@ use nulib\php\func; | |||||||
| use Traversable; | use Traversable; | ||||||
| 
 | 
 | ||||||
| class CacheChannel extends CapacitorChannel implements IteratorAggregate { | 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 { |   static function with(?iterable $rows=null, $cursorId=null, ?CapacitorStorage $storage=null): self { | ||||||
|     $storage ??= cache::storage(); |     $storage ??= cache::storage(); | ||||||
|     $channel = (new static($cursorId))->initStorage($storage); |     $channel = (new static($cursorId))->initStorage($storage); | ||||||
| @ -30,23 +17,53 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate { | |||||||
|     return $channel; |     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 |    * @param array|string $cursorId | ||||||
|    */ |    */ | ||||||
|   function __construct($cursorId) { |   function __construct($cursorId) { | ||||||
|     parent::__construct(); |     parent::__construct(); | ||||||
|     cache::verifix_id($cursorId); |     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 { |   function getCursorId(): array { | ||||||
|     return $this->cursorId; |     return [ | ||||||
|  |       "group_id" => $this->groupId, | ||||||
|  |       "id" => $this->id, | ||||||
|  |     ]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function getBaseFilter(): ?array { |   function getBaseFilter(): ?array { | ||||||
|     return $this->cursorId; |     return [ | ||||||
|  |       "group_id_" => $this->groupId, | ||||||
|  |       "id_" => $this->id, | ||||||
|  |     ]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected int $index = 0; |   protected int $index = 0; | ||||||
| @ -61,10 +78,17 @@ class CacheChannel extends CapacitorChannel implements IteratorAggregate { | |||||||
|     $index = $this->index++; |     $index = $this->index++; | ||||||
|     $key = $key ?? $index; |     $key = $key ?? $index; | ||||||
|     $key = substr(strval($key), 0, 128); |     $key = substr(strval($key), 0, 128); | ||||||
|     return cl::merge($this->cursorId, [ |     $addColumns = static::ADD_COLUMNS ?? []; | ||||||
|       "key_index" => $index, |     $addColumns = cl::select($item, | ||||||
|       "key" => $key, |       array_filter(array_keys($addColumns), function ($key) { | ||||||
|       "search" => $this->getSearch($item), |         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 { |   function build(?iterable $items): self { | ||||||
|     $this->reset(true); |     $this->delete(null); | ||||||
|     $this->chargeAll($items); |     $this->chargeAll($items); | ||||||
|     return $this; |     return $this; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function getIterator(): Traversable { |   function getIterator(): Traversable { | ||||||
|     $rows = $this->dbAll([ |     $rows = $this->dbAll([ | ||||||
|       "cols" => ["key", "item__"], |       "cols" => ["key_", "item__"], | ||||||
|       "where" => $this->getBaseFilter(), |       "where" => $this->getBaseFilter(), | ||||||
|     ]); |     ]); | ||||||
|     foreach ($rows as $row) { |     foreach ($rows as $row) { | ||||||
|       $key = $row["key"]; |       $key = $row["key_"]; | ||||||
|       $item = $this->unserialize($row["item__"]); |       $item = $this->unserialize($row["item__"]); | ||||||
|       yield $key => $item; |       yield $key => $item; | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								src/cache/TODO.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								src/cache/TODO.md
									
									
									
									
										vendored
									
									
								
							| @ -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 |  | ||||||
							
								
								
									
										4
									
								
								src/cache/cache.php
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								src/cache/cache.php
									
									
									
									
										vendored
									
									
								
							| @ -64,14 +64,14 @@ class cache { | |||||||
| 
 | 
 | ||||||
|   static function get(callable $compute, $dataId=null, ?string $file=null) { |   static function get(callable $compute, $dataId=null, ?string $file=null) { | ||||||
|     self::verifix_id($dataId); |     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"]); |     $noCache = !self::should_cache($dataId["id"], $dataId["group_id"]); | ||||||
|     return CacheFile::with($compute, $file)->get(null, $noCache); |     return CacheFile::with($compute, $file)->get(null, $noCache); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   static function all(?iterable $rows, $cursorId=null, ?string $file=null): iterable { |   static function all(?iterable $rows, $cursorId=null, ?string $file=null): iterable { | ||||||
|     self::verifix_id($cursorId); |     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) { |     $ccursorId = new CacheFile($file, function() use ($rows, $cursorId) { | ||||||
|       CacheChannel::with(null, $cursorId)->build($rows); |       CacheChannel::with(null, $cursorId)->build($rows); | ||||||
|       return $cursorId; |       return $cursorId; | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								tests/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								tests/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | *.db* | ||||||
|  | *.cache | ||||||
							
								
								
									
										2
									
								
								tests/cache/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								tests/cache/.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,2 +0,0 @@ | |||||||
| /*.db* |  | ||||||
| /*.cache |  | ||||||
							
								
								
									
										38
									
								
								tests/cache/CacheChannelTest.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								tests/cache/CacheChannelTest.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | |||||||
|  | <?php | ||||||
|  | namespace nulib\cache; | ||||||
|  | 
 | ||||||
|  | use nulib\output\msg; | ||||||
|  | 
 | ||||||
|  | class CacheChannelTest extends _TestCase { | ||||||
|  |   const DATA = [ | ||||||
|  |     "fr" => ["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); | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										22
									
								
								tests/cache/CursorChannelTest.php
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										22
									
								
								tests/cache/CursorChannelTest.php
									
									
									
									
										vendored
									
									
								
							| @ -1,22 +0,0 @@ | |||||||
| <?php |  | ||||||
| namespace nulib\cache; |  | ||||||
| 
 |  | ||||||
| use nulib\output\msg; |  | ||||||
| 
 |  | ||||||
| class CursorChannelTest extends _TestCase { |  | ||||||
|   function testUsage() { |  | ||||||
|     $data = [ |  | ||||||
|       "fr" => ["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); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user