When working on PHP websites made from scratch and without a framework, speed can often be an issue. Caching is extremely useful in order to speed up PHP webpages.
- file cache
- memcached/redis
- APC
Each used for slightly different goal.
File cache is usually something that you utilize when you can pre-render files or parts of them. It is used in templating solutions, partial views (mvc), css frameworks. That sort of stuff.
Memcached and redis are both more or less equal, except redis is more of a noSQL oriented thing. They are used for distributed cache ( multiple servers , same cached data ) and for storing the sessions, if you have cluster of webservers.
APC is good for two things: opcode cache and data cache. Faster then memcached, but works for each server separately.
Bottom line is : in a huge project you will use all of them. Each for a different task.
how to make a simple PHP caching system for your web pages.
Page Caching
1.create a first file for caching (first-cache.php)
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "\n";
readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
?>
- The first five lines create the cached file name according to the current PHP file
- Line six creates a
$cachetime
variable, which determines the life of our simple cache (Cachefile time). - Lines nine to thirteen are a conditional statement which looks for a cache file named
$cachefile
. If the file is found, a comment is inserted (line ten) and the$cachefile
file is included. Then, theexit
statement stops the execution of the script and the file is sent to the client browser. This means that if a static file is found, no PHP is interpreted by the server. - Line 14 creates a buffer if the
$cachefile
file isn’t found.
2.create another new PHP file (second-cache.php)
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
If a cached file named $cachefile (first-cache.php) isn’t found on your server, this code will be executed and will create the cache file itself.
3.Include Cache Files On Your Page
the first-cache.php file must be included at the beginning of your PHP page and the second-cache.php at the end, as shown below:
include('first-cache.php'); // Your regular PHP code goes here include('second-cache.php');
As a result, next time the page will be requested, the $cachefile static file will be served to the client browser instead of executing the whole PHP file.
What is OpCode Cache?
An OpCode cache is a performance-enhancing extension that caches the result of the PHP code compilation to bytecode. Throughout PHP’s lifetime, there have been a number of OpCode caches available, mostly depending on the used PHP version.
How Do I Enable OpCode Cache?
As available OpCode caches depend on the PHP version used on your server, please get in touch with your web hosting provider to see how to enable OpCode caching on your site.