处理高负载文件 I/O 的最佳实践是什么?

发布于 2024-10-09 02:22:06 字数 468 浏览 3 评论 0原文

对于具有较高并发负载的 LAMP 服务器,您推荐的最佳实践是什么,并且我需要处理文件 I/O 而不会过于依赖文件锁?

我的意思是,假设我想要一个 SUBSCRIBERS.CSV 文件,其中包含一堆姓名和电子邮件地址。但我希望人们能够填写表格来取消订阅。取消订阅操作将滚动该文件以删除给定电子邮件地址的匹配行(如果存在)。这在 PHP 中似乎是一个简单的任务,但是当您有大约 10 个人尝试同时取消订阅并添加 10 个新订阅者时会发生什么?这就是我认为 PHP 可能会遇到麻烦的地方,并且可能会由于文件锁定而生成错误,除非 Linux 或 PHP 比我想象的更强大。

请注意,我的客户需要 CSV 文件,而不是数据库表。在数据库表中,这不会是问题,但作为文件 I/O,我可能会遇到潜在的问题,对吧?

(顺便说一句,为了防止身份盗窃,我使用了 .htaccess 技巧,这样人们就无法通过猜测其名称来通过网络下载 CSV —— 它只能通过我的 PHP 脚本或 FTP 访问。)

What is your recommended best practice for a LAMP server with sort of a high simultaneous load and I need to handle file I/O without getting too hung on a file lock?

I mean, let's say I want to have a SUBSCRIBERS.CSV file that has a bunch of names and email addresses in it. But I want people to be able to fill out a form to unsubscribe. The unsubscribe action would scroll through that file to delete a matching line if it exists for a given email address. This seems like a simple task in PHP, but what happens when you have like 10 people trying to unsubscribe at once, and 10 new subscribers being added? That's where I think PHP might run into trouble and an error might be generated due to a file lock, unless Linux or PHP is more capable than I think.

Note my client wants a CSV file, not a database table. In a database table, this would not be a problem, but as file I/O, I might run into a potential issue, right?

(BTW, to prevent identity theft, I use an .htaccess trick so that one can't download the CSV over the web by guessing its name -- it must only be accessed either by my PHP script or by FTP.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-10-16 02:22:06

如果要求客户端与 CSV 文件交互,则不需要实际使用 CSV 文件作为数据存储。相反,使用数据库,在数据库中完成所有工作,并让 PHP 根据需要生成 CSV 文件。

因此,如果客户端需要访问 http://example.com/SUBSCRIBERS.CSV,只需使用 PHP处理 SUBSCRIBERS.CSV 并使用类似以下内容:

header("Content-type: text/csv");
$data = get_subscriber_data();
foreach ($data as $row) {
  // $row is an array of columns
  print implode(',', $row);
}

If the requirement is for the client to interface with a CSV file, you don't need to actually use the CSV file as the datastore. Instead, use a database, do all your work in a database, and let PHP generate the CSV file on demand.

So, if a client needs to access http://example.com/SUBSCRIBERS.CSV, just have PHP handle SUBSCRIBERS.CSV and use something like:

header("Content-type: text/csv");
$data = get_subscriber_data();
foreach ($data as $row) {
  // $row is an array of columns
  print implode(',', $row);
}
驱逐舰岛风号 2024-10-16 02:22:06
header("Content-type: text/csv");
$data = get_subscriber_data();
$fp = fopen('php://stdout', 'w'); 
foreach ($data as $row) {
  // $row is an array of columns
  fputcsv($fp, $row);
}
fclose($fp);
header("Content-type: text/csv");
$data = get_subscriber_data();
$fp = fopen('php://stdout', 'w'); 
foreach ($data as $row) {
  // $row is an array of columns
  fputcsv($fp, $row);
}
fclose($fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文