PostgreSQL & PHP Tutorials - Creating a Cache
Viewing page 1 of 2
« | Back |
Next |
»
I decided to create a small caching system for this site to cut down on the number of queries being run.
My system is all object oriented, so it was quite easy to add it in.
Firstly, the cache object!
<?php
class Cache {
/**
* Where to save the cache information.
*
* @see Cache
*
* @var String
*/
var $cache_directory = null;
/**
* Constructor
* This sets up the cache directory and makes sure it's all ready to go.
*
* @param String Cache Directory.
*
* @return void
*/
function __construct($cache_directory=false) {
if (!$cache_directory) {
trigger_error('Cache directory not passed in.', E_USER_ERROR);
}
if (!is_dir($cache_directory)) {
trigger_error('Cache directory does not exist.', E_USER_ERROR);
}
if (!is_writable($cache_directory)) {
trigger_error('Cache directory not writable.', E_USER_ERROR);
}
$this->cache_directory = $cache_directory;
}
/**
* PHP4 constructor.
* Call the PHP5 one.
*
* @see __construct
*/
function Cache($cache_directory=false) {
$this->__construct($cache_directory);
}
/**
* WriteCache
* Write the cache file.
* Creates a temp file, when that's ready to go, renames it to the proper cache filename.
*
* @param String Filename to save the cache in.
* @param String The content to put in the file.
*
* @return boolean Returns true if it could be cached. Returns false if it can't be cached.
*/
function WriteCache($filename=false, $content=false) {
if (!$filename) {
return false;
}
$filename = strtolower($filename);
$fullpath = $this->cache_directory . '/' . $filename;
$temp_cache_file = tempnam($this->cache_directory, 'cache_temp.');
$cache_handle = fopen($temp_cache_file, 'w');
if (!$cache_handle) {
trigger_error('Cannot write file.', E_USER_ERROR);
return false;
}
fputs($cache_handle, $content);
fclose($cache_handle);
rename($temp_cache_file, $this->cache_directory . '/' . $filename);
chmod($this->cache_directory . '/' . $filename, 0644);
return true;
}
/**
* ReadCache
* Reads the cache file from the cache directory.
*
* @param String Filename to read the cache from. If the file doesn't exist, then this returns false.
*
* @return mixed Returns false if the cache doesn't exist. Otherwise, it's read and returned.
*/
function ReadCache($filename=false) {
if (!$filename) {
return false;
}
$filename = strtolower($filename);
$fullpath = $this->cache_directory . '/' . $filename;
if (!is_file($fullpath)) {
return false;
}
return file_get_contents($fullpath);
}
/**
* ClearCache
* Clears the cache.
* You can either pass in a filename to remove
* If you leave it empty, it will clear the whole cache out.
*
* @param String The filename to remove from the cache.
*
* @return mixed Returns false if the cache doesn't exist. Otherwise, it's read and returned.
*/
function ClearCache($filename=false) {
if (!$filename) {
if (!$handle = opendir($this->cache_directory)) {
return false;
}
while (($file = readdir($handle)) !== false) {
if ($file == '.' || $file == '..') continue;
if (is_file($this->cache_directory . '/' . $file)) {
unlink($this->cache_directory . '/' . $file);
}
}
closedir($handle);
return true;
}
$filename = strtolower($filename);
$fullpath = $this->cache_directory . '/' . $filename;
if (!is_file($fullpath)) {
return true;
}
if (unlink($fullpath)) {
return true;
}
return false;
}
}
?>
Viewing page 1 of 2
« | Back |
Next |
»
Avg Rating: 3
Vote Count: 3
