In the articles I have seen before, they either use code to fill up space or use the language used by experts to communicate with experts to scare newcomers away. Therefore, this article tries to explain the overall idea in detail.
The two methods are briefly explained as follows:
First, use PHP's output control function (Output Control) to obtain the static page string, and then write it to a new file.
Instructions for use:
1. Instantiation
The code is as follows | Copy code | ||||
|
The first parameter is the cache seconds, and the second parameter is the cache path , configure as needed.
By default, the cache time is 3600 seconds, and the cache directory is cache/代码如下 | 复制代码 |
$value = $cache->get('data_key');4、写入缓存 $value = $cache->put('data_key', 'data_value');完整实例: $cache = new Cache(); //从缓存从读取键值 $key 的数据 //如果没有缓存数据 class Cache { //cache constructor, optional expiring time and cache path //returns the filename for the cache //creates new cache files with the given data, $key== name of the cache, data the info/values to store //returns cache for the given key |
The code is as follows | Copy code |
$value = $cache->get(' data_key'); 4. Write cache $value = $cache->put('data_key', 'data_value'); Complete example: $cache = new Cache() ;//Read the data of key value $key from cache$values = $cache->get($key);//If there is no cached dataif ($values == false) { //insert code here... //Write the data of key value $key $cache->put($key, $values );} else { //insert code here...}Cache.class.phpclass 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; was expired you need to create new }}?> |
2. Use templates to generate
What is a template? If you have used "Save as Template" in Dreamwerver, you should know that templates are used to unify styles. It only allows you to modify a certain part of the page. Of course, this "certain part" is determined by you. This is what the template mentioned in this article means. (In addition, PHP template technology also includes phplib, smarty, etc., which is not the content of this article)
Combining the concept of templates with this article to be more specific is: the artist first makes a page, and then we Use this page as a template (it should be noted that there is no need to use code like EditRegion3 for this template. This code is a logo made by Dreamwerver to facilitate its own design). Use an HTML code in the place we need to change in this template. Replace with characters that can be distinguished, such as "{title}", "[title]". When generating a static page, you only need to replace the data with these strings. This is what templates are about.
Steps:
1. Create a new php page and an html page [template page]; Note: If the data is called from the database, the data will be Save the form and then generate it in a loop;
2. In the php page, open the html page -> Read the content of the html page -> Replace parameters -> Create (open) a new html page -> Replace The content is written into the new file->Close the new file->Generation successful;
The code is as follows
|
Copy code
|
||||
| $content = fread($open,filesize("template.htm")); //Read the template file content
$newtemp = fopen("1.htm ","w");//Generate, open a non-existent (new) page in writing mode