Fixes on cache
This commit is contained in:
parent
5c8e9e37f1
commit
37280d4d86
3 changed files with 192 additions and 57 deletions
|
|
@ -60,6 +60,52 @@ class Cache {
|
|||
|
||||
}
|
||||
|
||||
static public function check_cache($md5_query, $model_name)
|
||||
{
|
||||
|
||||
if(!file_exists(Webmodel::$folder_cache.'/'.$md5_query.'_'.$model_name.'.php'))
|
||||
{
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
static public function save_cache($md5_query, $model_name, $query)
|
||||
{
|
||||
|
||||
$file="<?php\n\n";
|
||||
|
||||
$file.="use PhangoApp\PhaModels\Webmodel;\n\n";
|
||||
|
||||
while($row=Webmodel::$model[$model_name]->nocached_fetch_array($query))
|
||||
{
|
||||
|
||||
$file.="Webmodel::\$model['".$model_name."']->arr_cache_row[]=".var_export($row, true).";\n\n";
|
||||
|
||||
}
|
||||
|
||||
file_put_contents(Webmodel::$folder_cache.'/'.$md5_query.'_'.$model_name.'.php', $file);
|
||||
|
||||
}
|
||||
|
||||
static public function file_cache($md5_file, $model_name)
|
||||
{
|
||||
|
||||
include(Webmodel::$folder_cache.'/'.$md5_file.'_'.$model_name.'.php');
|
||||
|
||||
}
|
||||
|
||||
static public function refresh_cache($model_name, $new_id)
|
||||
{
|
||||
|
||||
array_map('unlink', glob(Webmodel::$folder_cache.'/'.'*_'.$model_name.'.php'));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -13,6 +13,8 @@ class SQLClass {
|
|||
|
||||
static public $debug=true;
|
||||
|
||||
static public $persistent='p:';
|
||||
|
||||
static public function print_sql_fail($sql_fail, $server_data='default')
|
||||
{
|
||||
|
||||
|
|
@ -121,7 +123,7 @@ class SQLClass {
|
|||
|
||||
Webmodel::$connection[$server_data]=mysqli_init();
|
||||
|
||||
if ( !( mysqli_real_connect(Webmodel::$connection[$server_data], $host_db, $login_db, $contra_db ) ) )
|
||||
if ( !( mysqli_real_connect(Webmodel::$connection[$server_data], SqlClass::$persistent.$host_db, $login_db, $contra_db ) ) )
|
||||
{
|
||||
|
||||
return false;
|
||||
|
|
@ -133,17 +135,6 @@ class SQLClass {
|
|||
//return Webmodel::$connection;
|
||||
}
|
||||
|
||||
static public function webtsys_pconnect( $host_db, $login_db, $contra_db, $server_data='default' )
|
||||
{
|
||||
|
||||
if ( !( Webmodel::$connection[$server_data] = @mysql_pconnect( $host_db, $login_db, $contra_db ) ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Webmodel::$connection;
|
||||
}
|
||||
|
||||
static public function webtsys_select_db( $db , $server_data='default')
|
||||
{
|
||||
|
||||
|
|
|
|||
184
src/Webmodel.php
184
src/Webmodel.php
|
|
@ -261,6 +261,13 @@ class Webmodel {
|
|||
|
||||
public $count_cache_query=0;
|
||||
|
||||
/**
|
||||
* Property used for count the new queries saved
|
||||
*
|
||||
*/
|
||||
|
||||
public $last_query_md5='';
|
||||
|
||||
/**
|
||||
* Property that define if this model was cached before, if not, obtain the query from the sql db.
|
||||
*
|
||||
|
|
@ -269,12 +276,40 @@ class Webmodel {
|
|||
public $cached=0;
|
||||
|
||||
/**
|
||||
* Property that define the cache type, nosql, cached in memory with memcached or redis, etc.
|
||||
* Property that define the cache method for save the rows in other source, how a file, redis, memcached...
|
||||
*
|
||||
*/
|
||||
|
||||
static public $type_cache='PhangoApp\PhaModels\Cache::file_cache';
|
||||
|
||||
/**
|
||||
* Property that define the cache type, nosql, cached in memory with memcached or redis, etc.
|
||||
*
|
||||
*/
|
||||
|
||||
static public $save_cache='PhangoApp\PhaModels\Cache::save_cache';
|
||||
|
||||
/**
|
||||
* Property that define the method for check if the cached of this query exists
|
||||
*
|
||||
*/
|
||||
|
||||
static public $check_cache='PhangoApp\PhaModels\Cache::check_cache';
|
||||
|
||||
/**
|
||||
* Property that define the method for refresh the cache of this table.
|
||||
*
|
||||
*/
|
||||
|
||||
static public $refresh_cache='PhangoApp\PhaModels\Cache::refresh_cache';
|
||||
|
||||
/**
|
||||
* Property that define the method for check if the cached of this query exists
|
||||
*
|
||||
*/
|
||||
|
||||
public $arr_cache_row=array();
|
||||
|
||||
/**
|
||||
* Property that define if id is modified.
|
||||
*
|
||||
|
|
@ -358,6 +393,12 @@ class Webmodel {
|
|||
|
||||
public $update=0;
|
||||
|
||||
/**
|
||||
* Method for set the method used from cache or directly from the database.
|
||||
*/
|
||||
|
||||
public $method_fetch_array='nocached_fetch_array';
|
||||
|
||||
//Construct the model
|
||||
|
||||
/**
|
||||
|
|
@ -836,6 +877,13 @@ class Webmodel {
|
|||
}
|
||||
else
|
||||
{
|
||||
if($this->cache==1)
|
||||
{
|
||||
|
||||
call_user_func(Webmodel::$refresh_cache, $this->name, $this->insert_id());
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
|
||||
|
|
@ -976,6 +1024,13 @@ class Webmodel {
|
|||
else
|
||||
{
|
||||
|
||||
if($this->cache==1)
|
||||
{
|
||||
|
||||
call_user_func(Webmodel::$refresh_cache, $this->name, $this->insert_id());
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
|
@ -1012,8 +1067,6 @@ class Webmodel {
|
|||
{
|
||||
//Check conditions.., script must check, i can't make all things!, i am not a machine!
|
||||
|
||||
$this->set_phango_connection();
|
||||
|
||||
if(!isset($this->arr_cache_query[$cache_name]) || $this->cache_query==0)
|
||||
{
|
||||
|
||||
|
|
@ -1129,22 +1182,6 @@ class Webmodel {
|
|||
|
||||
//$conditions is a variable where store the result from $arr_select and $arr_extra_select
|
||||
|
||||
/*if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions))
|
||||
{
|
||||
|
||||
$conditions=str_replace('where', '', $conditions);
|
||||
$conditions=str_replace('WHERE', '', $conditions);
|
||||
|
||||
$conditions='WHERE '.$where.' and '.$conditions;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$conditions='WHERE '.$where.' '.$conditions;
|
||||
|
||||
}*/
|
||||
|
||||
if($where!='')
|
||||
{
|
||||
|
||||
|
|
@ -1179,6 +1216,7 @@ class Webmodel {
|
|||
|
||||
}
|
||||
|
||||
//$this->last_query_md5=md5($sql_query);
|
||||
|
||||
if($cache_name!='' && !isset($this->arr_cache_query[$cache_name]))
|
||||
{
|
||||
|
|
@ -1191,12 +1229,43 @@ class Webmodel {
|
|||
if($this->cache==0)
|
||||
{
|
||||
|
||||
$this->method_fetch_array='nocached_fetch_array';
|
||||
|
||||
$this->set_phango_connection();
|
||||
|
||||
$query=SQLClass::webtsys_query($sql_query, $this->db_selected);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$this->arr_cache_row=array();
|
||||
|
||||
$md5_query=md5($sql_query);
|
||||
//Check if this operation is cached, if not cached, send normal query and change type fetch_row and fetch_array.
|
||||
|
||||
$this->method_fetch_array='cached_fetch_array';
|
||||
|
||||
if(!call_user_func(Webmodel::$check_cache, $md5_query, $this->name))
|
||||
{
|
||||
|
||||
//Connect to db for make the caching
|
||||
|
||||
$this->set_phango_connection();
|
||||
|
||||
$query=SQLClass::webtsys_query($sql_query, $this->db_selected);
|
||||
|
||||
//Save the cache
|
||||
|
||||
call_user_func(Webmodel::$save_cache, $md5_query, $this->name, $query);
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Load the rows
|
||||
|
||||
call_user_func(Webmodel::$type_cache, $md5_query, $this->name);
|
||||
|
||||
return $md5_query;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1293,26 +1362,9 @@ class Webmodel {
|
|||
|
||||
}
|
||||
|
||||
/*
|
||||
if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions))
|
||||
{
|
||||
$query=$this->query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected);
|
||||
|
||||
$conditions=str_replace('where', '', $conditions);
|
||||
$conditions=str_replace('WHERE', '', $conditions);
|
||||
|
||||
$conditions='WHERE '.$where.' and '.$conditions;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$conditions='WHERE '.$where.' '.$conditions;
|
||||
|
||||
}*/
|
||||
|
||||
$query=SQLClass::webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected);
|
||||
|
||||
list($count_field)= SQLClass::webtsys_fetch_row($query);
|
||||
list($count_field)= $this->fetch_row($query);
|
||||
|
||||
return $count_field;
|
||||
|
||||
|
|
@ -1396,9 +1448,14 @@ class Webmodel {
|
|||
public function fetch_row($query)
|
||||
{
|
||||
|
||||
$this->set_phango_connection();
|
||||
if($this->cache==0)
|
||||
{
|
||||
|
||||
return SQLClass::webtsys_fetch_row($query);
|
||||
$this->set_phango_connection();
|
||||
|
||||
return SQLClass::webtsys_fetch_row($query);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1411,12 +1468,51 @@ class Webmodel {
|
|||
public function fetch_array($query)
|
||||
{
|
||||
|
||||
$this->set_phango_connection();
|
||||
$func=$this->method_fetch_array;
|
||||
|
||||
return $this->$func($query);
|
||||
|
||||
return SQLClass::webtsys_fetch_array($query);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper function for obtain an associative array from a result of $this->select
|
||||
*
|
||||
* @param mixed $query The result of an $this->select operation
|
||||
*/
|
||||
|
||||
public function nocached_fetch_array($query)
|
||||
{
|
||||
|
||||
|
||||
$this->set_phango_connection();
|
||||
|
||||
return SQLClass::webtsys_fetch_array($query);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper function for obtain an associative array from a result of $this->select from cache
|
||||
*
|
||||
* @param mixed $query The result of an $this->select operation
|
||||
*/
|
||||
|
||||
public function cached_fetch_array($md5_query)
|
||||
{
|
||||
|
||||
/*
|
||||
$this->set_phango_connection();
|
||||
|
||||
return SQLClass::webtsys_fetch_array($query);*/
|
||||
|
||||
list($key, $value)=each($this->arr_cache_row);
|
||||
|
||||
return $value;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper function for obtain the last insert id.
|
||||
*
|
||||
|
|
@ -1566,6 +1662,8 @@ class Webmodel {
|
|||
static public function drop_table($table)
|
||||
{
|
||||
|
||||
$this->set_phango_connection();
|
||||
|
||||
return SQLClass::webtsys_query('drop table '.$table);
|
||||
|
||||
}
|
||||
|
|
@ -1579,7 +1677,7 @@ class Webmodel {
|
|||
static public function escape_string($value)
|
||||
{
|
||||
|
||||
return SQLClass::webtsys_escape_string($value);
|
||||
return addslashes($value);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue