<?php
if(!defined('R_P'))  die("access die");
class cache
{
	function get($k){
		$k = self::_hash($k);
		$temp = qdb::queryFirst("select v, d from ".TABLEPRE."cache where k = '{$k}' ");
		if(!empty($temp)){
			if($temp['d'] < $_SERVER['REQUEST_TIME']) return false;
			return @unserialize($temp['v']);
		}
		return false;
	}

	function set($k, $v, $time = 86400){
		$k = self::_hash($k);
		$v = addslashes(stripslashes(serialize($v)));
		$d = $time + $_SERVER['REQUEST_TIME'];
		return qdb::query("replace into ".TABLEPRE."cache (k, v, d) values ('{$k}', '{$v}', '{$d}')");
	}

	function delete($k){
		$k = self::_hash($k);
		return qdb::query("delete from ".TABLEPRE."cache where k = '{$k}' ");
	}
	
	function flush(){
		return qdb::query("TRUNCATE TABLE ".TABLEPRE."cache");
	}	

	function _hash($k){
		return sprintf("%u", crc32($k));
	}
}
?>