Added cache support for queries processing

This commit is contained in:
Antonio de la Rosa 2015-10-27 23:10:45 +01:00
parent 413579c93b
commit 5c8e9e37f1
3 changed files with 366 additions and 155 deletions

65
src/Cache.php Normal file
View file

@ -0,0 +1,65 @@
<?php
namespace PhangoApp\PhaModels;
use PhangoApp\PhaUtils\Utils;
use PhangoApp\PhaModels\Models;
class Cache {
static public function file_cache_query($model_name)
{
Utils::load_config('query_cache_'.$model_name, Webmodel::$folder_cache);
}
static public function save_cache_query()
{
//Utils::load_config('query_cache_'.$model_name, Webmodel::$folder_cache);
foreach(Webmodel::$model as $model)
{
if($model->cache_query==1 && $model->count_cache_query>0)
{
$yes_save=0;
$file="<?php\n\n";
$file.="use PhangoApp\PhaModels\Webmodel;\n\n";
foreach($model->arr_cache_query as $name_cache => $cache_query)
{
$file.="Webmodel::\$model['".$model->name."']->arr_cache_query['".$name_cache."']=\"".addslashes($cache_query)."\";\n\n";
$yes_save++;
}
//Open file
if($yes_save>0)
{
if(!file_put_contents(Webmodel::$folder_cache.'/query_cache_'.$model->name.'.php', $file))
{
echo "Error: cannot access to cache folder for save the query caches...\n";
}
}
//Save file
}
}
}
}
?>

View file

