improved the cache-handeling:

- works now with versioning (only via the db-storage, not files)
- removed a lot unnecessary db- and/or filesystem accesses
This commit is contained in:
Ralf Becker 2002-09-24 10:00:58 +00:00
parent 7751df9f39
commit 43c5ce9318
3 changed files with 102 additions and 29 deletions

View File

@ -408,17 +408,63 @@
return $old; return $old;
} }
/*!
@function read
@abstract Reads an eTemplate from the cache or database / filesystem (and updates the cache)
@param as discripted in soetemplate::read
@result True if a fitting template is found, else False
*/
function read($name,$template='default',$lang='default',$group=0,$version='')
{
$cname = ($template == '' ? 'default' : $template).'/'.$name.($lang == 'default' ? '' : '.'.$lang);
if (isset($GLOBALS['phpgw_info']['etemplate']['cache'][$cname])) function cache_name($name='',$template='default',$lang='default')
{
if (empty($name))
{
$name = $this->name;
$template = $this->template;
$lang = $this->lang;
}
elseif (is_array($name))
{
$template = $name['template'];
$lang = $name['lang'];
$name = $name['name'];
}
if (empty($template))
{
$template = 'default';
}
$cname = $template . '/' . $name . (!empty($lang) && $lang != 'default' ? '.' . $lang : '');
//echo "cache_name('$name','$template','$lang') = '$cname'";
return $cname;
}
/*!
@function store_in_cache()
@abstract stores the etemplate in the cache in phpgw_info
*/
function store_in_cache()
{
//echo "<p>store_in_cache('$this->name','$this->template','$this->lang','$this->version')</p>\n";
$GLOBALS['phpgw_info']['etemplate']['cache'][$this->cache_name()] = $this->as_array(1);
}
function in_cache($name,$template='default',$lang='default',$group=0,$version='')
{
$cname = $this->cache_name($name,$template,$lang);
if (is_array($name))
{
$version = $name['version'];
$name = $name['name'];
}
if (!isset($GLOBALS['phpgw_info']['etemplate']['cache'][$cname]) ||
!empty($version) && $GLOBALS['phpgw_info']['etemplate']['cache'][$cname]['version'] != $version)
{
//echo " NOT found in cache</p>\n";
return False;
}
//echo " found in cache</p>\n";
return $cname;
}
function read_from_cache($name,$template='default',$lang='default',$group=0,$version='')
{
//if (is_array($name)) $version = $name['version']; echo "<p>read_from_cache(,,,version='$version'): ";
if ($cname = $this->in_cache($name,$template,$lang,$group))
{ {
reset($this->db_cols); reset($this->db_cols);
while (list($db_col,$col) = each($this->db_cols)) while (list($db_col,$col) = each($this->db_cols))
@ -427,16 +473,28 @@
} }
$this->rows = count($this->data) - 1; $this->rows = count($this->data) - 1;
$this->cols = count($this->data[1]); // 1 = first row, not 0 $this->cols = count($this->data[1]); // 1 = first row, not 0
echo "\n<!-- $cname read from cache -->\n";
return True; return True;
} }
if (!soetemplate::read($name,$template,$lang,$group,$version)) return False;
return False; }
echo "\n<!-- $cname read & cache updated -->\n"; /*!
$GLOBALS['phpgw_info']['etemplate']['cache'][$cname] = $this->as_array(1); @function read
@abstract Reads an eTemplate from the cache or database / filesystem (and updates the cache)
return true; @param as discripted in soetemplate::read
@result True if a fitting template is found, else False
*/
function read($name,$template='default',$lang='default',$group=0,$version='')
{
if (!$this->read_from_cache($name,$template,$lang,$group,$version))
{
if (!soetemplate::read($name,$template,$lang,$group,$version))
{
return False;
}
$this->store_in_cache();
}
return True;
} }
/*! /*!
@ -447,13 +505,9 @@
*/ */
function save($name='',$template='.',$lang='.',$group='',$version='.') function save($name='',$template='.',$lang='.',$group='',$version='.')
{ {
$result = soetemplate::save($name,$template,$lang,$group,$version); if ($result = soetemplate::save($name,$template,$lang,$group,$version))
if ($result)
{ {
$cname = ($template == '' ? 'default' : $template).'/'.$name.($lang == 'default' ? '' : '.'.$lang); $this->store_in_cache();
$GLOBALS['phpgw_info']['etemplate']['cache'][$cname] = $this->as_array(1);
} }
return $result; return $result;
} }

