Enhancement to allow to use different caching systems for language files
This commit is contained in:
parent
014c3dd259
commit
b4447473a8
@ -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();
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
/* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
*
|
||||
* 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."<br>\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."<br>\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;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user