diff --git a/demo.php b/demo.php index fe648ed..864dde3 100644 --- a/demo.php +++ b/demo.php @@ -8,3 +8,4 @@ $item1="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy Cache::put("lorem",$item1); print_r(Cache::get("lorem")); +print "\n"; diff --git a/inc/Class.PHPCACHER.inc.php b/inc/Class.PHPCACHER.inc.php index 484557c..a2d1311 100644 --- a/inc/Class.PHPCACHER.inc.php +++ b/inc/Class.PHPCACHER.inc.php @@ -2,12 +2,33 @@ class PHPCACHER { static function isCached($item) { + if ( !isset($phpcacher_ttl) ) { $phpcacher_ttl=3600; }; + if ( !isset($phpcacher_path) ) { $phpcacher_path="./cache"; }; + $file=md5($item); } static function put($item,$data) { + if ( !isset($phpcacher_path) ) { $phpcacher_path="./cache"; }; + @mkdir($phpcacher_path,0755,TRUE); + $file=$phpcacher_path."/".md5($item).".data"; + file_put_contents($file,serialize($data)); + return TRUE; } static function get($item) { + if ( !isset($phpcacher_ttl) ) { $phpcacher_ttl=3600; }; + if ( !isset($phpcacher_path) ) { $phpcacher_path="./cache"; }; + $file=$phpcacher_path."/".md5($item).".data"; + if ( file_exists($file) ) { + if ( filemtime($file) > time()-$phpcacher_ttl ) { + return unserialize(file_get_contents($file)); + } + } + return FALSE; } static function del($item) { + if ( !isset($phpcacher_path) ) { $phpcacher_path="./cache"; }; + $file=$phpcacher_path."/".md5($item).".data"; + @unlink($file); + return TRUE; } }