From b4447473a845be1ba66beeb38acf74534349486a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 8 May 2010 02:16:29 +0000 Subject: [PATCH] Enhancement to allow to use different caching systems for language files --- htdocs/core/class/translate.class.php | 35 ++++++---- htdocs/lib/memory.lib.php | 96 +++++++++++++++++++-------- 2 files changed, 89 insertions(+), 42 deletions(-) diff --git a/htdocs/core/class/translate.class.php b/htdocs/core/class/translate.class.php index adab8665104..7e0d79dde18 100644 --- a/htdocs/core/class/translate.class.php +++ b/htdocs/core/class/translate.class.php @@ -150,7 +150,7 @@ class Translate { $newdomain = $domain; $modulename = ''; - + // Search if module directory name is different of lang file name if (preg_match('/^([^@]+)?@([^@]+)$/i',$domain,$regs)) { @@ -193,15 +193,23 @@ class Translate { { $found=false; - // Enable cache of lang file in memory (faster but need more memory) - // Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file) - $enablelangcacheinmemory=((isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))?true:false); - //$enablelangcacheinmemory=true; + // Enable cache of lang file in memory (not by default) + $usecachekey=''; + // Using a memcached server + if (! empty($conf->memcached->enabled)) + { + $usecachekey=$langofdir.'_'.$newdomain; + } + // Using cache with shmop. Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file) + else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) + { + $usecachekey=$newdomain; + } - if ($alt == 2 && $enablelangcacheinmemory) + if ($alt == 2 && $usecachekey) { require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php"); - $tmparray=dol_getshmop($newdomain); + $tmparray=dol_getcache($usecachekey); if (is_array($tmparray) && sizeof($tmparray)) { $this->tab_translate=array_merge($this->tab_translate,$tmparray); @@ -217,7 +225,7 @@ class Translate { { if ($fp = @fopen($file_lang,"rt")) { - if ($enablelangcacheinmemory) $tabtranslatedomain=array(); // To save lang in session + if ($usecachekey) $tabtranslatedomain=array(); // To save lang content in cache while ($ligne = fgets($fp,4096)) // Ex: Need 225ms for all fgets on all lang file for Third party page. Same speed than file_get_contents { @@ -246,13 +254,12 @@ class Translate { else { // On stocke toujours dans le tableau Tab en UTF-8 - //if (empty($this->charset_inputfile[$newdomain]) || $this->charset_inputfile[$newdomain] == 'UTF-8') $value=utf8_decode($value); if (empty($this->charset_inputfile[$newdomain]) || $this->charset_inputfile[$newdomain] == 'ISO-8859-1') $value=utf8_encode($value); //print 'XX'.$key; $this->tab_translate[$key]=$value; - if ($enablelangcacheinmemory) $tabtranslatedomain[$key]=$value; // To save lang in session + if ($usecachekey) $tabtranslatedomain[$key]=$value; // To save lang content in cache } } } @@ -260,11 +267,11 @@ class Translate { fclose($fp); $fileread=1; - // To save lang in session - if ($alt == 2 && $enablelangcacheinmemory && sizeof($tabtranslatedomain)) + // To save lang content into session + if ($alt == 2 && $usecachekey && sizeof($tabtranslatedomain)) { require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php"); - $size=dol_setshmop($newdomain,$tabtranslatedomain); + $size=dol_setcache($newdomain,$tabtranslatedomain); } //exit; break; // Break loop on each root dir @@ -520,7 +527,7 @@ class Translate { function get_available_languages($langdir=DOL_DOCUMENT_ROOT,$maxlength=0) { global $conf; - + // We scan directory langs to detect available languages $handle=opendir($langdir."/langs"); $langs_available=array(); diff --git a/htdocs/lib/memory.lib.php b/htdocs/lib/memory.lib.php index f25bab1e2d6..a62d5f4849c 100644 --- a/htdocs/lib/memory.lib.php +++ b/htdocs/lib/memory.lib.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2009-2010 Laurent Destailleur * * 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 @@ -34,6 +34,46 @@ $shmoffset=100; +/** \brief Save data into a memory area shared by all users, all sessions on server + * \param $memoryid Memory id of shared area + * \param $data Data to save + * \return int <0 if KO, Nb of bytes written if OK + */ +function dol_setcache($memoryid,$data) +{ + global $conf; + + // Using a memcached server + if (! empty($conf->memcached->enabled)) + { + } + else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) + { + dol_setshmop($memoryid,$data); + } +} + +/** \brief Read a memory area shared by all users, all sessions on server + * \param $memoryid Memory id of shared area + * \return int <0 if KO, data if OK + */ +function dol_getcache($memoryid) +{ + global $conf; + + // Using a memcached server + if (! empty($conf->memcached->enabled)) + { + } + else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) + { + $data=dol_getshmop($memoryid); + } + + return $data; +} + + /** \brief Return shared memory address used to store dataset with key memoryid * \param $memoryid Memory id of shared area @@ -62,32 +102,6 @@ function dol_listshmop() return $resarray; } -/** \brief Read a memory area shared by all users, all sessions on server - * \param $memoryid Memory id of shared area - * \return int 0=Nothing is done, <0 if KO, >0 if OK - */ -function dol_getshmop($memoryid) -{ - global $shmkeys,$shmoffset; - - if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0; - $shmkey=dol_getshmopaddress($memoryid);; - //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; - $handle=@shmop_open($shmkey,'a',0,0); - if ($handle) - { - $size=trim(shmop_read($handle,0,6)); - if ($size) $data=unserialize(shmop_read($handle,6,$size)); - else return -1; - shmop_close($handle); - } - else - { - return -2; - } - return $data; -} - /** \brief Save data into a memory area shared by all users, all sessions on server * \param $memoryid Memory id of shared area * \param $data Data to save @@ -117,9 +131,35 @@ function dol_setshmop($memoryid,$data) } else { - print 'Error in shmop_open'; + print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size; return -1; } } +/** \brief Read a memory area shared by all users, all sessions on server + * \param $memoryid Memory id of shared area + * \return int <0 if KO, data if OK + */ +function dol_getshmop($memoryid) +{ + global $shmkeys,$shmoffset; + + if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0; + $shmkey=dol_getshmopaddress($memoryid);; + //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; + $handle=@shmop_open($shmkey,'a',0,0); + if ($handle) + { + $size=trim(shmop_read($handle,0,6)); + if ($size) $data=unserialize(shmop_read($handle,6,$size)); + else return -1; + shmop_close($handle); + } + else + { + return -2; + } + return $data; +} + ?>