PHP Simple File Caching Tutorial

Caching is the practice of storing data in and retrieving data from a file, database or memory so that future requests for that data can be served faster. The data stored in a cache might be the result of an earlier computation or duplicate of data. Memory is faster to access than a file, a remote URL, a database or any other external store of information. Besides that, caching is temporarily storing recently used information such as content, which includes HTML pages, images, files and Web objects to make it faster for the user to access it, which helps improve the efficiency of the computer and its overall performance. In this tutorial, I will cover on how to implements PHP simple file caching in your web page.

Add a new project

Start your PHP editor and web server, you can go to download PHPStorm and XMPP. Then open your PHP editor create a new project “php-cache-filing”.

Create a new folder

Right-click the package name and create a new folder “cache”. This folder is to create a cache file after load the web page.

Create a new PHP file

After creating a project, right click your project name and create a new PHP file “example”. The following source code is the sample implementation of caching the file. After load the web page, it will generate a cache file in your cache folder. If the cache folder existing file, it will load from the existing file.

<?php
// Start of code
$time = microtime(true); // Gets microseconds
// a function to receive an write some data into a file
function get_and_write($url, $cache_file) {
	$string = file_get_contents($url);
	$f = fopen($cache_file, 'w');
	fwrite ($f, $string, strlen($string));
	fclose($f);
	return $string;
}

// a function that opens and and puts the data into a single var
function read_content($path) {
	$f = fopen($path, 'r');
	$buffer = '';
	while(!feof($f)) {
		$buffer .= fread($f, 2048);
	}
	fclose($f);
	return $buffer;
}

// our default cache file and URL
$cache_file = 'C:/xampp/htdocs/php-file-cache/cache/cache.page.php';
$url = '//www.linkedin.com/';

if (file_exists($cache_file)) { // is there a cache file?
    $timedif = (time() - filemtime($cache_file)); // how old is the file?
     if ($timedif < 3600*24) { // get a new file 24 hours
        $html = read_content($cache_file); // read the content from cache
    } else { // create a new cache file
        $html = get_and_write($url, $cache_file);
    }
} else { // no file? create a cache file
    $html = get_and_write($url, $cache_file);
}
echo $html;
// End of code
echo "Time Elapsed: ".(microtime(true) - $time)."s";

?>

Run your project

Now, you can run example.php in your browser. It will load a web page, if web page cache not exists, it will generate a new cache file.

Note : you can check the loading speed in the bottom of the page to compare with file caching aprroach.

(PHP Simple File Caching)

Source Code

 

 

(Visited 1,732 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

Leave a Reply

Your email address will not be published. Required fields are marked *