PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/htdocs/core/lib/memory.lib.php

https://github.com/asterix14/dolibarr
PHP | 247 lines | 165 code | 18 blank | 64 comment | 31 complexity | dc144bfd10b4b59ad6012046dc2eacbc MD5 | raw file
  1<?php
  2/* Copyright (C) 2009-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation; either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 16 * or see http://www.gnu.org/
 17 */
 18
 19/**
 20 *  \file		htdocs/core/lib/memory.lib.php
 21 *  \brief		Set of function for memory/cache management
 22 */
 23
 24global $shmkeys,$shmoffset;
 25
 26$shmkeys=array('main'=>1,'admin'=>2,'dict'=>3,'companies'=>4,'suppliers'=>5,'products'=>6,
 27				'commercial'=>7,'compta'=>8,'projects'=>9,'cashdesk'=>10,'agenda'=>11,'bills'=>12,
 28				'propal'=>13,'boxes'=>14,'banks'=>15,'other'=>16,'errors'=>17,'members'=>18,'ecm'=>19,
 29				'orders'=>20,'users'=>21,'help'=>22,'stocks'=>23,'interventions'=>24,
 30				'donations'=>25,'contracts'=>26);
 31$shmoffset=100;
 32
 33
 34
 35/**
 36 * 	Save data into a memory area shared by all users, all sessions on server
 37 *
 38 *  @param      $memoryid		Memory id of shared area
 39 * 	@param		$data			Data to save
 40 * 	@return		int				<0 if KO, Nb of bytes written if OK
 41 */
 42function dol_setcache($memoryid,$data)
 43{
 44	global $conf;
 45	$result=0;
 46
 47	// Using a memcached server
 48	if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
 49	{
 50		$memoryid=session_name().'_'.$memoryid;
 51		$m=new Memcached();
 52		$tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
 53		$result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
 54		//$m->setOption(Memcached::OPT_COMPRESSION, false);
 55		//print "Add memoryid=".$memoryid;
 56		$m->add($memoryid,$data);    // This fails if key already exists
 57		$rescode=$m->getResultCode();
 58		if ($rescode == 0)
 59		{
 60			return count($data);
 61		}
 62		else
 63		{
 64			return -$rescode;
 65		}
 66	}
 67	else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
 68	{
 69		$memoryid=session_name().'_'.$memoryid;
 70		$m=new Memcache();
 71		$tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
 72		$result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
 73		//$m->setOption(Memcached::OPT_COMPRESSION, false);
 74		$result=$m->add($memoryid,$data);    // This fails if key already exists
 75		if ($result)
 76		{
 77			return count($data);
 78		}
 79		else
 80		{
 81			return -1;
 82		}
 83	}
 84	// Using shmop
 85	else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
 86	{
 87		$result=dol_setshmop($memoryid,$data);
 88	}
 89
 90	return $result;
 91}
 92
 93/**
 94 * 	Read a memory area shared by all users, all sessions on server
 95 *
 96 *  @param      $memoryid		Memory id of shared area
 97 * 	@return		int				<0 if KO, data if OK
 98 */
 99function dol_getcache($memoryid)
100{
101	global $conf;
102
103	// Using a memcached server
104	if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
105	{
106		$memoryid=session_name().'_'.$memoryid;
107		$m=new Memcached();
108		$tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
109		$result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
110		//$m->setOption(Memcached::OPT_COMPRESSION, false);
111		//print "Get memoryid=".$memoryid;
112		$data=$m->get($memoryid);
113		$rescode=$m->getResultCode();
114		//print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
115		//var_dump($data);
116		if ($rescode == 0)
117		{
118			return $data;
119		}
120		else
121		{
122			return -$rescode;
123		}
124	}
125	else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
126	{
127		$memoryid=session_name().'_'.$memoryid;
128		$m=new Memcache();
129		$tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
130		$result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
131		//$m->setOption(Memcached::OPT_COMPRESSION, false);
132		$data=$m->get($memoryid);
133		//print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
134		//var_dump($data);
135		if ($data)
136		{
137			return $data;
138		}
139		else
140		{
141			return -1;
142		}
143	}
144	// Using shmop
145	else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
146	{
147		$data=dol_getshmop($memoryid);
148		return $data;
149	}
150
151	return 0;
152}
153
154
155
156/**
157 * 	\brief      Return shared memory address used to store dataset with key memoryid
158 *  \param      $memoryid		Memory id of shared area
159 * 	\return		int				<0 if KO, Memoy address of shared memory for key
160 */
161function dol_getshmopaddress($memoryid)
162{
163	global $shmkeys,$shmoffset;
164	if (empty($shmkeys[$memoryid])) return 0;
165	return $shmkeys[$memoryid]+$shmoffset;
166}
167
168/**
169 * 	\brief      Return list of contents of all memory area shared
170 * 	\return		int				0=Nothing is done, <0 if KO, >0 if OK
171 */
172function dol_listshmop()
173{
174	global $shmkeys,$shmoffset;
175
176	$resarray=array();
177	foreach($shmkeys as $key => $val)
178	{
179		$result=dol_getshmop($key);
180		if (! is_numeric($result) || $result > 0) $resarray[$key]=$result;
181	}
182	return $resarray;
183}
184
185/**
186 * 	\brief      Save data into a memory area shared by all users, all sessions on server
187 *  \param      $memoryid		Memory id of shared area
188 * 	\param		$data			Data to save
189 * 	\return		int				<0 if KO, Nb of bytes written if OK
190 */
191function dol_setshmop($memoryid,$data)
192{
193	global $shmkeys,$shmoffset;
194
195	//print 'dol_setshmop memoryid='.$memoryid."<br>\n";
196	if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0;
197	$shmkey=dol_getshmopaddress($memoryid);
198	$newdata=serialize($data);
199	$size=strlen($newdata);
200	//print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
201	$handle=shmop_open($shmkey,'c',0644,6+$size);
202	if ($handle)
203	{
204		$shm_bytes_written1=shmop_write($handle,str_pad($size,6),0);
205		$shm_bytes_written2=shmop_write($handle,$newdata,6);
206		if (($shm_bytes_written1 + $shm_bytes_written2) != (6+dol_strlen($newdata)))
207		{
208   			print "Couldn't write the entire length of data\n";
209		}
210		shmop_close($handle);
211		return ($shm_bytes_written1+$shm_bytes_written2);
212	}
213	else
214	{
215		print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
216		return -1;
217	}
218}
219
220/**
221 * 	\brief      Read a memory area shared by all users, all sessions on server
222 *  \param      $memoryid		Memory id of shared area
223 * 	\return		int				<0 if KO, data if OK
224 */
225function dol_getshmop($memoryid)
226{
227	global $shmkeys,$shmoffset;
228
229	if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0;
230	$shmkey=dol_getshmopaddress($memoryid);;
231	//print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
232	$handle=@shmop_open($shmkey,'a',0,0);
233	if ($handle)
234	{
235		$size=trim(shmop_read($handle,0,6));
236		if ($size) $data=unserialize(shmop_read($handle,6,$size));
237		else return -1;
238		shmop_close($handle);
239	}
240	else
241	{
242		return -2;
243	}
244	return $data;
245}
246
247?>