modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									79b8c33d4f
								
							
						
					
					
						commit
						8612a9dae4
					
				
							
								
								
									
										68
									
								
								src/cache/CacheFile.php
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										68
									
								
								src/cache/CacheFile.php
									
									
									
									
										vendored
									
									
								
							| @ -110,6 +110,8 @@ class CacheFile extends SharedFile { | |||||||
| 
 | 
 | ||||||
|   protected ?Delay $duration; |   protected ?Delay $duration; | ||||||
| 
 | 
 | ||||||
|  |   protected $data; | ||||||
|  | 
 | ||||||
|   protected ?array $datafilenames; |   protected ?array $datafilenames; | ||||||
| 
 | 
 | ||||||
|   /** charger les données. le fichier a déjà été verrouillé en lecture */ |   /** charger les données. le fichier a déjà été verrouillé en lecture */ | ||||||
| @ -119,6 +121,7 @@ class CacheFile extends SharedFile { | |||||||
|       [ |       [ | ||||||
|         "start" => $start, |         "start" => $start, | ||||||
|         "duration" => $duration, |         "duration" => $duration, | ||||||
|  |         "data" => $data, | ||||||
|         "datafiles" => $datafilenames, |         "datafiles" => $datafilenames, | ||||||
|       ] = $this->unserialize(null, false, true); |       ] = $this->unserialize(null, false, true); | ||||||
|       if ($this->overrideDuration) { |       if ($this->overrideDuration) { | ||||||
| @ -128,10 +131,12 @@ class CacheFile extends SharedFile { | |||||||
|     } else { |     } else { | ||||||
|       $start = null; |       $start = null; | ||||||
|       $duration = null; |       $duration = null; | ||||||
|  |       $data = null; | ||||||
|       $datafilenames = []; |       $datafilenames = []; | ||||||
|     } |     } | ||||||
|     $this->start = $start; |     $this->start = $start; | ||||||
|     $this->duration = $duration; |     $this->duration = $duration; | ||||||
|  |     $this->data = $data; | ||||||
|     $this->datafilenames = $datafilenames; |     $this->datafilenames = $datafilenames; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -161,15 +166,16 @@ class CacheFile extends SharedFile { | |||||||
|     $this->serialize([ |     $this->serialize([ | ||||||
|       "start" => $this->start, |       "start" => $this->start, | ||||||
|       "duration" => $this->duration, |       "duration" => $this->duration, | ||||||
|  |       "data" => $this->data, | ||||||
|       "datafiles" => $datafilenames, |       "datafiles" => $datafilenames, | ||||||
|     ], false, true); |     ], false, true); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function loadData(string $datafile) { |   protected function loadDatafile(string $datafile) { | ||||||
|     return file::reader($datafile)->unserialize(); |     return file::reader($datafile)->unserialize(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function saveData(string $datafile, $data): void { |   protected function saveDatafile(string $datafile, $data): void { | ||||||
|     file::writer($datafile)->serialize($data); |     file::writer($datafile)->serialize($data); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -180,10 +186,10 @@ class CacheFile extends SharedFile { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function unlinkFiles(bool $datafilesOnly=false): void { |   protected function unlinkFiles(bool $datafilesOnly=false): void { | ||||||
|     if (!$datafilesOnly) @unlink($this->file); |  | ||||||
|     foreach ($this->datafilenames as $datafilename) { |     foreach ($this->datafilenames as $datafilename) { | ||||||
|       $this->unlinkDatafile($datafilename); |       $this->unlinkDatafile($datafilename); | ||||||
|     } |     } | ||||||
|  |     if (!$datafilesOnly) @unlink($this->file); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** tester si $value peut être mis en cache */ |   /** tester si $value peut être mis en cache */ | ||||||
| @ -195,26 +201,28 @@ class CacheFile extends SharedFile { | |||||||
| 
 | 
 | ||||||
|   protected ?Delay $oduration; |   protected ?Delay $oduration; | ||||||
| 
 | 
 | ||||||
|  |   protected $odata; | ||||||
|  | 
 | ||||||
|   protected ?array $odatafilenames; |   protected ?array $odatafilenames; | ||||||
| 
 | 
 | ||||||
|   protected function beforeAction() { |   protected function beforeAction() { | ||||||
|     $this->loadMetadata(); |     $this->loadMetadata(); | ||||||
|     $this->ostart = cv::clone($this->start); |     $this->ostart = cv::clone($this->start); | ||||||
|     $this->oduration = cv::clone($this->duration); |     $this->oduration = cv::clone($this->duration); | ||||||
|  |     $this->odata = cv::clone($this->data); | ||||||
|     $this->odatafilenames = cv::clone($this->datafilenames); |     $this->odatafilenames = cv::clone($this->datafilenames); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function afterAction() { |   protected function afterAction() { | ||||||
|     $start = $this->start; |     $modified = false; | ||||||
|  |     if ($this->start != $this->ostart) $modified = true; | ||||||
|     $duration = $this->duration; |     $duration = $this->duration; | ||||||
|     $oduration = $this->oduration; |     $oduration = $this->oduration; | ||||||
|     $datafilenames = $this->datafilenames; |  | ||||||
|     $modified = false; |  | ||||||
|     if ($start != $this->ostart) $modified = true; |  | ||||||
|     if ($duration === null || $oduration === null) $modified = true; |     if ($duration === null || $oduration === null) $modified = true; | ||||||
|     elseif ($duration->getDest() != $oduration->getDest()) $modified = true; |     elseif ($duration->getDest() != $oduration->getDest()) $modified = true; | ||||||
|     # égalité stricte uniquement pour $datafiles qui est un array
 |     # égalité stricte uniquement pour $data et $datafiles
 | ||||||
|     if ($datafilenames !== $this->odatafilenames) $modified = true; |     if ($this->data !== $this->odata) $modified = true; | ||||||
|  |     if ($this->datafilenames !== $this->odatafilenames) $modified = true; | ||||||
|     if ($modified && !$this->readonly) { |     if ($modified && !$this->readonly) { | ||||||
|       $this->lockWrite(); |       $this->lockWrite(); | ||||||
|       $this->saveMetadata(); |       $this->saveMetadata(); | ||||||
| @ -230,8 +238,13 @@ class CacheFile extends SharedFile { | |||||||
|       $this->afterAction(); |       $this->afterAction(); | ||||||
|       return $result; |       return $result; | ||||||
|     } finally { |     } finally { | ||||||
|  |       $this->ostart = null; | ||||||
|  |       $this->oduration = null; | ||||||
|  |       $this->odata = null; | ||||||
|  |       $this->odatafilenames = null; | ||||||
|       $this->start = null; |       $this->start = null; | ||||||
|       $this->duration = null; |       $this->duration = null; | ||||||
|  |       $this->data = null; | ||||||
|       $this->datafilenames = null; |       $this->datafilenames = null; | ||||||
|       $this->unlock(true); |       $this->unlock(true); | ||||||
|     } |     } | ||||||
| @ -242,13 +255,15 @@ class CacheFile extends SharedFile { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected function refreshData($data, bool $noCache) { |   protected function refreshData($data, bool $noCache) { | ||||||
|  |     $defaultSource = $data === null; | ||||||
|     $source = $this->getSource($data); |     $source = $this->getSource($data); | ||||||
|     $dataname = $source->getName(); |     $dataname = $source->getName(); | ||||||
|     $datafilename = ".{$this->basename}.{$dataname}".self::EXT; |     $datafilename = ".{$this->basename}.{$dataname}".self::EXT; | ||||||
|     $datafile = path::join($this->basedir, $datafilename); |     $datafile = path::join($this->basedir, $datafilename); | ||||||
| 
 | 
 | ||||||
|     $updateMetadata = $this->shouldUpdate($noCache); |     $updateMetadata = $this->shouldUpdate($noCache); | ||||||
|     $updateData = !array_key_exists($datafilename, $this->datafilenames); |     if ($defaultSource) $updateData = $this->data === null; | ||||||
|  |     else $updateData = !array_key_exists($datafilename, $this->datafilenames); | ||||||
|     if (!$this->readonly && ($updateMetadata || $updateData)) { |     if (!$this->readonly && ($updateMetadata || $updateData)) { | ||||||
|       $this->lockWrite(); |       $this->lockWrite(); | ||||||
|       if ($updateMetadata) { |       if ($updateMetadata) { | ||||||
| @ -256,31 +271,52 @@ class CacheFile extends SharedFile { | |||||||
|         $this->unlinkFiles(true); |         $this->unlinkFiles(true); | ||||||
|         $this->start = null; |         $this->start = null; | ||||||
|         $this->duration = null; |         $this->duration = null; | ||||||
|  |         $this->data = null; | ||||||
|         $updateData = true; |         $updateData = true; | ||||||
|       } |       } | ||||||
|  |       if ($defaultSource) { | ||||||
|         if ($updateData) { |         if ($updateData) { | ||||||
|         # calculer un fichier
 |           # calculer la valeur
 | ||||||
|           try { |           try { | ||||||
|             $data = $source->get(); |             $data = $source->get(); | ||||||
|           } catch (Exception $e) { |           } catch (Exception $e) { | ||||||
|           # ne pas garder le fichier en cas d'exception
 |             # le fichier n'est pas mis à jour, mais ce n'est pas gênant: lors
 | ||||||
|  |             # des futurs appels, l'exception continuera d'être lancée ou la
 | ||||||
|  |             # valeur sera finalement mise à jour
 | ||||||
|  |             throw $e; | ||||||
|  |           } | ||||||
|  |         } else { | ||||||
|  |           $data = $this->data; | ||||||
|  |         } | ||||||
|  |         if ($this->shouldCache($data)) $this->data = $data; | ||||||
|  |         else $this->data = $data = null; | ||||||
|  |       } else { | ||||||
|  |         if ($updateData) { | ||||||
|  |           # calculer la valeur
 | ||||||
|  |           try { | ||||||
|  |             $data = $source->get(); | ||||||
|  |           } catch (Exception $e) { | ||||||
|  |             # ne pas garder le fichier destination en cas d'exception
 | ||||||
|             $this->unlinkDatafile($datafile); |             $this->unlinkDatafile($datafile); | ||||||
|             throw $e; |             throw $e; | ||||||
|           } |           } | ||||||
|         } elseif (file_exists($datafile)) { |         } elseif (file_exists($datafile)) { | ||||||
|         $data = $this->loadData($datafile); |           $data = $this->loadDatafile($datafile); | ||||||
|         } else { |         } else { | ||||||
|           $data = null; |           $data = null; | ||||||
|         } |         } | ||||||
|         if ($this->shouldCache($data)) { |         if ($this->shouldCache($data)) { | ||||||
|         $this->saveData($datafile, $data); |           $this->saveDatafile($datafile, $data); | ||||||
|           $this->datafilenames[$datafilename] = true; |           $this->datafilenames[$datafilename] = true; | ||||||
|         } else { |         } else { | ||||||
|           # ne pas garder le fichier s'il ne faut pas mettre en cache
 |           # ne pas garder le fichier s'il ne faut pas mettre en cache
 | ||||||
|           $this->unlinkDatafile($datafile); |           $this->unlinkDatafile($datafile); | ||||||
|         } |         } | ||||||
|  |       } | ||||||
|  |     } elseif ($defaultSource) { | ||||||
|  |       $data = $this->data; | ||||||
|     } elseif (file_exists($datafile)) { |     } elseif (file_exists($datafile)) { | ||||||
|       $data = $this->loadData($datafile); |       $data = $this->loadDatafile($datafile); | ||||||
|     } else { |     } else { | ||||||
|       $data = null; |       $data = null; | ||||||
|     } |     } | ||||||
| @ -293,12 +329,14 @@ class CacheFile extends SharedFile { | |||||||
|    * cette méthode est un NOP |    * cette méthode est un NOP | ||||||
|    */ |    */ | ||||||
|   function refresh(bool $noCache=false): self { |   function refresh(bool $noCache=false): self { | ||||||
|  |     $this->action(function() use ($noCache) { | ||||||
|       $source = $this->source; |       $source = $this->source; | ||||||
|       if ($source !== null) $datas = [null]; |       if ($source !== null) $datas = [null]; | ||||||
|       else $datas = array_keys($this->sources); |       else $datas = array_keys($this->sources); | ||||||
|       foreach ($datas as $data) { |       foreach ($datas as $data) { | ||||||
|         $this->refreshData($data, $noCache); |         $this->refreshData($data, $noCache); | ||||||
|       } |       } | ||||||
|  |     }); | ||||||
|     return $this; |     return $this; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								src/cache/TODO.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								src/cache/TODO.md
									
									
									
									
										vendored
									
									
								
							| @ -2,7 +2,5 @@ | |||||||
| 
 | 
 | ||||||
| * CacheChannel | * CacheChannel | ||||||
|   * spécifier des clés supplémentaires utilisable dans la recherche |   * spécifier des clés supplémentaires utilisable dans la recherche | ||||||
| * CacheFile |  | ||||||
|   * pour $data===null, stocker dans le fichier de cache directement |  | ||||||
| 
 | 
 | ||||||
| -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary | -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user