<?php
class Cache { 
	var $expire = 3600;
	
  	function __construct() {
    	foreach (glob(DIR_CACHE . 'cache.*') as $file) {
      		$array = explode('.', end(explode('/', $file)));

      		if ($array[2] < time()) {
				unlink($file);
      		}
    	}
  	}
		
  	function set($key, $value) {
    	$this->delete($key);

    	$file = fopen(DIR_CACHE . 'cache.' . $key . '.' . (time() + $this->expire), 'a');

    	fwrite($file, serialize($value));
		
    	fclose($file);
  	}

  	function get($key) {
    	foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $file) {
      		$handle = fopen($file, 'r');
      		$cache  = fread($handle, filesize($file));
	  
      		fclose($handle);

      		return unserialize($cache);
    	}
  	}
 
  	function delete($key) {
    	foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $file) {
      		unlink($file);
    	}
  	}
}
?>