View File

@ -42,6 +42,7 @@
'import_dump' => True, 'import_dump' => True,
'writeLangFile' => True 'writeLangFile' => True
); );
var $debug; // =1 show some debug-messages, = 'app.name' show messages only for eTemplate 'app.name'
var $name; // name of the template, e.g. 'infolog.edit' var $name; // name of the template, e.g. 'infolog.edit'
var $template; // '' = default (not 'default') var $template; // '' = default (not 'default')
var $lang; // '' if general template else language short, e.g. 'de' var $lang; // '' if general template else language short, e.g. 'de'
@ -74,7 +75,14 @@
$this->db = $GLOBALS['phpgw']->db; $this->db = $GLOBALS['phpgw']->db;
$this->db_cols = $this->db_key_cols + $this->db_data_cols; $this->db_cols = $this->db_key_cols + $this->db_data_cols;
$this->read($name,$template,$lang,$group,$version,$rows,$cols); if (empty($name))
{
$this->init($name,$template,$lang,$group,$version,$rows,$cols);
}
else
{
$this->read($name,$template,$lang,$group,$version,$rows,$cols);
}
} }
/*! /*!
@ -157,7 +165,10 @@
function read($name,$template='default',$lang='default',$group=0,$version='') function read($name,$template='default',$lang='default',$group=0,$version='')
{ {
$this->init($name,$template,$lang,$group,$version); $this->init($name,$template,$lang,$group,$version);
if ($this->debug == 1 || $this->debug == $this->name)
{
echo "<p>soetemplate::read('$this->name','$this->template','$this->lang','$this->version')</p>\n";
}
if ($GLOBALS['phpgw_info']['server']['eTemplate-source'] == 'files' && $this->readfile()) if ($GLOBALS['phpgw_info']['server']['eTemplate-source'] == 'files' && $this->readfile())
{ {
return True; return True;
@ -201,10 +212,15 @@
} }
$sql .= " ORDER BY et_lang DESC,et_template DESC,et_version DESC"; $sql .= " ORDER BY et_lang DESC,et_template DESC,et_version DESC";
if ($this->debug == $this->name)
{
echo "<p>soetemplate::read: sql='$sql'</p>\n";
}
$this->db->query($sql,__LINE__,__FILE__); $this->db->query($sql,__LINE__,__FILE__);
if (!$this->db->next_record()) if (!$this->db->next_record())
{ {
return $this->readfile(); $version = $this->version;
return $this->readfile() && (empty($version) || $version == $this->version);
} }
$this->db2obj(); $this->db2obj();
@ -229,7 +245,10 @@
if ($this->name == '' || $app == '' || $name == '' || !@file_exists($file) || !($f = @fopen($file,'r'))) if ($this->name == '' || $app == '' || $name == '' || !@file_exists($file) || !($f = @fopen($file,'r')))
{ {
//echo "<p>Can't open '$file' !!!</p>\n"; if ($this->debug == 1 || $this->debug == $this->name)
{
echo "<p>Can't open '$file' !!!</p>\n";
}
return False; return False;
} }
$xul = fread ($f, filesize ($file)); $xul = fread ($f, filesize ($file));
@ -239,7 +258,7 @@
{ {
$this->xul_io = CreateObject('etemplate.xul_io'); $this->xul_io = CreateObject('etemplate.xul_io');
} }
if ($this->xul_io->import(&$this,$xul) != '') if (!is_array($this->xul_io->import(&$this,$xul)))
{ {
return False; return False;
} }

View File

@ -51,7 +51,7 @@
$this->html = CreateObject('etemplate.html'); // should be in the api (older version in infolog) $this->html = CreateObject('etemplate.html'); // should be in the api (older version in infolog)
$this->sbox = CreateObject('etemplate.sbox2'); // older version is in the api $this->sbox = CreateObject('etemplate.sbox2'); // older version is in the api
if (!$this->read($name,$template,$lang,$group,$version)) if (empty($name) || !$this->read($name,$template,$lang,$group,$version))
{ {
$this->init($name,$template,$lang,$group,$version,$rows,$cols); $this->init($name,$template,$lang,$group,$version,$rows,$cols);
return False; return False;