Good evening. I have personal accounts on my site. When authorizing a user, the system looks at whether it has a cache in the personal account, if not, it loads from the database and loads it into the cache. The cache is built on files of the form USER_ID.txt so all changes that the user makes are saved to the cache. Then cron every 10 minutes, checks if there are user cache files, if there is, then enumerates and saves data to the database, then clears the cache. Everything seems to be normal, but sometimes there is such a problem that the information was not updated in the database, but the cache file was deleted, thereby rolling back. Here is the script:

function Save($folder) { global $Mysql; $GetFilesDir = Local_temp::GetFilesDir($folder); if($GetFilesDir == false){ $Return['error']['get_files_dir'] = true; return $Return; } foreach($GetFilesDir as $key=>$file_name) { $Save = array(); $Get = Local_temp::Get($folder.$file_name); if($Get == false) continue; $result = $Mysql->query("SELECT * FROM `users` WHERE `id`='{$Get['id']}' "); $myrow = $Mysql->get_array($result); if($myrow == false) continue; foreach($myrow as $k=>$v) { if(is_numeric($k)) continue; if($Get[$k] != $v) $Save += array($k=>$Get[$k]); } if($Save == false) continue; $UpdateUser = Update_user::Update($Save, $Get['id'], false); if(mysql_affected_rows() != 1) continue; $history_file_name = date('Y').'_'.date('m').'_'.date('d').'_'.date('H').'-'.date('i').'.txt'; Local_temp::NewFolder('local_history_temp/users/'.$Get['id']); Local_temp::Set('local_history_temp/users/'.$Get['id'].'/'.$history_file_name, $Get); Local_temp::Drop($folder.$file_name); } return true; } /* Local_temp::Get($folder.$file_name); - Получение файла кеша $UpdateUser = Update_user::Update($Save, $Get['id'], false); - Обновление БД local_history_temp - Директория отвечающая за архив кеша (так я и увидел, что систему откатывает) Local_temp::Set('local_history_temp/users/'.$Get['id'].'/'.$history_file_name, $Get); - Новый кеш Local_temp::Drop($folder.$file_name); - Удаление кеша */ 
  • Have you ever seen on the Internet such as “no way, never save any permanent data to the cache, even if Gwyneth Paltrow asks up with Jason Statham to ask you to?” - etki
  • There are also signs "for no reason, never execute queries to the database in a loop." In addition to rare exceptions when the cycle is small and of a static length - Snow

1 answer 1

1) Working with the database is safer and structuring, it is easier to work with the database, and it seems to work a little bit slower than with files, why not immediately do everything in the database? If we consider that resources are going to create files, check files, delete, -.- 2) According to your problem, the problem is with $ GET - you do not filter it (judging by the code), most likely it causes errors. 3) If you are still filtering, then put checkpoints (after 2-3 actions) and write them to the debug file, when your problem occurs, you can go to that file and check where what went wrong.

  • The project of online games, the base is always loaded, I had to do everything this way. Thanks for the help, I will try your method - angers777