处理 HTTP 请求
如果有来自多个客户端的 HTTP 请求到达 Web 服务器,则请求将按顺序处理。
对于所有 http 请求,我想使用令牌桶系统。 因此,当有第一个请求时,我将一个数字写入文件并增加下一个请求的数字,依此类推。
我不想在数据库中执行此操作,因为数据库大小会增加。
这是执行此操作的正确方法吗? .请建议
编辑:因此,如果用户发布评论,则评论应存储在文件中而不是数据库中。因此,为了跟踪它,有一个变量会针对每个请求递增。该数字将用于书面形式文件名并参考它以供将来参考。所以如果有很多请求,这是正确的方法吗..
谢谢..
If there is a HTTP request coming to a web server from many clients the requests will be handled in the order.
For all the http request i want to use a token bucket system.
So when there is a first Request i write a number to a file and increment the number for the next request and so on..
I dont want to do it in DB since the DB size increases..
Is this the right way to do this.Please suggest
Edit:So if a user posts a comment the comment should be stored in the a file instead of the DB.So to keep track of it there is a variable that is incremented for every request.this number will be used in writing the file name and refer it for future reference.so if there are many requests is this the right way to do it..
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不锁定( http://php.net/manual/en/function.flock. php)文件夹中的文件?
基本上每个 php 脚本都会尝试锁定它可以锁定的第一个文件,完成后它会解锁/删除该文件。
我在一个由“进程管理器”生成 250 多个子进程的系统中使用它。尝试使用数据库,但它减慢了一切。
如果您想继续增加某些内容的文件编号,我建议使用 mktime() 或 time() 并使用
但同样,根据您想要如何读取数据或使用它,有很多选项。您能否提供更多详细信息?
-----编辑1-----
虽然不需要,但我通常会保留它以防万一:)。
这应该创建一个txt文件30/69848968695_01_0.txt和..02_0.txt和..03_0.txt。
当您想显示评论时,只需按文件名对它们进行排序......
Why not lock ( http://php.net/manual/en/function.flock.php ) files in a folder ?
Basically each php script tries to lock the first file it can and when it's done it unlocks/erases the file.
I use this in a system with 250+ child processes spawned by a "process manager". Tried to use a database but it slowed down everything.
If you want to keep incrementing the file number for some content i would suggest using mktime() or time() and using
But again, depending on how you want to read the data or use it, there are many options. Could you provide more details?
-----EDIT 1-----
The while should not be neede but i usually keep it anyway just in case :).
That should create a txt file 30/69848968695_01_0.txt and ..02_0.txt and ..03_0.txt.
When you want to show the comments you just sort them by filename....
数据库大小不需要增加。您所需要的只是一行。从概念上讲,逻辑是这样的:
请注意,您正在使用数据库锁来处理同时处理多个请求的可能性。
所以我建议使用数据库作为管理计数的地方。如果您愿意,您仍然可以将其他数据写入文件。但是,您仍然需要对文件进行整理。数据库更难吗?
The database size need not increase. All you need is a single row. In concept the logic goes:
Note that you're using the database locks to deal with the possibilities that multiple requests are being processed at the same time.
So I'm suggesting to use the database as the place to manage your count. You can still write your other data to files if you wish. However you'll still need housekeeping for the files. Is that much harder with a database?
我同意其他一些评论者的观点,即无论您试图解决哪个问题,您都可能使问题变得比需要的更加困难。
您的示例提到将注释放入文件中并将它们保留在数据库之外。
您计数的目的到底是什么?您想统计用户发表的评论数量吗?或者确切的评论请求总数?
如果您不需要实时更新任何地方的计数,您可以编写一个简单的脚本来读取服务器访问日志并累加总数。
此外,马修在上面指出,如果您希望以特定顺序处理请求,您将很快遇到奇怪的并发错误和性能问题。
如果您更新帖子以更明确地包含详细信息,我们应该能够为您提供进一步的帮助。
希望这有帮助。
I agree with some of the other commenters that, regardless of whichever problem you are trying to solve, you may be making it more difficult than it needs to be.
Your example is mentioning putting comments in a file and keeping them outside the database.
What is the purpose of your count, exactly? You want to count the number of comments a user has made? Or total number of comment requests exactly?
If you don't have to update the count anywhere in real time, you could write a simple script that reads your server access logs and adds up the total.
Also, Matthew points out above, if you want requests to be handled in a particular order you will be rapidly heading for strange concurrency bugs and performance issues.
If you update your post to include details more explicitly, we should be able to help you further.
Hope this helps.