【发布时间】:2019-09-27 16:09:04
【问题描述】:
我有一个函数可以每隔几分钟在 ~8000 个具有固定名称的 .md 文件上写入 ~120Kb-150Kb HTML 和元数据:
a-agilent-technologies-healthcare-nyse-us-39d4
aa-alcoa-basic-materials-nyse-us-159a
aaau-perth-mint-physical-gold--nyse-us-8ed9
aaba-altaba-financial-services-nasdaq-us-26f5
aac-healthcare-nyse-us-e92a
aadr-advisorshares-dorsey-wright-adr--nyse-us-d842
aal-airlines-industrials-nasdaq-us-29eb
- 如果文件不存在,它会很快生成/写入。
- 但是,如果文件存在,它的运行速度也会慢得多,因为现有文件携带约 150KB 的数据。
我该如何解决这个问题?
我是否在同一目录中生成一个具有新名称的新文件,并在 for 循环中取消链接旧文件?
或者我是否生成一个新文件夹并写入所有文件然后我取消链接以前的目录?这种方法的问题在于,有时 90% 的文件都被重写,而有些文件保持不变。
代码
这个函数在for循环中被调用,你可以在link看到它
public static function writeFinalStringOnDatabase($equity_symbol, $md_file_content, $no_extension_filename)
{
/**
*@var is the MD file content with meta and entire HTML
*/
$md_file_content = $md_file_content . ConfigConstants::NEW_LINE . ConfigConstants::NEW_LINE;
$dir = __DIR__ . ConfigConstants::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
$new_filename = EQ::generateFileNameFromLeadingURL($no_extension_filename, $dir);
if (file_exists($new_filename)) {
if (is_writable($new_filename)) {
file_put_contents($new_filename, $md_file_content);
if (EQ::isLocalServer()) {
echo $equity_symbol . " ???? " . ConfigConstants::NEW_LINE;
}
} else {
if (EQ::isLocalServer()) {
echo $equity_symbol . " symbol MD file is not writable in " . __METHOD__ . " ???? Maybe, check permissions!" . ConfigConstants::NEW_LINE;
}
}
} else {
$fh = fopen($new_filename, 'wb');
fwrite($fh, $md_file_content);
fclose($fh);
if (EQ::isLocalServer()) {
echo $equity_symbol . " front md file does not exit in " . __METHOD__ . " It's writing on the database now ????" . ConfigConstants::NEW_LINE;
}
}
}
【问题讨论】:
-
如果
file_put_contents和fwrite作用相同,为什么还要使用它们?或者让我这样说:你为什么有if (file_exists($new_filename))? -
如果您每隔几分钟生成(大部分)相同的 8000 个文件,在我看来,更好的解决方案是根据要求即时生成它们。 (或者他们不是通过网络请求的?)
-
在 Total 中听起来是个坏主意,因为磁盘分区可能会变慢,然后一个目录中存在许多文件,请参阅 stackoverflow.com/questions/2994544/…
标签: php database file database-design filesystems