简单的PHP缓存包含3个以上的部分 [英] Simple PHP caching with more than 3 parts

查看:98
本文介绍了简单的PHP缓存包含3个以上的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的PHP缓存(带有ob_start和文件)中,我需要不缓存的部分(每页大于或等于3),而不缓存(.PHP动态内容或.PHP文件)

In a simple PHP caching (with ob_start and files), I need parts (more -or equal- than 3 per page) where don't caching (.PHP dynamic content or .PHP file context-user based).

+-----------------+
| CACHING CONTENT |
|                 |
+-----------------+
|   NO CACHING    |
+-----------------+
| CACHING CONTENT |
+-----------------+
|   NO CACHING    |
+-----------------+
|                 |
| CACHING CONTENT |
+-----------------+

在不缓存部分中,我要包含动态内容。我可以缓存三个cached.html文件(选项1),但我更喜欢每个缓存页面只有一个文件(而不是3页,选项2)。缓存的最佳选择是什么?

In "no caching" parts I want include dynamic content. I can caching in three cached.html files (option 1), but I prefer have only one file per cached page (instead 3 pages, option 2). What is the best option for caching?


  1. 缓存多个文件(head_tag.html,body_part1.html,body_part2.html,body_part3.html ...)和中间动态内容(files.php)。

  2. 缓存到唯一文件,带有一些用于替换动态内容的标签(而且...如何?)

  3. 其他

注意:请不要使用第三种系统解决方案(内存缓存,APC ...)。我需要基于PHP的选项。

NOTE: Please, no third systems solutions (memcached, APC...). I need it from PHP-based option.

推荐答案

您可以将占位符用于页面的非缓存部分,并进行缓存整个页面。例如,整个缓存的页面看起来可能像这样:

You can use placeholders for the non-caching parts of the page, and cache the whole page. For example, the whole cached page could look like:

<html>
... (static content)
#DYNAMIC-CONTENT-NAME#
... (static content)
#SECOND-DYNAMIC-CONTENT-PLACEHOLDER#
... (static content)
</html>

然后在PHP中,您只需获取此缓存页面并将所有占位符替换为动态内容。 / p>

Then in PHP, you would simply obtain this cached page and replace all placeholders with the dynamic content.

// obtain the cached page from storage
$cached_page = get_cached_page();

// generate the HTML for the dynamic content
$dynamic_content = get_dynamic_content();

// replace the placeholders with the actual dynamic content
$page = str_replace('#DYNAMIC-CONTENT-NAME#', $dynamic_content, $cached_page);

// display the resulting page
echo $page;

通过这种方式,您可以根据需要放置任意数量的命名占位符,以获取尽可能多的动态内容根据需要,只需将它们替换为实际内容。

This way, you can place as many named placeholders as you like, for as many pieces of dynamic content as you like, then you simply replace them with the actual content.

这篇关于简单的PHP缓存包含3个以上的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