简体   繁体   English

缓存PHP内容的建议

[英]Recommendations for Caching PHP content

So my site is starting to use too much resources. 因此,我的网站开始使用太多资源。 the core of my site is a badly-coded plugin for vbulletin. 我网站的核心是vbulletin编码错误的插件。 Unfortunately it's not that easy to just switch, so I'll just have to try to rewrite some stuff, but most importantly: The main page where different parameters are attached to takes way too much processing power like ?image.php?id=1 and ?image.php?id=2.. and such. 不幸的是,切换并不是那么容易,所以我只需要尝试重写一些东西,但是最重要的是:附加了不同参数的主页占用了太多的处理能力,例如?image.php?id = 1和?image.php?id = 2 ..等。

Anyways, I need something that I can use to cache these pages in html for each id, they don't really change that often, only when comments are posted so it's useless to load them every time. 无论如何,我需要一些可用于将每个ID用html缓存的页面的东西,它们实际上并不会经常更改,只有在发布评论时才这样做,因此每次加载它们都没有用。 I never worked with anything like this before so any tips, hints, software to use are very welcome 我以前从未使用过类似的工具,因此非常欢迎使用任何技巧,提示和软件

EDIT: Reason I'm asking is because my processor usage on my host is starting to reach high and if I get more visitors I might get suspended. 编辑:我问的原因是因为我在主机上的处理器使用率开始很高,如果我得到更多的访问者,我可能会被暂停。

EDIT: I was thinking a system something like this: a database with every ?id in it and the date it was last modified (like comment added or something) if the date is older than the last cached version saved as html, load the html, else run the php script and generate a new cached html. 编辑:我在想这样的系统:一个数据库,其中每个?id以及它的最后修改日期(如添加注释或其他内容),如果该日期早于保存为html的上次缓存版本,则加载html ,否则运行php脚本并生成一个新的缓存的html。 I'm probably gonna have to make something like this myself I guess, just not too sure how to get started on something like that. 我猜我可能要自己做类似的事情,只是不太确定如何开始这样的事情。

You could add these in image.php at the very top 您可以在最顶部的image.php中添加它们

header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822, strtotime("2 day")));

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {

    header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304);
    exit;
}

Where is your real issue now? 您现在真正的问题在哪里? The database calls or serving up static content? 数据库调用还是提供静态内容?

Sounds to me like you should start off with a simple cache using local file system. 在我看来,您应该从使用本地文件系统的简单缓存开始。 Can you cache the pages that you mention being the slowest in their entirety? 您能否缓存您提到的整体上最慢的页面? If so you could write your own simple cache code, just writing out the file to disk and loading it from there when it has not expired/been invalidated. 如果是这样,您可以编写自己的简单缓存代码,只需将文件写到磁盘上,然后在文件未过期/失效时从那里加载。

There are other things like APC, memcaced, which might help you too. 诸如APC之类的其他东西也可能对您有所帮助。 Quick google search will give you the low down there. 快速的Google搜索将为您提供最低的选择。

You could implement the above code for client side caching, as for server side you could store the processed (if vbulletin does something to image / resizing / cropping etc. which causes high memory usage) images into files (in some cache dir) so PHP doesn't have to process them every time the script is called. 您可以将上述代码用于客户端缓存,对于服务器端,您可以将处理后的图像(如果vbulletin进行图像/调整大小/裁剪等操作,这会导致大量内存使用)将图像存储到文件中(在某些缓存目录中)不必在每次调用脚本时都对其进行处理。

Algorithm itself should consist of hashing the $_GET params that are needed for vbulletin such as id=2 or width=250 , height=250 算法本身应包括对vbulletin所需的$ _GET参数进行哈希处理,例如id=2width=250height=250

so code would looke something like... 所以代码看起来像...

$file = 'cache_dir/'.md5('file_cached_'.$_GET['id'].'_w'.$_GET['width'].'_h'.$_GET['height']).'.jpg';
if ( ! file_exists($file))
{

  $image = get_image_by_id($_GET['id']);

  // this is just example...
  $image_blob = vbulletin_heavy_image_processing($image);

  file_put_contents($file, $image_blob);
  echo $image_blob;
}
else 
{
  echo file_get_contents($file);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM