PHP:更快的 cURL 执行
我有一个使用 cURL 来抓取多个网站内容的应用程序。我想以某种方式优化它。是否可以实现单例设计模式,并以某种方式以某种方式在一定的时间间隔内提供我需要的内容的 URL,这样我只实例化它一次?
现在,我为每个呼叫建立和销毁连接。示例代码将受到高度赞赏。
谢谢。
I have an application that uses cURL to grab the contents of several websites. I'd like to optimize this somehow. Would it be possible to implement a singleton design pattern and somehow feed curl the URLs I need contents for at certain intervals -- such that I only instantiate it once?
Right now, I setup and destroy connections for each call. Sample code would be highly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说,这听起来像是不必要的微优化。对于必须通过互联网从已经超出您控制的资源中获取大量数据的过程,您将节省几分之一微秒的时间。如果您只是想让进程运行得更快,可以尝试并行运行多个下载。
编辑:和/或确保您的卷曲支持压缩内容。
This sounds like unnecessary micro-optimization to me. You'll save a fraction of a microsecond for a process that has to go across the internet to grab a hunk of data from a resource that's already out of your control. If you're simply trying to get the process to run faster, maybe try running multiple downloads in parallel.
Edit: And/or make sure your curl supports compressed content.
我想到了许多可能的解决方案。最简单的可能是构建某种缓存机制。将响应存储在磁盘上,并使用它直到它变得陈旧,然后执行新请求来更新缓存。仅此一点就可以极大地提高你的表现。实现此目的的另一种方法是使用缓存代理服务器。
另一种选择是简单地创建一个 cronjob,使用 wget 每隔几分钟获取所需的内容并将结果存储在磁盘上。然后只需从您的应用程序访问该内容即可。这样,您就无需等待请求完成。
A number of possible solutions come to mind. The easiest one is probably to build in some kind of caching mechanism. Store the response on disk, and use that until it becomes stale, then perform a new request to update the cache. That alone should vastly improve your performance. Another way of implementing this would be to use a caching proxy server.
Another option is to simply create a cronjob with wget fetching the needed content every couple of minutes and storing the result on disk. Then just access that content from your application. That way, you'll never have to wait for a request to finish.
当然,只需使用 curl multi 来运行请求平行线。查看该页面上的示例。
附带说明:这与单例或设计模式无关。虽然静态允许您在 Java 中的请求之间保持持久的应用程序状态,但这在 PHP 中是不可能的。
Sure, just use curl multi to have requests run in parallel. Look at the example on that page.
As a side note: this has nothing to do with singleton or design patterns. While static allows you to keep a persistent application state between requests in Java, this is not possible in PHP.