dolibarr  7.0.0-beta
memory.lib.php
Go to the documentation of this 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 3 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 
24 global $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=1000; // Max number of entries found into a language file. If too low, some entries will be overwritten.
32 
33 
34 
42 function 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  global $dolmemcache;
51  if (empty($dolmemcache) || ! is_object($dolmemcache))
52  {
53  $dolmemcache=new Memcached();
54  $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
55  $result=$dolmemcache->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
56  if (! $result) return -1;
57  }
58 
59  $memoryid=session_name().'_'.$memoryid;
60  //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
61  $dolmemcache->add($memoryid,$data); // This fails if key already exists
62  $rescode=$dolmemcache->getResultCode();
63  if ($rescode == 0)
64  {
65  return count($data);
66  }
67  else
68  {
69  return -$rescode;
70  }
71  }
72  else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
73  {
74  global $dolmemcache;
75  if (empty($dolmemcache) || ! is_object($dolmemcache))
76  {
77  $dolmemcache=new Memcache();
78  $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
79  $result=$dolmemcache->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
80  if (! $result) return -1;
81  }
82 
83  $memoryid=session_name().'_'.$memoryid;
84  //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
85  $result=$dolmemcache->add($memoryid,$data); // This fails if key already exists
86  if ($result)
87  {
88  return count($data);
89  }
90  else
91  {
92  return -1;
93  }
94  }
95  // Using shmop
96  else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
97  {
98  $result=dol_setshmop($memoryid,$data);
99  }
100 
101  return $result;
102 }
103 
110 function dol_getcache($memoryid)
111 {
112  global $conf;
113 
114  // Using a memcached server
115  if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
116  {
117  global $m;
118  if (empty($m) || ! is_object($m))
119  {
120  $m=new Memcached();
121  $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
122  $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
123  if (! $result) return -1;
124  }
125 
126  $memoryid=session_name().'_'.$memoryid;
127  //$m->setOption(Memcached::OPT_COMPRESSION, false);
128  //print "Get memoryid=".$memoryid;
129  $data=$m->get($memoryid);
130  $rescode=$m->getResultCode();
131  //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
132  //var_dump($data);
133  if ($rescode == 0)
134  {
135  return $data;
136  }
137  else
138  {
139  return -$rescode;
140  }
141  }
142  else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
143  {
144  global $m;
145  if (empty($m) || ! is_object($m))
146  {
147  $m=new Memcache();
148  $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
149  $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
150  if (! $result) return -1;
151  }
152 
153  $memoryid=session_name().'_'.$memoryid;
154  //$m->setOption(Memcached::OPT_COMPRESSION, false);
155  $data=$m->get($memoryid);
156  //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
157  //var_dump($data);
158  if ($data)
159  {
160  return $data;
161  }
162  else
163  {
164  return -1;
165  }
166  }
167  // Using shmop
168  else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
169  {
170  $data=dol_getshmop($memoryid);
171  return $data;
172  }
173 
174  return 0;
175 }
176 
177 
178 
185 function dol_getshmopaddress($memoryid)
186 {
187  global $shmkeys,$shmoffset;
188  if (empty($shmkeys[$memoryid])) return 0;
189  return $shmkeys[$memoryid]+$shmoffset;
190 }
191 
197 function dol_listshmop()
198 {
199  global $shmkeys,$shmoffset;
200 
201  $resarray=array();
202  foreach($shmkeys as $key => $val)
203  {
204  $result=dol_getshmop($key);
205  if (! is_numeric($result) || $result > 0) $resarray[$key]=$result;
206  }
207  return $resarray;
208 }
209 
217 function dol_setshmop($memoryid,$data)
218 {
219  global $shmkeys,$shmoffset;
220 
221  //print 'dol_setshmop memoryid='.$memoryid."<br>\n";
222  if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0;
223  $shmkey=dol_getshmopaddress($memoryid);
224  $newdata=serialize($data);
225  $size=strlen($newdata);
226  //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
227  $handle=shmop_open($shmkey,'c',0644,6+$size);
228  if ($handle)
229  {
230  $shm_bytes_written1=shmop_write($handle,str_pad($size,6),0);
231  $shm_bytes_written2=shmop_write($handle,$newdata,6);
232  if (($shm_bytes_written1 + $shm_bytes_written2) != (6+dol_strlen($newdata)))
233  {
234  print "Couldn't write the entire length of data\n";
235  }
236  shmop_close($handle);
237  return ($shm_bytes_written1+$shm_bytes_written2);
238  }
239  else
240  {
241  print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
242  return -1;
243  }
244 }
245 
252 function dol_getshmop($memoryid)
253 {
254  global $shmkeys,$shmoffset;
255 
256  if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0;
257  $shmkey=dol_getshmopaddress($memoryid);
258  //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
259  $handle=@shmop_open($shmkey,'a',0,0);
260  if ($handle)
261  {
262  $size=trim(shmop_read($handle,0,6));
263  if ($size) $data=unserialize(shmop_read($handle,6,$size));
264  else return -1;
265  shmop_close($handle);
266  }
267  else
268  {
269  return -2;
270  }
271  return $data;
272 }
273 
dol_getcache($memoryid)
Read a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:110
dol_getshmop($memoryid)
Read a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:252
dol_getshmopaddress($memoryid)
Return shared memory address used to store dataset with key memoryid.
Definition: memory.lib.php:185
dol_setshmop($memoryid, $data)
Save data into a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:217
dol_setcache($memoryid, $data)
Save data into a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:42
dol_listshmop()
Return list of contents of all memory area shared.
Definition: memory.lib.php:197
print
Draft customers invoices.
Definition: index.php:91
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.