处理高负载文件 I/O 的最佳实践是什么?
对于具有较高并发负载的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要求客户端与 CSV 文件交互,则不需要实际使用 CSV 文件作为数据存储。相反,使用数据库,在数据库中完成所有工作,并让 PHP 根据需要生成 CSV 文件。
因此,如果客户端需要访问 http://example.com/SUBSCRIBERS.CSV,只需使用 PHP处理 SUBSCRIBERS.CSV 并使用类似以下内容:
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: