added some caching to not read resources multiple times from the database, specially in listing them, dont query db for every row multiple times

This commit is contained in:
Ralf Becker 2011-10-14 08:36:12 +00:00
parent 7f9bbe99a3
commit bca8179821
2 changed files with 45 additions and 23 deletions

View File

@ -1,6 +1,6 @@
<?php
/**
* eGroupWare - resources
* EGroupware - resources
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package resources
@ -165,7 +165,7 @@ class resources_bo
}
}
}
$rows[$num]['picture_thumb'] = $this->get_picture($resource['res_id']);
$rows[$num]['picture_thumb'] = $this->get_picture($resource);
$rows[$num]['admin'] = $this->acl->get_cat_admin($resource['cat_id']);
}
return $nr;
@ -627,21 +627,18 @@ class resources_bo
/**
* get resource picture either from vfs or from symlink
* Cornelius Weiss <egw@von-und-zu-weiss.de>
* @param int $res_id id of resource
* @param int|array $resource res-id or whole resource array
* @param bool $fullsize false = thumb, true = full pic
* @return string url of picture
*/
function get_picture($res_id=0,$fullsize=false)
function get_picture($resource,$fullsize=false)
{
if ($res_id > 0)
{
$src = $this->so->get_value('picture_src',$res_id);
}
#echo $scr."<br>". $this->pictures_dir."<br>";
switch($src)
if ($resource && !is_array($resource)) $resource = $this->read($resource);
switch($resource['picture_src'])
{
case 'own_src':
$picture = egw_link::vfs_path('resources',$res_id,self::PICTURE_NAME,true); // vfs path
$picture = egw_link::vfs_path('resources',$resource['res_id'],self::PICTURE_NAME,true); // vfs path
if ($fullsize)
{
$picture = egw::link(egw_vfs::download_url($picture));
@ -650,17 +647,17 @@ class resources_bo
{
$picture = egw::link('/etemplate/thumbnail.php',array('path' => $picture));
}
//$picture=$GLOBALS['egw_info']['server'].$picture;
#echo $picture."<br>";
break;
case 'cat_src':
list($picture) = $this->cats->return_single($this->so->get_value('cat_id',$res_id));
list($picture) = $this->cats->return_single($resource['cat_id']);
$picture = unserialize($picture['data']);
if($picture['icon'])
{
$picture = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/images/'.$picture['icon'];
break;
}
// fall through
case 'gen_src':
default :
$picture = $GLOBALS['egw_info']['server']['webserver_url'].$this->resource_icons;
@ -670,7 +667,6 @@ class resources_bo
}
/**
* remove_picture
* removes picture from vfs
*
* Cornelius Weiss <egw@von-und-zu-weiss.de>

View File

@ -1,6 +1,6 @@
<?php
/**
* eGroupWare - resources
* EGroupware - resources
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package resources
@ -35,21 +35,48 @@ class resources_so extends so_sql_cf
*/
function get_value($key,$res_id)
{
return $this->db->select($this->table_name,$key,array('res_id' => $res_id),__LINE__,__FILE__)->fetchColumn();
return $res_id == $this->data['res_id'] ? $this->data[$key] :
$this->db->select($this->table_name,$key,array('res_id' => $res_id),__LINE__,__FILE__)->fetchColumn();
}
/**
* reads resource including custom fields
*
* @param interger $res_id res_id
* @return array/boolean data if row could be retrived else False
* Reimplemented to do some minimal caching (re-use already read data)
*
* @param int|array $res_id res_id
* @return array|boolean data if row could be retrived else False
*/
function read($res_id)
{
// read main data
$resource = parent::read($res_id);
if (is_array($res_id) && count($res_id) == 1 && isset($res_id['res_id'])) $res_id = $res_id['res_id'];
return $resource;
/*if (!is_array($res_id) && $res_id == $this->data['res_id'])
{
error_log(__METHOD__.'('.array2string($res_id).') this->data[res_id]='.array2string($this->data['res_id']).' --> returning this->data');
}
else
{
error_log(__METHOD__.'('.array2string($res_id).') this->data[res_id]='.array2string($this->data['res_id']).' --> returning parent::read()');
}*/
return !is_array($res_id) && $res_id == $this->data['res_id'] ? $this->data : parent::read($res_id);
}
/**
* deletes resource
*
* Reimplemented to do some minimal caching (re-use already read data)
*
* @param int|array $res_id id of resource
* @return int|array affected rows, should be 1 if ok, 0 if an error or array with id's if $only_return_ids
*/
function delete($res_id)
{
if (($ok = parent::delete($res_id)) && !is_array($res_id) && $res_id == $this->data['res_id'])
{
unset($this->data);
}
return $ok;
}
/**
@ -66,5 +93,4 @@ class resources_so extends so_sql_cf
return $res_id;
}
}