php网站预加载或缓存
我有一个基于 php 的网站。 我使用 switch case 来包含不同的页面并进行导航。 我采用了一种方法,使我的索引页面包含导航栏和页脚,
我的问题是,每次我从一个页面导航到另一个页面时,所有内容都会再次加载,并使网站变得沉重。
<?php include('models/header.php'); ?>
<div id="content">
<center>
<div id="switch" align="center">
<?php
switch($view)
{
case 'Index':
include('pages/index.php');
break;
case 'Services':
include('pages/Services.php');
break;
case 'About':
include('pages/about.php');
break;
case 'Contact':
include('pages/contact.php');
break;
case 'Download':
include('pages/download.php');
break;
default:
include('pages/error.php');
}
?>
</div>
</div>
</center>
<br>
<?php include('models/footer.php'); ?>
</div>
有没有一种方法可以设置它,以便这些元素预加载一次并保留在缓存中,以便每次导航到新页面时都不需要加载它们......?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的代码,您实际上不需要需要缓存任何内容,这样做可能会导致实际需要的开销更多。
无论是否缓存,您仍然需要访问一个文件,您的收益将是操作码生成。但是 PHP 仍然需要访问文件系统,除非您使用带有 memcached 解决方案http://en.wikipedia.org/wiki/Ramfs#Linux" rel="nofollow">RAMFS,您不会注意到真正的变化。
但是,您确实需要缓存代码,出于明显的原因,您应该看看 APC,这是 PHP 的操作码缓存。
基本上,它会缓存您对包含文件进行的调用并缓存 PHP 解释器结果。
最后,我实际上建议您阅读加速网站速度的最佳实践< /a> 这将帮助您以一种可能更显着的方式增强用户体验。
Given your code, you actually don't need to cache anything, doing so could lead to more overhead that it is actually needed.
Cached or not cached, you will still need to access a file, which your gain will be the opcode generation. But PHP still needs to access filesystem, except if you use a memcached solution with RAMFS, you won't note a real change.
However, you really need to cache your code, for obvious reasons, you should take a look at APC, which is an opcode cache for PHP.
Basically, it'll cache the calls you make to your included file and cache the PHP interpreter result.
Finally, I actually advise you to give a read to Best Practices for Speeding Up Your Web Site which will help you enhance user experience in a probably more notable way.
如果浏览器选择的话,页面中的元素(例如图像)将完全重新加载。如果您的元素是 PHP 文件,它们通常会完全重新加载,因为 PHP 页面经常更改。
不过,您可以在 PHP 中设置标头,告诉浏览器将页面缓存一段时间。请参阅http://php.net/manual/en/function.header.php 了解更多信息。
The elements in the page (such as images) will be fully reloading if the browser chooses so. If your elements are PHP files, they will generally be reloaded completely as PHP pages often change.
You could set headers in PHP that will tell the browser to cache the page for a certain amount of time, though. See http://php.net/manual/en/function.header.php for more info.
有很多不同的方法可以做到这一点。我建议使用 Smarty。
更新
我想这仍然有点模糊。 Smarty 实际上有一个已编译的模板目录,可以方便地保存。您可以配置 smarty 以多种不同的方式进行缓存,但基本思想是您有一个基于会话 ID 预编译和存储的平面文件。
Theres a lot of different ways to do this. I would recommend using Smarty.
UPDATE
I guess this a bit vague still. Smarty actually has a compiled templates directory that it keeps handy. You can configure smarty to cache in a lot of different ways but the basic idea is that you have a flat file thats precompiled and stored based on session ID.