简体   繁体   English

PHP缓存页面的特定部分

[英]PHP Cache specific parts of a page

I have sections on a page that require a fair amount of resources that I would like to cache, here is an example of the page. 我在页面上有一些部分需要相当多的资源我想要缓存,这里是一个页面示例。

[=== Some Static HTML ===]
[=== PHP Script 1 ===]
[=== Some Static HTML ===]
[=== PHP Script 2 ===]

I would like to put "PHP Script 1" into a cache file eg script1.html and include it rather than processing the whole script, and the same for Script 2. 我想将“PHP脚本1”放入缓存文件,例如script1.html并包含它而不是处理整个脚本,对于脚本2也是如此。

The problem I have is I can cache the whole page easily and it works but I would like to just cache specific parts (as above) because some things like user session data needs to be live. 我遇到的问题是我可以轻松地缓存整个页面并且它可以工作,但我想只缓存特定部分(如上所述),因为有些事情需要用户会话数据。

I have this class that is meant to be able to stop and start the buffer so I can pull out specific parts without breaking the rest of the page however it doesn't do what I want. 我有这个类,意味着能够停止和启动缓冲区,所以我可以拉出特定的部分,而不会破坏页面的其余部分,但它不能做我想要的。 http://pastebin.com/Ua6DDExw http://pastebin.com/Ua6DDExw

I would like to be able to go like this below, while it would store the section in a file with a simple php inlcude rather hitting the database. 我希望能够像下面这样,虽然它会将该部分存储在一个文件中,其中包含一个简单的php inlcude而不是数据库。

HTML Content

<?php
$cache->start_buffer("cache_name");
// PHP Script
$cache->end_buffer("cache_name");
?>

HTML Content

<?php
$cache->start_buffer("cache_name");
// PHP Script
$cache->end_buffer("cache_name");
?>

I don't have access to memcache or anything else like that because this will be going on shared hosting. 我没有访问memcache或其他任何类似的东西,因为这将在共享主机上进行。

Any help would be great, Thanks 任何帮助都会很棒,谢谢

look into using ob_start() and ob_flush() It does what you are looking to do. 研究使用ob_start()ob_flush()它可以做你想做的事情。 You'll need to manually write it to a file. 您需要手动将其写入文件。 There are cache.php classes out in the wild as well. 在野外也有cache.php类。

http://php.net/manual/en/function.ob-start.php http://php.net/manual/en/function.ob-start.php

<?php  

  $cache_time = 3600; // Time in seconds to keep a page cached  
  $cache_folder = '/cache'; // Folder to store cached files (no trailing slash)  

  // Think outside the box the original said to use the URI instead use something else.
  $cache_filename = $cache_folder.md5(",MyUniqueStringForMyCode"); // Location to lookup or store cached file  

  //Check to see if this file has already been cached  
  // If it has get and store the file creation time  
  $cache_created  = (file_exists($cache_file_name)) ? filemtime($this->filename) : 0;

  if ((time() - $cache_created) < $cache_time) {  
    $storedData = readCacheFile($cache_filename);
  }
  else
  {

    // Alternatively you can ignore the ob_start/get_contents/end_flush code 
    // and just call a function and store it directly to the variable.
    // Start saving stuff
    ob_start();  

    /** do your work here echoing data to the screen */

    $storedData = ob_get_contents();
    ob_end_flush();

    // create the cachefile for the data.
    createCacheFile($cache_filename);
  }


  // Do stuff with $storedData.

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

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