How the Cache works

Source: Internet
Author: User
Tags current time php code php tutorial

How the cache works is actually not complicated. Its core idea is: first, we store what needs to be displayed in a text file (that is, a cached file). Then, if a user requests the content of a page, we first check if the corresponding cache (that is, that text file) exists in this page--if it exists and is the most recent cached file, then output the contents of the text file directly to the client for viewing If this page corresponds to a cached file that does not exist or the time that the cache generation does not meet the requirements (too old), execute the corresponding PHP file directly at once and store the display in the cached file. Repeat the above process. As a result, although the PHP code was added, we saved the most from the time that PHP was linked to the database tutorial and then extracted the data.

*/

Import Cache Class
Require_once (' cache.class.php tutorial ');
Create a cache class object CacheManager
$CacheManager = new Cache ();
Call the StartCache method, which indicates the start cache
$CacheManager->startcache ();
All echo content in the following blocks is written as a cache to the cache file
echo "Start Cache example at:". Date (' H:i:s '). " <br/> ";
Sleep (2);
echo "End Cache example at:". Date (' H:i:s '). " <br/> ";

echo "<br/><a href= ' clear.php ' >clean the cache</a><br/>";
All echo content in the above blocks will be written to cache as cache file
$CacheManager->endcache ();


cache.class.php Code

Class Cache {
var $status = true; A value of TRUE indicates that caching is turned off, and the value false indicates that the caching function is off.
var $cacheDir = ' cache/'; Default directory where cached files are stored
var $cacheFile = '; The real file name of the cached file
var $timeOut = 1000; Maximum time limit for repeated use of content
var $startTime = 0; Start time of program execution
var $caching = true; Whether the content needs to be cached, or false to read the cached file contents directly

function Getmicrotime () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}

function StartCache () {
$this->starttime = $this->getmicrotime ();
Ob_start ();
if ($this->status) {
$this->cachefile = $this->cachedir.urlencode ($_server[' Request_uri '));
if ((File_exists ($this->cachefile)) &&
((Fileatime ($this->cachefile) + $this->timeout) > Time ())
{
Reading content from cached files
$handle = fopen ($this->cachefile, "R");
$html = Fread ($handle, FileSize ($this->cachefile));
Fclose ($handle);

Display content
Echo $html;

Show program Execution time
Echo ' <p>total time: '
. Round (($this->getmicrotime ())-($this->starttime), 4). ' </p> ';

Exit program
Exit ();
}
Else
{
Set cache flag caching to True
$this->caching = true;
}
}
}

Function Endcache () {
      if ($this->status) {
          if ($this->caching)
         {
            //First output the page content, and then save the output in the cached file
             $html = Ob_get_clean ();
            Echo $html;
            $handle = fopen ($this->cachefile, ' W ');
            fwrite ($handle, $html);
            fclose ($handle);

Show Page Execution time
Echo ' <p>total time: '. Round (($this->getmicrotime ()-$this->starttime), 4). ' </p> ';
}
}
}

function Cleancache () {
if ($handle = Opendir ($this->cachedir)) {
while (false!== ($file = Readdir ($handle))) {
if (Is_file ($this->cachedir. $file)) unlink ($this->cachedir. $file);
}

Closedir ($handle);
}
}

}


Again a simple PHP cache file Instance code


if ($content = $cache->start ($cache _id))
{
Echo $content;
Exit ();
}

Require_once ' cache/function.php ';

$cacheDir = './pear_cache/';
$cache = new Cache_function (' file ', array (' Cache_dir ' => $cacheDir));
$arr = Array (' Orient ', ' South ', ' West ');
$cache->call (' slowfunction ', $arr);
Echo '
';

$arr = Array (' Orient ', ' South ', ' West ');

Slowfunction ($arr);

  function slowfunction ($arr = null)
{
Echo] a function that is slow to execute:(
";
Echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). '
';
foreach ($arr as $fruit)
{
echo "The Sun is $fruit at this time
";
}
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.