Php file cache implementation program code _ PHP Tutorial

Source: Internet
Author: User
Php file cache implementation program code. File caching refers to generating a file from the cache. This file can be php, txt, and other files. when I download the file, I will judge the last generation time of the access, if the specified file cache is exceeded, it is used to generate a file from the cache, which can be php, txt, and other files. when I download the file, I will judge the last generation time of the access, if the cache file is re-generated after the specified time, otherwise the cache file will be called directly, which can reduce the query on the mysql database.

File Cache principle

1. serialize the data to be cached through the serialize function and save it directly to the file. When you need to use cached data, read the file content through deserialization, copy the content to the required variable, and then use it. Infrequently modified data can be written to cache files.


File Cache instance

Index. php

The code is as follows:

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' => '123 ',
'GH _ name' => '123 ');
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/', $ md5 = true ){
If ($ md5 ){
$ Md5id = md5 ($ id );
Return CACHE_ROOT. '/'. $ pre. substr ($ md5id,). '/'. substr ($ md5id,). '/'. $ 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 ('folder cannot be created '. $ path );
}
}

Stores the files to be cached in a serialized manner. when used, the files are deserialized for use.

Smarty is a tool used to cache PHP template data. Smarty caches HTML files into a static HTML page. you can use smarty to cache template files.
Example:

The code is as follows:

// Contains the Smarty class library
Require ('libs/Smarty. class. php ');

// Create an object of the Smarty class
$ Smarty = new Smarty;

// Enable cache
$ Smarty-> caching = true;

// Specify the directory for storing cached files
$ Smarty-> cache_dir = "./cache /";

// 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.