save function

This commit is contained in:
Fred Tempez 2023-03-10 13:48:27 +01:00
parent 2b329979c2
commit 0a8a8ecd72

View File

@ -142,17 +142,18 @@ class JsonDb extends \Prowebcraft\Dot
*/
public function save()
{
$lenght = strlen(json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | LOCK_EX));
$try = 0;
while ($try < 5) {
$written = file_put_contents($this->db, json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | LOCK_EX)); // Multi user get a locker
if ($written == $lenght) {
$v = json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | LOCK_EX);
$l = strlen($v);
$t = 0;
while ($t < 5) {
$w = file_put_contents($this->db, $v); // Multi user get a locker
if ($w == $l) {
break;
}
$try++;
sleep(1);
}
if ($written !== $lenght) {
if ($w !== $l) {
exit('Erreur d\'écriture, les données n\'ont pas été sauvegardées');
}