PHP buffer ob_start and page File Cache-PHP source code

Source: Internet
Author: User
In php, cache is divided into many types. functions such as ob_startob_get_flush are used to reduce the load on servers, but they cannot actually reduce server load, we can use the page cache or file cache to truly implement the cache technology. In php, cache is divided into many types. functions such as ob_start ob_get_flush can reduce server load by caching technology, but it cannot actually reduce server load, we can use the page cache or file cache to truly implement the cache technology.

Script ec (2); script

Ob_start ob_get_flush these functions are a type of cache technology, which reduces the pressure on the server. They do not know the concept of obfuscation and cache until they are used in project development,
These functions, such as ob_start ob_get_flush, are techniques used to output strings to the client in programming to prolong the time. Delayed output (when a string is first sent to the buffer, It is output to the browser ), is an output technique. The most common application is the static technology (static cache can be implemented ):


Save the code to be output to the cache before using ob_get_contents (); get the content and write it to the file.

Php ob_start and ob_end_flush () are the buffer output functions of php.

Ob_start ([string output_callback])-open the output buffer. All output information is not directly sent to the browser, but saved in the output buffer. An optional callback function is used to process output result information.

Ob_end_flush-end (send) output buffer content, disable the output buffer.

Php outputs will be stored in a memory maintained by php, which is also known as buffer and cache. Then, when the buffer is full, php will automatically send the data to the web server.

That is to say, each echo does not necessarily output anything, but is saved in the buffer.

Ob_start () can be understood as (but it is actually different from the following statement). This buffer is controlled by the ob _ series functions, that is, PHP will not maintain its own buffer, and will not automatically send the buffer content to the web server until you ob_end () or similar ob operations.

The ob _ function is generally used to capture the current output, which has nothing to do with efficiency. There are many reasons for capturing the output. For example, if I capture the output and cache it into a file, the content of the cache file can be directly read as the output in the next request.

The Code is as follows:

Ob_start ();

2 Content

3 echo ob_get_contents ();

Example

The Code is as follows:
// Open the buffer
Ob_start ();
?>
All php page output
// Retrieve all output content from the php page
$ Content = ob_get_contents ();
// Create a file, open it, and prepare to write
$ Fp = fopen(1_1.html "," w ");
// Write the entire PHP page content to 1.html
Fwrite ($ fp, $ content );
Fclose ($ fp );
?>

Caching is a big concept. It is generally used to solve the high load problem of large websites. As shown in the rain below, caching is a technology implementation, of course, in addition to the memory cache, there are also files and page caches. For example, if you want to configure system variables in uchome, you generally do not need to change the settings to generate files as file caches, the data is directly read by reading files instead of directly reading from the database. These are set to avoid the re-query of data to reduce the access pressure.

There is a lot of information about the PHP cache class on the Internet, but this class should be a simple one that I have seen to meet the requirements. Let's just look at the code!

Instructions for use:

1. instantiation

The Code is as follows:

$ Cache = new Cache ();

2. Set cache time and cache directory

$ Cache = new Cache (60, '/any_other_path/'); the first parameter is the cache seconds, and the second parameter is the Cache path, which can be configured as needed.
By default, the cache duration is 3600 seconds, and the cache directory is cache/

3. Read Cache

The Code is as follows:

$ Value = $ cache-> get ('data _ key'); 4. Write to cache

$ Value = $ cache-> put ('data _ key', 'Data _ value'); Complete instance:

$ Cache = new Cache ();

// Read key value $ key data from the cache
$ Values = $ cache-> get ($ key );

// If no data is cached
If ($ values = false ){
// Insert code here...
// Write data with the key value $ key
$ Cache-> put ($ key, $ values );
} Else {
// Insert code here...
}
Cache. class. php

Class Cache {
Private $ cache_path; // path for the cache
Private $ cache_expire; // seconds that the cache expires

// Cache constructor, optional expiring time and cache path
Public function Cache ($ exp_time = 3600, $ path = "cache /"){
$ This-> cache_expire = $ exp_time;
$ This-> cache_path = $ path;
}

// Returns the filename for the cache
Private function fileName ($ key ){
Return $ this-> cache_path.md5 ($ key );
}

// Creates new cache files with the given data, $ key = name of the cache, data the info/values to store
Public function put ($ key, $ data ){
$ Values = serialize ($ data );
$ Filename = $ this-> fileName ($ key );
$ File = fopen ($ filename, 'w ');
If ($ file) {// able to create the file
Fwrite ($ file, $ values );
Fclose ($ file );
}
Else return false;
}

// Returns cache for the given key
Public function get ($ key ){
$ Filename = $ this-> fileName ($ key );
If (! File_exists ($ filename) |! Is_readable ($ filename) {// can't read the cache
Return false;
}
If (time () <(filemtime ($ filename) + $ this-> cache_expire) {// cache for the key not expired
$ File = fopen ($ filename, "r"); // read data file
If ($ file) {// able to open the file
$ Data = fread ($ file, filesize ($ filename ));
Fclose ($ file );
Return unserialize ($ data); // return the values
}
Else return false;
}
Else return false; // was expired you need to create new
}
}
?>

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.