<?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 nb_cache_db extends dbobject {
public function __construct(){
parent::__construct('cache_newzbin');
if ( !chrono::isinit('nb_import') ){ chrono::init('nb_import'); }
if ( !chrono::isinit('nb_restore') ){ chrono::init('nb_restore'); }
$this->cleanup();
}
private function cleanup(){
$lt = configs::get('newzbinv2', 'lifetime', NULL);
//drop search
$arg=Array('limit'=>(time() - $lt['search']) );
$this->dbquery($arg, "expire_search");
//drop nfo
$arg=Array('limit'=>(time() - $lt['nfo']) );
$this->dbquery($arg, "expire_nfo");
//drop imdb
$arg=Array('limit'=>(time() - $lt['imdb']) );
$this->dbquery($arg, "expire_imdb");
//drop report older than time() - $lt['report'] //but not in cache_rpcbrw table(for later)
$arg=Array('limit'=>(time() - $lt['nfo']) );
$this->dbquery($arg, "expire_report");
return TRUE;
}
public function isexpired($type, $ref){
if ( FALSE === $this->iscached($type, $ref) ){ return TRUE; }
$lifetime = configs::get('newzbinv2', 'lifetime', $type);
if ( FALSE == $lifetime ){ return TRUE; }
$limit = time() - $lifetime;
$arg=Array('id'=>md5($ref), 'limit'=>$limit);
$o = $this->dbquery($arg, $type."_".__FUNCTION__);
//errors::raise("$type:".md5($ref).", is expired ? $o", CORE_LOG_NOTICE, 'NBDB');
return $o;
}
public function iscached($type, $ref){
$arg=Array('id'=>md5($ref));
return $this->dbquery($arg, $type."_".__FUNCTION__);
}
public function import_result($ref, $nbrid){
if ( FALSE === $this->iscached('search', $ref) ){
$arg=Array('ref'=>md5($ref), 'time'=>time(), );
$this->dbquery($arg, 'insert_search');
}
//lock request
$arg=Array('ref'=>md5($ref), 'id'=>$nbrid);
return $this->dbquery($arg, 'append_result');
}
public function drop_results($ref){
$arg=Array('ref'=>md5($ref));
return $this->dbquery($arg, 'drop_results');
}
public function import_report($ref, $data){
$data = nb_fetch::normalize_report($data);
$data = base64_encode(serialize($data));
$arg=Array('id'=>md5($ref), 'data'=>$data, 'time'=>time());
return $this->dbquery($arg, 'insert_report');
}
public function update_report_date($ref){
$arg=Array('id'=>md5($ref), 'time'=>time());
return $this->dbquery($arg, 'update_report_date');
}
public function drop_report($ref){
$arg=Array('id'=>md5($ref));
return $this->dbquery($arg, 'drop_report');
}
public function count_results($ref){
$arg=Array('ref'=>md5($ref));
return $this->dbquery($arg, 'count_results');
}
public function fetch_results($ref, $start, $end){
$arg=Array('ref'=>md5($ref), 'start'=>$start, 'end'=>$end);
return $this->dbquery($arg, 'fetch_results', 'id');
}
public function get_report($id){
$arg=Array('id'=>md5($id) );
$o = $this->dbquery($arg, 'get_report', 'report');
return unserialize(base64_decode($o));
}
public function import_nfo($ref, $data){
$arg=Array('id'=>md5($ref), 'data'=>$data, 'time'=>time());
return $this->dbquery($arg, 'insert_nfo');
}
public function read_nfo($ref){
$arg=Array('id'=>md5($ref) );
return $this->dbquery($arg, 'get_nfo', 'nfo');
}
public function import_imdb($ref, $data){
$arg=Array('id'=>md5($ref), 'data'=>$data, 'time'=>time());
return $this->dbquery($arg, 'insert_imdb');
}
public function read_imdb($ref){
$arg=Array('id'=>md5($ref) );
return $this->dbquery($arg, 'get_imdb', 'meta');
}
}
return TRUE;