我怎样才能减少 php 脚本的处理器占用空间?

发布于 2024-08-06 11:40:09 字数 299 浏览 5 评论 0原文

我正在尝试制作一个可以加载当前天气预报的 php 脚本,它使用一些 XML 预处理来消化输入,但是它经常被访问并重新加载。问题始于我当前的主机,是的,我确实明白为什么会限制脚本占用的处理能力。

目前需要执行整个流程,每次执行大约需要 3 秒。我限制为 12 个,但我还是收到了相当多的 ping。

我向你们提出的问题是:我可以使用什么方法(如果有的话)来缓存脚本的输出,以便它不必预处理 5 分钟前已经做过的事情。由于天气原因,时差最多可达2小时。

我对 php 也很熟悉,所以不用担心 xD。

~非常感谢, 乔尼:D

I'm attempting to make a php script that can load the current weather forecast and it uses a bit of XML pre-processing to digest the input, however it is accessed quite often and reloaded. The problem begins with my current host, which yes I do understand why, limits the amount of processing power a script takes up.

Currently takes an entire process for ever execution, which is around 3 seconds per execution. I'm limited to 12, yet I get quite a few pings.

My question to you guys is: What methods, if any, can I use to cache the output of a script so that it does not have to pre-process something it already did 5 minutes ago. Since it is weather, I can have a time difference of up to 2 hours.

I am quite familiar with php too, so don't worry xD.

~Thank you very much,
Jonny :D

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

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

发布评论

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

评论(4

我乃一代侩神 2024-08-13 11:40:09

您可以运行一个 cronjob 来生成天气预报数据,然后从缓存中显示整个数据。您可以使用 APC,以便它始终加载到内存中(以及所有其他附加优点)。

You could run a cronjob that would generate the weather forecast data and then just display the whole thing from cache. You could use APC so it is always loaded in memory (plus all other added advantages).

最单纯的乌龟 2024-08-13 11:40:09

Zend Framework 提供了具有多个后端(File、memcached ,APD)。或者你可以自己推出类似的东西:

$cachFile = "/path/to/cache/file";
$ttl = 60; // 60 second time to live
if (!file_exists($cacheFile) || time()-filemtime($cacheFile) > $ttl) {
    $data = getWeatherData(); // Go off and get the data
    file_put_contents(serialize($cacheFile), $data);
} else {
    $data = unserialize(file_get_contents($cacheFile));
}

The Zend Framework provides the Zend_Cache object with multiple backends (File, memcached, APD). Or you can roll your own with something like:

$cachFile = "/path/to/cache/file";
$ttl = 60; // 60 second time to live
if (!file_exists($cacheFile) || time()-filemtime($cacheFile) > $ttl) {
    $data = getWeatherData(); // Go off and get the data
    file_put_contents(serialize($cacheFile), $data);
} else {
    $data = unserialize(file_get_contents($cacheFile));
}
不羁少年 2024-08-13 11:40:09

需要一个代码片段来查看您正在执行什么样的处理。考虑使用 xdebug 来更好地优化您的代码。
您还可以使用 AB 等基准测试工具来查看您的服务器可以处理多少个进程。

有几种不同的缓存机制可用,但如果不知道您正在执行什么样的过程,就很难说......

need a code snippet to see what kind of processing you are doing. consider using xdebug to better optimize your code.
Also you may use a benchmarking tool such as AB to see how many processes your server can handle.

there are several different caching mechanisms available but without seeing what kind of process you are doing it is hard to say...

烟雨扶苏 2024-08-13 11:40:09

3 秒是一个非常长的执行时间,正如已经问过的,一些冷的人会很高兴看到你如何处理“输入”以及输入的格式。

找到一篇关于将脚本缓存到文件的快速而肮脏的文章此处:

http://codestips.com/?p=153

3 seconds is an extremely long execution time, as already asked, some cold would be nice to see how you process the 'input' and in what format said input is in.

A quick and dirty article about caching out of script to file is found here:

http://codestips.com/?p=153

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文