Location: PHPKode > projects > Simple Way to Usenet > libs/swun/rpc/rpc_cache_db.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class rpc_cache_db extends dbobject {
	

	public function __construct($cnf='cache_rpc'){ parent::__construct($cnf); }
	
	//Status
	public function set_status($rpc, $status){
		$arg=Array('rpc'=>$rpc);
		if ( FALSE === $this->dbquery($arg, 'iscached') ){
			if ( FALSE === $this->dbquery($arg, 'cacherpc')){ return FALSE; }
		}
		$status = base64_encode(serialize($status));
		$arg=Array('rpc'=>$rpc, 'status'=>$status, 'time'=>time() );
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function get_status($rpc){
		$arg=Array('rpc'=>$rpc);
		if ( FALSE === $this->dbquery($arg, 'iscached') ){ return FALSE; }
		$status = $this->dbquery($arg, __FUNCTION__, 'status');
		return unserialize( base64_decode($status) );
	}
	
	public function get_lastreq($rpc){
		$arg=Array('rpc'=>$rpc);
		return $this->dbquery($arg, 'get_statusdate', 'status_date');
	}
	
	public function status_isexpired($rpc, $to){
		$arg=Array('rpc'=>$rpc);
		if ( FALSE === $this->dbquery($arg, 'iscached') ){ return TRUE; }
		$date = $this->dbquery($arg, 'get_statusdate', 'status_date');
		$periode = time() - $date;
		return ($periode > $to);
	}
	
	public function set_lastbrw($rpc, $time){
		$arg=Array('rpc'=>$rpc, 'time'=>$time);
		if ( FALSE === $this->dbquery($arg, 'iscached') ){
			if ( FALSE === $this->dbquery($arg, 'cacherpc')){ return FALSE; }
		}
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function get_lastbrw($rpc){
		$arg=Array('rpc'=>$rpc);
		if ( FALSE === $this->dbquery($arg, 'iscached') ){
			if ( FALSE === $this->dbquery($arg, 'cacherpc')){ return FALSE; }
		}
		return $this->dbquery($arg, __FUNCTION__, 'browser_date');
	}
	
	
	
	
	
	//browser
	public function clean_browser($rpc){
		$arg=Array('rpc'=>$rpc, 'limit'=>$this->get_lastbrw($rpc));
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function hash_iscached($rpc, $catid, $hash){
		$arg=Array('rpc'=>$rpc, 'catid'=>$catid,'hash'=>$hash,);
		return $this->dbquery($arg, __FUNCTION__);
	}
		
	public function insert_item(&$ITEM){
		$c = $ITEM->get_content();		
		$arg=Array(	'rpc'	=>$ITEM->get_rpc(),	
					'hash'	=>$ITEM->get_hash(),
					'catid'	=>$ITEM->get_catid(),
					'title'	=>$ITEM->get_title(),
					'data'	=>base64_encode(serialize($c)), 
					'engine'=>$ITEM->get_engine(),
					'id'	=>$ITEM->get_id(),
					'date'	=>$ITEM->get_date(),
					'size'	=>$c['size'],
					'time'	=>time() );
		unset($c);
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function update_item(&$ITEM){
		$arg=Array(	'rpc'	=>$ITEM->get_rpc(),	
					'catid'	=>$ITEM->get_catid(),
					'hash'	=>$ITEM->get_hash(),
					'title'	=>$ITEM->get_title(),
					'time'	=>time() );
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function listbycat($rpc, $catid, $order, $way){
		$arg=Array('rpc'=>$rpc, 'catid'=>$catid, 'order'=>$order, 'way'=>$way );
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function idisonrpc($rpc, $engine, $id){
		$arg=Array('rpc'=>$rpc, 'id'=>$id, 'engine'=>$engine );
		return $this->dbquery($arg, __FUNCTION__);
	}

	//rate
	public function gotfree_slot(){	
		$arg=Array('time'=>time()-60);
		$c = $this->dbquery($arg, 'count_slot');
		$this->dbquery($arg, 'unlock_slot');
		return ( $c < 5 );
	}
	
	public function lock_slot($reason, $id){
		if ( !self::gotfree_slot() ){ return FALSE; } 
		$arg=Array('time'=>time(), 'why'=>$reason, 'id'=>$id, 'who'=>profile::client());
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function list_slot(){
		$arg=Array('time'=>time()-60);
		while ( FALSE !== ($slot = $this->dbquery($arg, __FUNCTION__)) ){ $o[] = $slot;	}
		if ( isset($o) ){ return $o; }
		return FALSE;
	}  

	
	public function delayed_enqueue($rpc, $id){	
		$arg=Array('time'=>time(), 'rpc'=>$rpc, 'id'=>$id, 'unic'=>CORE::hash());
		return $this->dbquery($arg, 'add_delayed');
	}
	
	public function cancel_delayed($rpc, $unic){	
		$arg=Array('unic'=>$unic, 'rpc'=>$rpc);
		return $this->dbquery($arg, 'delay_delone');
	}
	
	public function delay_getoldest(){	
		$arg=Array();
		return $this->dbquery($arg, __FUNCTION__);
	}
	
	public function list_delayed($rpc=NULL){		
		if ( $rpc === NULL ){
			$arg=Array('rpc'=>$rpc);
			return $this->dbquery($arg, 'list_delayedbyrpc');
		}
		$arg=Array();
		return $this->dbquery($arg, 'list_delayed');
	}
	
	public function id_isdelayed($rpc, $id){		
		$arg=Array('rpc'=>$rpc, 'id'=>$id);
		return $this->dbquery($arg, 'is_delayed');
	}
}
Return current item: Simple Way to Usenet