From 037e9575614f3b92a4c0e7470250864a3d1e877f Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 8 May 2010 14:18:27 +0000 Subject: [PATCH] Change to allow usage of different caching systems --- htdocs/core/class/translate.class.php | 2 +- htdocs/lib/memory.lib.php | 60 ++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/translate.class.php b/htdocs/core/class/translate.class.php index 393ea5b34e6..a7f501f9975 100644 --- a/htdocs/core/class/translate.class.php +++ b/htdocs/core/class/translate.class.php @@ -198,7 +198,7 @@ class Translate { // Using a memcached server if (! empty($conf->memcached->enabled)) { - $usecachekey=$langofdir.'_'.$newdomain; + $usecachekey=$newdomain.'_'.$langofdir; } // 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)) diff --git a/htdocs/lib/memory.lib.php b/htdocs/lib/memory.lib.php index a62d5f4849c..368dd82d3ee 100644 --- a/htdocs/lib/memory.lib.php +++ b/htdocs/lib/memory.lib.php @@ -34,7 +34,8 @@ $shmoffset=100; -/** \brief Save data into a memory area shared by all users, all sessions on server +/** + * \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 @@ -42,18 +43,36 @@ $shmoffset=100; function dol_setcache($memoryid,$data) { global $conf; + $result=0; // Using a memcached server - if (! empty($conf->memcached->enabled)) + if (! empty($conf->memcached->enabled) && class_exists('Memcached')) { + $m=new Memcached(); + $result=$m->addServer($conf->global->MEMCACHED_SERVER, $conf->global->MEMCACHED_PORT); + //$m->setOption(Memcached::OPT_COMPRESSION, false); + $m->add($memoryid,$data); + $rescode=$m->getResultCode(); + if ($rescode == 0) + { + return sizeof($data); + } + else + { + return -$rescode; + } } + // Using shmop else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { - dol_setshmop($memoryid,$data); + $result=dol_setshmop($memoryid,$data); } + + return $result; } -/** \brief Read a memory area shared by all users, all sessions on server +/** + * \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 */ @@ -62,20 +81,38 @@ function dol_getcache($memoryid) global $conf; // Using a memcached server - if (! empty($conf->memcached->enabled)) + if (! empty($conf->memcached->enabled) && class_exists('Memcached')) { + $m=new Memcached(); + $result=$m->addServer($conf->global->MEMCACHED_SERVER, $conf->global->MEMCACHED_PORT); + //$m->setOption(Memcached::OPT_COMPRESSION, false); + $data=$m->get($memoryid); + $rescode=$m->getResultCode(); + //print "memoryid=".$memoryid." - rescode=".$rescode." - date=".sizeof($data)."\n
"; + //var_dump($data); + if ($rescode == 0) + { + return $data; + } + else + { + return -$rescode; + } } + // Using shmop else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { $data=dol_getshmop($memoryid); + return $data; } - return $data; + return 0; } -/** \brief Return shared memory address used to store dataset with key memoryid +/** + * \brief Return shared memory address used to store dataset with key memoryid * \param $memoryid Memory id of shared area * \return int <0 if KO, Memoy address of shared memory for key */ @@ -86,7 +123,8 @@ function dol_getshmopaddress($memoryid) return $shmkeys[$memoryid]+$shmoffset; } -/** \brief Return list of contents of all memory area shared +/** + * \brief Return list of contents of all memory area shared * \return int 0=Nothing is done, <0 if KO, >0 if OK */ function dol_listshmop() @@ -102,7 +140,8 @@ function dol_listshmop() return $resarray; } -/** \brief Save data into a memory area shared by all users, all sessions on server +/** + * \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 @@ -136,7 +175,8 @@ function dol_setshmop($memoryid,$data) } } -/** \brief Read a memory area shared by all users, all sessions on server +/** + * \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 */