فِعلاً .. لاشيء يضاهي البرمجة الكائنية !
cache.php
كود PHP:
class cache {
var $life_time;
var $cache_dir;
protected $cache_file;
protected $create_new_cache = true;
function cache($life_time = 3600, $cache_dir = 'cache') {
$this->life_time = $life_time;
if (file_exists($cache_dir) == false) {
mkdir($cache_dir);
}
$this->cache_dir = (is_writeable($cache_dir) == true) ? $cache_dir : $_ENV['TEMP'];
$this->cache_file = $this->cache_dir.'/'.md5($_SERVER['PHP_SELF']).'.cache';
if (file_exists($this->cache_file) == true and (time() - $this->life_time) <= filemtime($this->cache_file)) {
$this->create_new_cache = false;
readfile($this->cache_file);
exit();
} else {
ob_start();
}
}
function __destruct() {
if ($this->create_new_cache == true) {
$fp = fopen($this->cache_file, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
}
}
}
require_once('config.php');
$cache = new cache($life_time, $cache_dir);
config.php
كود PHP:
$cache_dir = 'C:\AppServ\www\cache\cache';
$life_time = 10; // seconds
example1.php
كود PHP:
require_once('cache.php');
echo '<h1>'.time().'</h1>';
example2.php
كود PHP:
require_once('cache.php');
echo '<h1>'.date('h:i:s a').'</h1>';
ملفات الـ cache يتم تخزينها في ملف بنفس الإسم ، والإستخدام يتم بإستدعاء الملف cache.php في أول الملف المراد عمل cache له ، في ملف الإعدادت لابد من وضع المسار كاملاً .
أتوجه بالشكر للأستاذ hilaby على جهده في هذا الموضوع ، حقيقةً لقد خرجت بكم هائل من المعلومات ، شكراً لكم جميعاً .
ما رأيكم في تطوير هذا الـ class ليحمل خواص أكثر ؟ ونسميه easycache مثلاً ليصبح شقيقاً للـ easytemplate 
أية ملاحظات أو أفكار لا تترددو في طرحها .
لابد لي من التعمق بشكل أكثر في البرمجة الكائنية وإستعلامات الـ mysql المعقدة