PHP file Cache Implementation program code

Source: Internet
Author: User

Principle of File Caching

1. The data that needs to be cached is serialized through the Serialize function and saved directly to the file. When you need to use cached data, read the contents of the file and copy it to the desired variable by deserializing it, and then use the. Data that is infrequently changed can be written to a cached file.


File Cache Instance

index.php

The code is as follows Copy Code

Define (' Cache_root ', './');
Include_once ('./cache.func.php ');
$file = ' Qzp ';
$cacheFile = Getcachefilebyid ($file, ' info/');

Print_r (Readcache ($cacheFile));

$info = Array (
' username ' => ' qzp ',
' Area_name ' => ' admin ',
' Mp_name ' => ' 1234 ',
' Gh_name ' => ' 5678 ');
Writecache ($cacheFile, $info);

cache.func.php file

function Arrayeval ($array, $level = 0) {
$space = ';
for ($i = 0; $i <= $level; $i + +) {
$space. = "T";
}
$evaluate = "Arrayn$space (n");
$comma = $space;
foreach ($array as $key => $val) {
$key = is_string ($key)? "Addcslashes ($key, ' \ ').": $key;
$val =!is_array ($val) && (!preg_match ("/^-?[ 1-9]d*$/", $val) | | strlen ($val) > 12)? "Addcslashes ($val, ' \ ').": $val;
if (Is_array ($val)) {
$evaluate. = "$comma $key =>". Arrayeval ($val, $level + 1);
} else {
$evaluate. = "$comma $key => $val";
}
$comma = ", n$space";
}
$evaluate. = "N$space)";
return $evaluate;
}

function Getcachefilebyid ($id, $pre = ' info/', $md 5=true) {
if ($MD 5) {
$MD 5id = MD5 ($id);
return cache_root. ' /'. $pre. substr ($md 5id,0,2). ' /'. substr ($md 5id,2,2). ' /'. $id;
}else{
return cache_root. ' /'. $pre. $id;
}
}

function Readcache ($filename, $time =0) {
if ($datas = @file_get_contents ($filename)) {
$datas = Unserialize ($datas);
if ($time < 1 | | (Time ()-$datas [' Time '] < $time)) {
return $datas [' data '];
}
}
return false;
}

function Writecache ($filename, $data) {
MakeDir (DirName ($filename));
if ($handle = fopen ($filename, ' w+ ')) {
$datas = Array (' Data ' => $data, ' time ' =>time ());
Flock ($handle, LOCK_EX);
$rs = fputs ($handle, serialize ($datas));
Flock ($handle, lock_un);
Fclose ($handle);
if ($rs!==false) {
return true;
}
}
return false;
}

function MakeDir ($path)
{
if (! file_exists ($path)) {
MakeDir (DirName ($path));
if (! mkdir ($path, 0777))
Die (' Unable to create folder '. $path);
}
}

Store the files to be cached and use them to deserialize them when used.

Use a tool that caches PHP template data to Smarty. Smarty the HTML file into a static HTML page, you need to cache the template file to use Smarty.
Cases:

  code is as follows copy code

<?php
///contains Smarty class library
require (' libs/smarty.class.php ');
 
//Create object for Smarty class
$smarty = new Smarty;
& nbsp
//Enable caching
$smarty->caching = true;
 
//Specify directory for cache file Save
$smarty->cache_dir = "./cache/";
& nbsp
//will also save the output
$smarty->display (' index.tpl ')

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.