Php file cache data

Source: Internet
Author: User
When looking at the Internet cafe, because the page contains the movie search function (user input), this function cannot be made static, so it can only be dynamic, when using dynamic data, it will bring a great test to the database and server pressure. Therefore, we can only use the cache data method to cache data in the form of packages... syntaxHighlighter. all ();

 

When looking at the Internet cafe, the movie search function (user input) exists in the page)

This function cannot be made static, so it can only be dynamic. when dynamic, it will bring a great test to the database and server pressure.

Therefore, you can only use the cache data method.

The data cache format includes:

1. cache data to the memory. I believe you will think of Memcached. memcached is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results, so as to speed up dynamic Web applications and improve scalability.

2. use a file to cache data. save the data to the file and save it in the form of key => value. The key indicates the file name. The key must be unique in this place.

Set the file cache time. if it is out of date, obtain data from the database and save it to the file,

The following is a file cache class:

1. cache data

2. get data

3. determine whether the cached data exists

4. delete a cache data

5. clear expired cache data

6. clear cache data

 

Class Inc_FileCache {

Private $ cacheTime = 3600; // Default cache time

Private $ cacheDir = CACHE_DIR; // absolute cache path

Private $ md5 = true; // whether to encrypt the key

Private $ suffix = ". php"; // sets the file suffix.

Public function _ construct ($ config ){

If (is_array ($ config )){

Foreach ($ config as $ key => $ val ){

$ This-> $ key = $ val;

}

}

}

// Set cache

Public function set ($ key, $ val, $ leftTime = null ){

$ Key = $ this-> md5? Md5 ($ key): $ key;

$ LeftTime = $ leftTime? $ LeftTime: $ this-> cacheTime;

! File_exists ($ this-> cacheDir) & mkdir ($ this-> cacheDir, 0777 );

$ File = $ this-> cacheDir. '/'. $ key. $ this-> suffix;

$ Val = serialize ($ val );

@ File_put_contents ($ file, $ val) or $ this-> error (_ line __, "file write failed ");

@ Chmod ($ file, 0777) or $ this-> error (_ line __, "failed to set file permissions ");

@ Touch ($ file, time () + $ leftTime) or $ this-> error (_ line __, "failed to change file time ");

}

// Get cache

Public function get ($ key ){

$ This-> clear ();

If ($ this-> _ isset ($ key )){

$ Key_md5 = $ this-> md5? Md5 ($ key): $ key;

$ File = $ this-> cacheDir. '/'. $ key_md5. $ this-> suffix;

$ Val = file_get_contents ($ file );

Return unserialize ($ val );

}

Return null;

}

// Determine whether the question is valid

Public function _ isset ($ key ){

$ Key = $ this-> md5? Md5 ($ key): $ key;

$ File = $ this-> cacheDir. '/'. $ key. $ this-> suffix;

If (file_exists ($ file )){

If (@ filemtime ($ file)> = time ()){

Return true;

} Else {

@ Unlink ($ file );

Return false;

}

}

Return false;

}

// Delete an object

Public function _ unset ($ key ){

If ($ this-> _ isset ($ key )){

$ Key_md5 = $ this-> md5? Md5 ($ key): $ key;

$ File = $ this-> cacheDir. '/'. $ key_md5. $ this-> suffix;

Return @ unlink ($ file );

}

Return false;

}

// Clear expired cache files

Public function clear (){

$ Files = scandir ($ this-> cacheDir );

Foreach ($ files as $ val ){

If (@ filemtime ($ this-> cacheDir. "/". $ val) <time ()){

@ Unlink ($ this-> cacheDir. "/". $ val );

}

}

}

// Clear all cached files

Public function clear_all (){

$ Files = scandir ($ this-> cacheDir );

Foreach ($ files as $ val ){

@ Unlink ($ this-> cacheDir. "/". $ val );

}

}

Private function error ($ line, $ msg ){

Die ("error file:". _ file _. "/n error line: $ line/n error message: $ msg ");

}

}

 

The call method on the page is as follows:

 

$ CacheFile = new Inc_FileCache (array ('cachetime' => 60, 'suffix '=>'. php '));

// Get the hit list of movies

$ Where = "where pid = 75 ";

$ MoviehotModel = $ this-> getM ('moviehot ');

$ MoviehotCount = $ moviehotModel-> getCount ($ where );

If (! $ CacheFile-> _ isset ($ where. $ moviehotCount. 'moviehot ')){

$ MoviehotResult = $ moviehotModel-> getList ("WHERE pid = 75", '0, 10', "orderby desc ");

If (count ($ moviehotResult)> 0 ){

$ CacheFile-> set ($ where. $ moviehotCount. 'moviehot ', $ moviehotResult );

}

} Else {

$ MoviehotResult = $ cacheFile-> get ($ where. $ moviehotCount. 'moviehot ');

}

$ This-> tpl ['moviehotresresult'] = $ moviehotResult;

 

If you have any good file cache code, share it.

 

From ms_X0828

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.