#native_company# #native_desc#
#native_cta#

MySQL and PHP Chache class

By Halit YESIL
on March 31, 2007

Version: 1.0

Type: Class

Category: Databases

License: GNU General Public License

Description: MySQL and php chache class

unsid : User manuel id key string example session_id()

only method (update check delete read)
id : chache id
sid : chache sid

$obj = new pg_chache(unsid,[ id,[ sid ]]);

Write method
$out = $obj->write(unsid , yourstatus, yourchache );

Read method
$out = $obj->read(id, sid, unsid);

Update method
$out = $obj->update(id, sid, unsid, yourstatus, yourchache);

Check method
$out = $obj->check(id, sid, unsid);

Delete method
$out = $obj->delete(id, sid, unsid);

<?PHP
	
	/*
	*
	*--------DB-----------------------------------
		CREATE TABLE `pg_chache` (
			`chache_id` int(11) NOT NULL auto_increment,
			`chache_sid` varchar(100) collate utf8_unicode_ci NOT NULL default '',
			`chache_unsid` varchar(100) collate utf8_unicode_ci NOT NULL default '',
			`chache_status` varchar(100) collate utf8_unicode_ci NOT NULL default '',
			`chache` longtext collate utf8_unicode_ci NOT NULL,
			`create_date` bigint(15) NOT NULL default '0',
			`update_date` bigint(15) NOT NULL default '0',
			PRIMARY KEY  (`chache_id`),
			KEY `chache_sid` (`chache_sid`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
	*------------------------------------------------
	*-------Methods------------------------------
	*	
	*	write ( unsid , status , chache ) @return chache_sid & chache_id
	*	check( id , sid , unsid ) @return chache_status
	*	read(  id, sid, unsid ) @return *
	*	update( id, sid, unsid, status, chache )  @return true | false
	*	delete( id, sid, unsid  ) @return ture | false
	*	
	*------------------------------------------------
	*/
	class pg_chache
	{
		var $ct = 'upload_chache'; //chache db name
		var $sidChar = 50; //sid character length
		var $expiretime = 3600; //Chache life
		var $id;
		var $sid;
		var $unsid;
		var $sids;
		
		function pg_chache($unsid, $id = false, $sid = false){
			$this->expire();
			if(!$this->checkSid($unsid)) return false;
			$this->unsid = $unsid;
			if(doubleval($id) > 0 && $this->checkSid($sid)){
				$this->id = doubleval($id);
				$this->sid = $sid;
			}else{
				return false;
			}
			return true;
		}
		
		//Create new chache data
		function write($unsid, $status = 'false', $chache = ''){
			$sid = $this->getsid();
			if(!$this->checkSid($sid) || !$this->checkSid($unsid)) return false;
			global $db;
			$sql = "INSERT INTO	
							" . $this->ct . " 
					VALUES(	NULL,
							'".$sid."',
							'".$unsid."',
							'".$status."',
							'".$chache."',
							'".$this->nowDate()."',
							'".$this->nowDate()."')";

			$Q = $db->sql_query($sql);
			if(!$Q) return false;
			
			$Qs = $db->sql_query("SELECT chache_id AS cid FROM ".$this->ct." WHERE chache_sid='".$sid."' AND chache_unsid='".$unsid."' ORDER BY chache_id DESC LIMIT 1");
			if(!$Qs) return false;
			$Ss = $db->sql_numrows($Qs);
			if($Ss != 1) return false;
			$As = $db->sql_fetchrow($Qs);
			$cid = $As['cid'];
			$this->id = $cid;
			$this->unsid = $unsid;
			return array(id=>$cid, sid=>$sid);
		}
		
		//Update cheche data
		function update($id, $sid, $unsid, $status = 'false', $chache = ''){
			if(doubleval($id) <= 0) return false;
			if(!$this->checkSid($sid) || !$this->checkSid($unsid)) return false;
			global $db;
			$sql = "UPDATE 
						" . $this->ct . " 
					SET
						chache_status='" . $status . "',
						chache='" . $chache . "',
						update_date='" . $this->nowDate() . "'
					WHERE chache_id='" . $id . "' AND chache_sid='" . $sid . "' AND chache_unsid='" . $unsid . "' LIMIT 1";
			$Q = $db->sql_query($sql);
			if(!$Q) return false; else return true;
		}
		
		//read chache data
		function read($id, $sid, $unsid){
			if(doubleval($id) <= 0) return false;
			if(!$this->checkSid($sid) || !$this->checkSid($unsid)) return false;
			global $db;
			$sql = "SELECT * FROM " . $this->ct . " WHERE chache_id='".$id."' AND chache_sid='".$sid."' AND chache_unsid='".$unsid."' LIMIT 1";
			$Q = $db->sql_query($sql);
			if(!$Q) return false;
			$S = $db->sql_numrows($Q);
			if($S <= 0)	return false;
			$A = $db->sql_fetchrow($Q);
			return array(	id=>$A['chache_id'],
							sid=>$A['chache_sid'],
							unsid=>$A['chache_unsid'],
							status=>$A['chache_status'],
							chache=>$A['chache'],
							create=>$A['create_date'],
							update=>$A['update_date']);
		}
		
		//Check chache exists data
		function check($id, $sid, $unsid){
			if(doubleval($id) <= 0) return false;
			if(!$this->checkSid($sid) || !$this->checkSid($unsid)) return false;
			global $db;
			$sql = "SELECT COUNT(*) AS total FROM " . $this->ct . " WHERE chache_id='".$id."' AND chache_sid='".$sid."' AND chache_unsid='".$unsid."'";
			$Q = $db->sql_query($sql);
			if(!$Q) return false;
			$S = $db->sql_numrows($Q);
			if($S <= 0)	return false;
			$A = $db->sql_fetchrow($Q);
			return $A['total'];
		}
		
		//Delete chache data
		function delete($id, $sid, $unsid){
			if(doubleval($id) <= 0) return false;
			if(!$this->checkSid($sid) || !$this->checkSid($unsid)) return false;
			global $db;
			
			$sql = "DELETE FROM " . $this->ct . " WHERE (update_date < '" . ( $this->nowDate() - $this->expiretime ) . "') OR (chache_id='".$id."' AND chache_sid='".$sid."' AND chache_unsid='".$unsid."')";
			$Q = $db->sql_query($sql);
			if(!$Q) return false; else return true;
		}
		
		/*##############*/
		function expire(){
			global $db;
			$sql = "DELETE FROM " . $this->ct . " WHERE (update_date < '" . ( $this->nowDate() - $this->expiretime ) . "')";
			$Q = $db->sql_query($sql);
			if(!$Q) return false; else return true;
		}
		
		function nowDate(){
			return time();
		}
		
		function checkSid($sid){
			if(!$sid || $sid === false || !eregi('^[a-zA-Z0-9]+$', $sid))	
				return false;
			else
				return true;
		}
		
		function getsid(){
			if(!$this->sid) $this->sid = $this->generatesid();
			return $this->sid;
		}
		
		function generatesid(){
			if(!is_array($this->sids)) $this->allsid();
			if(!is_array($this->sids)) return false;
			$sids = $this->sids;
			$str = "abcdefghijklmnoprstuvyzxwqABCDEFGHIJKLMNOPRSTUVYZXWQ0123456789";
			$this->sid = '';
			for($l = 0; $l < $this->sidChar; $l++) $this->sid .=substr($str, rand(0, strlen($str)-1), 1);
			return empty($sids[$this->sid]) ? $this->sid : $this->generatesid();
		}
		
		function allsid(){
			global $db;
			
			$sql = "SELECT chache_sid FROM " . $this->ct . " WHERE 1";
			$Q = $db->sql_query($sql);
			if(!$Q){
				$this->sids = false;
				return false;
			}
			
			$S = $db->sql_numrows($Q);
			if($S <= 0){
				$this->sids = array();
				return true;
			}
			
			while($A = $db->sql_fetchrow($Q)){
				$out[$A['chache_sid']] = $A['chache_sid'];
			}
			
			$this->sids = $out;
			return true;
		}
	}
?>