Simple PHP caching with more than 3 parts

牧云@^-^@ 提交于 2020-01-15 06:07:25

问题


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 |
+-----------------+

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. Caching in several files (head_tag.html, body_part1.html, body_part2.html, body_part3.html...) and intermediate dynamic content (files.php).
  2. Caching to unique file, with some tag for replace with dynamic content (And... How?)
  3. Other

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


回答1:


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>

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.




回答2:


There is two ways to do this with straight php

Header approch

$cachetime = 60 * 60 * 24 * 7; // 1 Week
header(‘Expires: ‘.gmdate(‘D, d M Y H:i:s’, time()+$expires).’GMT’);

Or by caching the complete file (with includes/content from dynamic content) in your filesystem (Can be used to cache portions of site)

<?php
  $cachefile = "cache/".$reqfilename.".html"; #change $reqfilename to $_SERVER['PHP_SELF'] if you are using in headers, footers, menus files
  $cachetime = 60 * 60 * 24 * 7; // 1 Week
  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
     include($cachefile);
     exit;
  }
  ob_start(); // start the output buffer
?>
.. Your usual PHP script and HTML here ...
<?php
   // open the cache file for writing
   $fp = fopen($cachefile, 'w'); 

   // save the contents of output buffer to the file
   fwrite($fp, ob_get_contents());
   // close the file
   fclose($fp); 
   // Send the output to the browser
   ob_end_flush(); 
?>

What you can also do is cache the files on the users computer by using the header or updating your htaccess with caching information. The htaccess implementation might be different depending on your modules installed on the hosting server. I use:

# Add Expiration
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType text/html "access plus 1 day"
ExpiresByType text/php "access plus 1 day"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 week"
ExpiresByType image/ico "access plus 1 week"
ExpiresByType text/xml "access plus 1 day"


来源:https://stackoverflow.com/questions/7787670/simple-php-caching-with-more-than-3-parts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!