我的PHP文件缓存类

我的PHP缓存其实就是个文件缓存,我写的程序都是在链接数据库之前要判断缓存是否存在,如果存在就调用缓存,不存在再去链接数据库。代码如下:

class cache {
	// CACHEPATH 是我定义的常量,是cache目录
	public static function read($name) {
		if (! file_exists ( CACHEPATH . $name . '.php' )) {
			return FALSE;
		}
		$array = include ( CACHEPATH . $name . '.php' );
		if ($array ['expire'] < time ()) {
			self::delete($name);
			return FALSE;
		} else {
			return $array ['value'];
		}
	}

	public static function write($name, $value, $expire) {
		$data = array ('expire' => time () + $expire, 'value' => $value );
		$str = "";
		file_put_contents ( CACHEPATH . $name . '.php', $str );
	}

	public static function delete($name) {
		return @unlink ( CACHEPATH . $name . '.php' );
	}

	public static function delete_all() {
		$handler = opendir ( CACHEPATH );
		while ( ($fname = readdir ( $handler )) !== FALSE ) {
			if ($fname != '.' && $fname != '..' && $fname != '.svn' && $fname != '.cvs') {
				@unlink ( CACHEPATH . $fname );
			}
		}
		closedir ( $handler );
	}

}

cache::read cache::write 直接调用静态成员函数就可以了

你可能感兴趣的:(php,function,delete,cache,数据库,include)