What is the best way to cache files in php?

I use Smarty with my php code and I like to cache some pages of the website, so I used the following code:

// TOP of script ob_start(); // start the output buffer $cachefile ="cache/cachefile.html"; // normal PHP  $smarty->display('somefile.tpl.html') ; $fp = fopen($cachefile, 'w'); // open the cache file for writing fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file fclose($fp); // close the file ob_end_flush(); // Send the output to the browser 

but when I print ob_get_contents () at the end of the php file, it is empty! and the cache file actually created is also empty! since I can cache files in php when I use smarty, I know that I can use Smarty cache, but for some reason this does not work for me.

Also, give me information about the APC cache. how to use it? it is worth using in my case, Iโ€™m thin only for caching database queries, I read the php manual about it, but I canโ€™t get anything :) tanks.

+4
source share
2 answers

I pushed part of the code from the documentation (here here ) for a more complete example of smarty cache. Also, I'm not sure what you used in your example, but you should use smarty methods to manage the cache.

  require('Smarty.class.php'); $smarty = new Smarty; // 1 Means use the cache time defined in this file, // 2 means use cache time defined in the cache itself $smarty->caching = 2; // set the cache_lifetime for index.tpl to 5 minutes $smarty->cache_lifetime = 300; // Check if a cache exists for this file, if one doesn't exist assign variables etc if(!$smarty->is_cached('index.tpl')) { $contents = get_database_contents(); $smarty->assign($contents); } // Display the output of index.tpl, will be from cache if one exists $smarty->display('index.tpl'); // set the cache_lifetime for home.tpl to 1 hour $smarty->cache_lifetime = 3600; // Check if a cache exists for this file, if one doesn't exist assign variables etc if(!$smarty->is_cached('home.tpl')) { $contents = get_database_contents(); $smarty->assign($contents); } // Display the output of index.tpl, will be from cache if one exists $smarty->display('home.tpl'); 

As for the APC cache, it will work just as smarty does. They both store data in a file for a certain amount of time. Each time you want to access data, it checks if the cache is valid, and if so returns the value of the cache.

However, if you are not using smarty, you can use APC as such:
This example goes through storing the result of the database query in a cache, similarly, you can change this to instead store all the output of the page, so you donโ€™t have to often perform expensive PHP functions.

 // A class to make APC management easier class CacheManager { public function get($key) { return apc_fetch($key); } public function store($key, $data, $ttl) { return apc_store($key, $data, $ttl); } public function delete($key) { return apc_delete($key); } } 

Combined with some logic

 function getNews() { $query_string = 'SELECT * FROM news ORDER BY date_created DESC limit 5'; // see if this is cached first... if($data = CacheManager::get(md5($query_string))) { // It was stored, return the value $result = $data; } else { // It wasn't stored, so run the query $result = mysql_query($query_string, $link); $resultsArray = array(); while($line = mysql_fetch_object($result)) { $resultsArray[] = $line; } // Save the result inside the cache for 3600 seconds CacheManager::set(md5($query_string), $resultsArray, 3600); } // Continue on with more functions if necessary } 

This example is slightly modified from here .

+1
source

Do you mean that you call ob_get_contents () again after calling ob_end_flush ()? If so, then the material you wrote in the file will be โ€œdeletedโ€ from PHP memory.

If you want to output HTML code, first store ob_end_flush in a variable, and then pass this to fwrite. You can use this variable later on the page.

+1
source

All Articles