0) file_put_contents(static::$ttl_dir.static::normalize_key($key),time()+$ttl); else @unlink(static::$ttl_dir.static::normalize_key($key)); } return (bool)$result; } public static function set($key, $val,$ttl = 0){ if (MEMCACHE_CHECK_KEYS && !is_string($key)) trigger_error("The key needs to be string! (note: not even numbers are accepted)",E_USER_ERROR); if ($result = (@file_put_contents(static::$cache_dir.static::normalize_key($key),gzcompress(serialize($val),3)) !== false)){ if ($ttl > 0) file_put_contents(static::$ttl_dir.static::normalize_key($key),time()+$ttl); else @unlink(static::$ttl_dir.static::normalize_key($key)); } return (bool)$result; } public static function exists($key){ if (MEMCACHE_CHECK_KEYS && !is_string($key)) trigger_error("The key needs to be string! (note: not even numbers are accepted)",E_USER_ERROR); return file_exists(static::$cache_dir.static::normalize_key($key)); } public static function del($key){ if (MEMCACHE_CHECK_KEYS && !is_string($key)) trigger_error("The key needs to be string! (note: not even numbers are accepted)",E_USER_ERROR); @unlink(static::$ttl_dir.static::normalize_key($key)); return @unlink(static::$cache_dir.static::normalize_key($key)); } public static function get($key,&$success = false){ if (MEMCACHE_CHECK_KEYS && !is_string($key)) trigger_error("The key needs to be string! (note: not even numbers are accepted)",E_USER_ERROR); $val = null; $success = ( // success is based on evaluation order to detect unsuccessful event early and prevent later instructions file_exists(static::$cache_dir.static::normalize_key($key)) // file exists? (if not, abort with fail, as in every other step bellow) && ($cnt = file_get_contents(static::$cache_dir.static::normalize_key($key))) !== false // file can be read? && ($ucnt = @gzuncompress($cnt)) !== false // file is uncompressable && ( // the resulted var is: ($val = @unserialize($ucnt)) !== false // not boolean false || $ucnt == serialize(false) // is boolean false, but it should be. ) ); return $val; } public static function clear(){ $dh = opendir(static::$cache_dir); while ($file = readdir($dh)){ if ($file == "." || $file == "..") continue; @unlink(static::$cache_dir.$file); } closedir($dh); $dh = opendir(static::$ttl_dir); while ($file = readdir($dh)){ if ($file == "." || $file == "..") continue; @unlink(static::$ttl_dir.$file); } closedir($dh); return true; } } memcache_fakecache::init();