Answer the question
In order to leave comments, you need to log in
Cyclic writing from an array to files, how to optimize?
I made a script for recording a TV program in the database, I'm not very friendly with mysql and php, I decided to base it on the file system. The script is planned to be run by cron once a week, the main thing is that the host does not hang :)))
Can something be done to optimize it? Please help me with my fierce code)) I
spent the whole day tormented)
<?php
$xml = simplexml_load_string(file_get_contents('xmltv2.xml'), "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$array = array_map('normalizeArray', $array);
function normalizeArray($value)
{
if (is_array($value)) {
if (!empty($value['@attributes'])) {
$attributes = $value['@attributes'];
unset($value['@attributes']);
$value = array_merge($value, $attributes);
}
$value = array_map('normalizeArray', $value);
}
return $value;
}
function str2url($str) {
$translit=array(
"А"=>"a","Б"=>"b","В"=>"v","Г"=>"g","Д"=>"d","Е"=>"e","Ё"=>"e","Ж"=>"zh","З"=>"z","И"=>"i","Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n","О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t","У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch","Ш"=>"sh","Щ"=>"shch","Ъ"=>"","Ы"=>"y","Ь"=>"","Э"=>"e","Ю"=>"yu","Я"=>"ya",
"а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e","ё"=>"e","ж"=>"zh","з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l","м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h","ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"shch","ъ"=>"","ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
"A"=>"a","B"=>"b","C"=>"c","D"=>"d","E"=>"e","F"=>"f","G"=>"g","H"=>"h","I"=>"i","J"=>"j","K"=>"k","L"=>"l","M"=>"m","N"=>"n","O"=>"o","P"=>"p","Q"=>"q","R"=>"r","S"=>"s","T"=>"t","U"=>"u","V"=>"v","W"=>"w","X"=>"x","Y"=>"y","Z"=>"z"
);
$result=strtr($str,$translit);
$result=preg_replace("/[^a-zA-Z0-9_]/i","-",$result);
$result=preg_replace("/\-+/i","-",$result);
$result=preg_replace("/^kanal-/i","",$result);
$result=preg_replace("/(^\-)|(\-$)/i","",$result);
return $result;
}
$chname = array();
foreach ($array[channel] as $value) {
$chname[id.$value["id"]] = $value["display-name"];
$qwe = str2url($chname[id.$value["id"]] = $value["display-name"]);
if(!is_dir('./tvprogi/'.$qwe)) mkdir('./tvprogi/'.$qwe);
}
foreach ($array[programme] as $prog) {
$data[start] = substr($prog[start], 0, -6);
$data[title] = $prog[title];
$data[category] = $prog[category];
$data[desc] = $prog[desc];
if ($day != substr($prog[start], 6, -12)) {
$names = str2url($chname[id.$prog[channel]]);
$fp = fopen('./tvprogi/'.$names.'/'.$day.'.json', "w");
/*$fp2 = fopen($dir.'all.json', "w");
fwrite($fp2, json_encode($data));
fclose ($fp2);*/
fwrite($fp, json_encode($tmps));
fclose ($fp);
$tmps = array();
}
$tmps[] = $data;
$day = substr($prog[start], 6, -12);
}
?>
<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE tv SYSTEM "http://www.test.info/download/xmltv.dtd">
<tv generator-info-name="x" generator-info-url="http://test.info/">
<channel id=1">
<display-name lang="ru">Первый канал</display-name>
<icon src="http://test.info/channel/1.gif" />
</channel>
<channel id="105">
<display-name lang="ru">Россия 1</display-name>
<icon src="http://test.info/channel/2.gif" />
</channel>
<channel id="333">
<display-name lang="ru">СрHР 100% NEWs</display-name>
<icon src="http://test.info/channel/3.gif" />
</channel>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="201710186050000 +0300" stop="20171016090000 +0300" channel="1">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="201710186050000 +0300" stop="20171016090000 +0300" channel="105">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171016050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171017050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="20171018050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
<programme start="201710186050000 +0300" stop="20171016090000 +0300" channel="333">
<title lang="ru">Название ТВ передачи</title>
<category lang="ru">Категория</category>
</programme>
</tv>
Answer the question
In order to leave comments, you need to log in
somehow you need to think a little about the future: if now 5 people use the site, then there is nothing wrong with file operations, although it’s not convenient and terribly. and if the site is used by 2000 people at the same time, then this will be 2000 simultaneous readings of the file. also on the "FILE SYSTEM" it is not very convenient to search, edit, add, etc. so spend a little time and do everything in the database, it will be much easier.
The host will not hang because a separate process will be allocated for this script and the maximum that happens the script will die without doing its job and as another outcome it will break, what works based on the data from this script.
If xmltv2.xml is small and there are no algorithmic errors, the script will be executed without problems, you hung up on fs in vain
the size of the xml file is about 4-5 mb.. the script is run once a week, and then everything is stored in statics... I think static files do not create a load on the host? if you compare the html site and the site which, for example, works on mysql? also in many cms the cache is generated to the statics so that queries to the database are not made ?? why then on files it is bad?? I'm interested in the process of the constructor itself, how to optimize it so that during the generation of files it would not load the site so much ??
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question