dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
16  * or see https://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  } else {
67  return -$rescode;
68  }
69  } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache'))
70  {
71  global $dolmemcache;
72  if (empty($dolmemcache) || !is_object($dolmemcache))
73  {
74  $dolmemcache = new Memcache();
75  $tmparray = explode(':', $conf->global->MEMCACHED_SERVER);
76  $result = $dolmemcache->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211);
77  if (!$result) return -1;
78  }
79 
80  $memoryid = session_name().'_'.$memoryid;
81  //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
82  $result = $dolmemcache->add($memoryid, $data); // This fails if key already exists
83  if ($result)
84  {
85  return count($data);
86  } else {
87  return -1;
88  }
89  } // Using shmop
90  elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
91  {
92  $result = dol_setshmop($memoryid, $data);
93  }
94 
95  return $result;
96 }
97 
104 function dol_getcache($memoryid)
105 {
106  global $conf;
107 
108  // Using a memcached server
109  if (!empty($conf->memcached->enabled) && class_exists('Memcached'))
110  {
111  global $m;
112  if (empty($m) || !is_object($m))
113  {
114  $m = new Memcached();
115  $tmparray = explode(':', $conf->global->MEMCACHED_SERVER);
116  $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211);
117  if (!$result) return -1;
118  }
119 
120  $memoryid = session_name().'_'.$memoryid;
121  //$m->setOption(Memcached::OPT_COMPRESSION, false);
122  //print "Get memoryid=".$memoryid;
123  $data = $m->get($memoryid);
124  $rescode = $m->getResultCode();
125  //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
126  //var_dump($data);
127  if ($rescode == 0)
128  {
129  return $data;
130  } else {
131  return -$rescode;
132  }
133  } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache'))
134  {
135  global $m;
136  if (empty($m) || !is_object($m))
137  {
138  $m = new Memcache();
139  $tmparray = explode(':', $conf->global->MEMCACHED_SERVER);
140  $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211);
141  if (!$result) return -1;
142  }
143 
144  $memoryid = session_name().'_'.$memoryid;
145  //$m->setOption(Memcached::OPT_COMPRESSION, false);
146  $data = $m->get($memoryid);
147  //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
148  //var_dump($data);
149  if ($data)
150  {
151  return $data;
152  } else {
153  return -1;
154  }
155  } // Using shmop
156  elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
157  {
158  $data = dol_getshmop($memoryid);
159  return $data;
160  }
161 
162  return 0;
163 }
164 
165 
166 
173 function dol_getshmopaddress($memoryid)
174 {
175  global $shmkeys, $shmoffset;
176  if (empty($shmkeys[$memoryid])) return 0;
177  return $shmkeys[$memoryid] + $shmoffset;
178 }
179 
185 function dol_listshmop()
186 {
187  global $shmkeys, $shmoffset;
188 
189  $resarray = array();
190  foreach ($shmkeys as $key => $val)
191  {
192  $result = dol_getshmop($key);
193  if (!is_numeric($result) || $result > 0) $resarray[$key] = $result;
194  }
195  return $resarray;
196 }
197 
205 function dol_setshmop($memoryid, $data)
206 {
207  global $shmkeys, $shmoffset;
208 
209  //print 'dol_setshmop memoryid='.$memoryid."<br>\n";
210  if (empty($shmkeys[$memoryid]) || !function_exists("shmop_write")) return 0;
211  $shmkey = dol_getshmopaddress($memoryid);
212  $newdata = serialize($data);
213  $size = strlen($newdata);
214  //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
215  $handle = shmop_open($shmkey, 'c', 0644, 6 + $size);
216  if ($handle)
217  {
218  $shm_bytes_written1 = shmop_write($handle, str_pad($size, 6), 0);
219  $shm_bytes_written2 = shmop_write($handle, $newdata, 6);
220  if (($shm_bytes_written1 + $shm_bytes_written2) != (6 + dol_strlen($newdata)))
221  {
222  print "Couldn't write the entire length of data\n";
223  }
224  shmop_close($handle);
225  return ($shm_bytes_written1 + $shm_bytes_written2);
226  } else {
227  print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
228  return -1;
229  }
230 }
231 
238 function dol_getshmop($memoryid)
239 {
240  global $shmkeys, $shmoffset;
241 
242  if (empty($shmkeys[$memoryid]) || !function_exists("shmop_open")) return 0;
243  $shmkey = dol_getshmopaddress($memoryid);
244  //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
245  $handle = @shmop_open($shmkey, 'a', 0, 0);
246  if ($handle)
247  {
248  $size = trim(shmop_read($handle, 0, 6));
249  if ($size) $data = unserialize(shmop_read($handle, 6, $size));
250  else return -1;
251  shmop_close($handle);
252  } else {
253  return -2;
254  }
255  return $data;
256 }
dol_getcache($memoryid)
Read a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:104
dol_getshmop($memoryid)
Read a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:238
dol_getshmopaddress($memoryid)
Return shared memory address used to store dataset with key memoryid.
Definition: memory.lib.php:173
dol_setshmop($memoryid, $data)
Save data into a memory area shared by all users, all sessions on server.
Definition: memory.lib.php:205
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
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:185
print
Draft customers invoices.
Definition: index.php:89