@ -29,7 +29,7 @@ class UserPhangoModel extends Webmodel {
public $password='password';
public $repeat_password='repeat_password';
public function insert($post, $safe_query=0)
public function insert($post, $safe_query=0, $cache_name='')
{
if($this->check_user_exists($post[$this->username], $post[$this->email]))
@ -46,7 +46,7 @@ class UserPhangoModel extends Webmodel {
}
return parent::insert($post, $safe_query);
return parent::insert($post, $safe_query, $cache_name);
}
else
@ -60,7 +60,7 @@ class UserPhangoModel extends Webmodel {
}
public function update($post, $safe_query=0)
public function update($post, $safe_query=0, $cache_name='')
{
if(isset($post[$this->username]) && $post[$this->email])
@ -97,7 +97,7 @@ class UserPhangoModel extends Webmodel {
}
return parent::update($post, $safe_query);
return parent::update($post, $safe_query, $cache_name);
}
else

View file

@ -5,6 +5,10 @@ namespace PhangoApp\PhaModels;
use PhangoApp\PhaI18n\I18n;
use PhangoApp\PhaModels\CoreFields\PrimaryField;
use PhangoApp\PhaModels\Forms\BaseForm;
use PhangoApp\PhaUtils\Utils;
define('ORDER_ASC', 0);
define('ORDER_DESC', 1);
/**
* The most important class for the framework
@ -215,6 +219,48 @@ class Webmodel {
public $cache=0;
/**
* Property that define if the sql queries are cached.
*
*/
public $cache_query=0;
/**
* Property that define the folder where the default caches are saved
*
*/
static public $folder_cache='./cache';
/**
* Property used for define the callback function used for load cache queries.
*
*/
static public $type_cache_query='PhangoApp\PhaModels\Cache::file_cache_query';
/**
* Property used for define the callback function used for save cache queries.
*
*/
static public $save_cache_query='PhangoApp\PhaModels\Cache::save_cache_query';
/**
* Property used for save the queries.
*
*/
public $arr_cache_query=array();
/**
* Property used for count the new queries saved
*
*/
public $count_cache_query=0;
/**
* Property that define if this model was cached before, if not, obtain the query from the sql db.
*
@ -227,7 +273,7 @@ class Webmodel {
*
*/
public $type_cache='fileCache';
static public $type_cache='PhangoApp\PhaModels\Cache::file_cache';
/**
* Property that define if id is modified.
@ -324,7 +370,7 @@ class Webmodel {
*
*/
public function __construct($name_model, $cache=0, $type_cache='fileCache')
public function __construct($name_model, $cache=0, $type_cache='PhangoApp\PhaModels\Cache::file_cache')
{
//Webmodel::$root_model='app/'.$name_model.'/'.Webmodel::$model_path;
@ -344,7 +390,7 @@ class Webmodel {
}
$this->cache=$cache;
$this->type_cache=$type_cache;
Webmodel::$type_cache=$type_cache;
//Global access to models
@ -443,7 +489,14 @@ class Webmodel {
}
//Load file with query cache from this model.
if($this->cache_query==true)
{
call_user_func(Webmodel::$type_cache_query, $this->name);
}
}
/**
@ -557,6 +610,7 @@ class Webmodel {
/**
* This method is used for make raw string queries.
*
* TODO: Add an sql builder cache?. I can create a file with all cache queries and load this when i connect to database.
*/
public function query($sql_query)
@ -638,17 +692,73 @@ class Webmodel {
public function set_order($order_by)
{
if(gettype($order_by)=='array')
{
$arr_order=[];
foreach($order_by as $key => $order)
{
settype($order, 'integer');
$arr_set_order[$order]='ASC';
$arr_set_order=[0 => 'ASC', 1 => 'DESC'];
if(isset($this->components[$key]))
{
$arr_order[]=$key.' '.$arr_set_order[$order];
}
}
$this->order_by='order by '.implode(',', $arr_order);
}
else
{
$this->order_by=$order_by;
}
}
public function set_limit($limit)
{
$this->limit=$limit;
if(gettype($limit)=='array')
{
$c=count($limit);
settype($limit[0], 'integer');
$this->limit='limit '.$limit[0];
if($c==2)
{
$this->limit.=', '.$limit[1];
}
}
else
{
$this->limit=$limit;
}
}
/**
* A simple method for reset the conditions properties of Webmodel
*/
public function reset_conditions()
{
@ -660,11 +770,26 @@ class Webmodel {
}
/**
* A simple method for show the conditions in sql format
*/
public function show_conditions()
{
return $this->conditions;
}
/**
* Simple method for save cache queries.
*/
static public function save_cache_query()
{
call_user_func(Webmodel::$save_cache_query);
}
/**
@ -675,7 +800,7 @@ class Webmodel {
* @param array $post Is an array with data to insert. You have a key that represent the name of field to fill with data, and the value that is the data for fill.
*/
public function insert($post, $safe_query=0)
public function insert($post, $safe_query=0, $cache_name='')
{
$this->set_phango_connection();
@ -748,7 +873,7 @@ class Webmodel {
* @param $conditions is a string containing a sql string beginning by "where". Example: where id=1.
*/
public function update($post, $safe_query=0)
public function update($post, $safe_query=0, $cache_name='')
{
$this->set_phango_connection();
@ -883,12 +1008,15 @@ class Webmodel {
* @param $raw_query If set to 0, you obtain fields from table related if you selected a foreignkey field, if set to 1, you obtain an array without any join.
*/
public function select($arr_select=array(), $raw_query=0)
public function select($arr_select=array(), $raw_query=0, $cache_name='')
{
//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)
{
if(count($arr_select)==0)
{
@ -1041,11 +1169,29 @@ class Webmodel {
$arr_distinct[0]='';
$arr_distinct[1]=' DISTINCT ';
$sql_query='select '.$arr_distinct[$this->distinct].' '.$fields.' from '.$selected_models.' '.$conditions;
}
else
{
$sql_query=$this->arr_cache_query[$cache_name];
}
if($cache_name!='' && !isset($this->arr_cache_query[$cache_name]))
{
$this->count_cache_query++;
$this->arr_cache_query[$cache_name]=$sql_query;
}
if($this->cache==0)
{
$query=SQLClass::webtsys_query('select '.$arr_distinct[$this->distinct].' '.$fields.' from '.$selected_models.' '.$conditions, $this->db_selected);
$query=SQLClass::webtsys_query($sql_query, $this->db_selected);
}
else
{