Php Cache technology

Source: Internet
Author: User
Cache technology is available not only in php, but also in many systems to reduce server pressure and database pressure, this article describes how to use php to cache instance files from the php Cache technology entry to the cache instance. the code is as follows :... cache technology is available not only in php, but also in many systems to reduce server pressure and database pressure, this article introduces the php Cache technology to how to use cache instances to describe how to use php to cache instance files,

First look at this cache class, the code is as follows:

 Warn ('The cached file does not exist and cannot be created. you need to manually create it. '); return false ;}$ this-> cache_dir = $ cache_dirname;} function _ destruct () {echo 'cache class bye. ';} function get_url () {if (! Isset ($ _ server ['request _ uri ']) {$ url = $ _ server ['request _ uri'];} else {$ url = $ _ server ['script _ name']; $ url. = (! Emptyempty ($ _ server ['query _ string'])? '? '. $ _ Server ['query _ string']: '';} return $ url;} function warn ($ errorstring) {echo"Error:
" . $errorstring . "
";} Function cache_page ($ pageurl, $ pagedata) {if (! $ Fso = fopen ($ pageurl, 'w') {$ this-> warns ('cache file cannot be opened. '); // trigger_error return false;} if (! Flock ($ fso, lock_ex) {// lock_nb, arrange it to lock $ this-> warns ('The cache file cannot be locked. '); // trigger_error return false;} if (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats $ this-> warns ('Cannot write to the cache file. '); // trigger_error return false;} flock ($ fso, lock_un); // release the lock fclose ($ fso); return true;} function display_cache ($ cachefile) {if (! File_exists ($ cachefile) {$ this-> warn ('The cache file cannot be read. '); // trigger_error return false;} echo 'read cache file :'. $ cachefile; // return unserialize (file_get_contents ($ cachefile); $ fso = fopen ($ cachefile, 'r'); $ data = fread ($ fso, filesize ($ cachefile); fclose ($ fso); return $ data;} function readdata ($ cachefile = 'default_cache.txt ') {$ cachefile = $ this-> cache_dir. "/". $ cachefile; if (file_exists ($ cachefile) & filemtime ($ cachefile)> (time ()-$ this-> expiretime )) {$ data = $ this-> display_cache ($ cachefile);} else {$ data = "from here wo can get it from mysql tutorial database, update time is". Date ('l ds of f y h: I: s ')."The expiration time is :". date ('l ds of f y h: I: s A', time () + $ this-> expiretime ). "----------"; $ this-> cache_page ($ cachefile, $ data);} return $ data ;}}?>

Next I will interrupt this code and explain it line by line.

Program dialysis: this cache class is named cache and has two attributes:

Private $ cache_dir;

Private $ expiretime = 180;

$ Cache_dir is the parent directory of the relative Website Directory of the cache file. $ expiretime (note 1) is the time when the cached data expires. The main idea is as follows:

When the data or file is loaded, the system first determines whether the cached file exists or does not, and returns false. The last modification time and cache time of the file are not greater than the current time, if the cache size is large, it indicates that the cache has not expired. if it is small, false is returned. when false is returned, raw data is read, written to the cache file, and data is returned .,

Next let's look at the program:

 Warn ('cache file does not exist and cannot be created, you need to manually create. '); return false ;}$ this-> cache_dir = $ cache_dirname ;}?>

When the class is started for the first time, construct the default function with the parameter cache file name. if the file does not exist, create a folder with the editing permission and throw an exception when the creation fails. set the $ cache_dir attribute of the cache class to the name of this folder. all the cached files are under this folder. the code is as follows:

 

This is a class destructor. to demonstrate this, we output a string to indicate that we have successfully released the cache class resources. the code is as follows:

 Error:
" . $errorstring . "
";}?>

This method outputs an error message. the code is as follows:

 

This method returns the information of the current url. this is what I see many foreign cms systems do, mainly cache x. php? Page = 1, x. php? Page = 2. for such files, this cache class function is listed here. the code is as follows:

 Warns ('cache file cannot be opened. '); // trigger_error return false;} if (! Flock ($ fso, lock_ex) {// lock_nb, arrange it to lock $ this-> warns ('The cache file cannot be locked. '); // trigger_error return false;} if (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats $ this-> warns ('Cannot write to the cache file. '); // trigger_error return false;} flock ($ fso, lock_un); // release lock fclose ($ fso); return true;}?>

The cache_page method imports the cached file name and data respectively. this is the method for writing data to a file. first, open the file with fopen, call the handle to lock the file, and then use fwrite to write the file, finally, releasing this handle will throw an error in any step. you may see that this comment is written to the byte stream, and serialize is written to other formats. By the way, if we want to put an array, (select query results from mysql database can be written using the serialize function. use unserialize to read the original type. the code is as follows:

 Warn ('The cache file cannot be read. '); // trigger_error return false;} echo 'read cache file :'. $ cachefile; // return unserialize (file_get_contents ($ cachefile); $ fso = fopen ($ cachefile, 'r'); $ data = fread ($ fso, filesize ($ cachefile); fclose ($ fso); return $ data ;}?>

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.