PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.0
  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. * \file htdocs/core/lib/memory.lib.php
  20. * \brief Set of function for memory/cache management
  21. */
  22. global $shmkeys,$shmoffset;
  23. $shmkeys=array('main'=>1,'admin'=>2,'dict'=>3,'companies'=>4,'suppliers'=>5,'products'=>6,
  24. 'commercial'=>7,'compta'=>8,'projects'=>9,'cashdesk'=>10,'agenda'=>11,'bills'=>12,
  25. 'propal'=>13,'boxes'=>14,'banks'=>15,'other'=>16,'errors'=>17,'members'=>18,'ecm'=>19,
  26. 'orders'=>20,'users'=>21,'help'=>22,'stocks'=>23,'interventions'=>24,
  27. 'donations'=>25,'contracts'=>26);
  28. $shmoffset=100;
  29. /**
  30. * Save data into a memory area shared by all users, all sessions on server
  31. *
  32. * @param $memoryid Memory id of shared area
  33. * @param $data Data to save
  34. * @return int <0 if KO, Nb of bytes written if OK
  35. */
  36. function dol_setcache($memoryid,$data)
  37. {
  38. global $conf;
  39. $result=0;
  40. // Using a memcached server
  41. if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
  42. {
  43. $memoryid=session_name().'_'.$memoryid;
  44. $m=new Memcached();
  45. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  46. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  47. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  48. //print "Add memoryid=".$memoryid;
  49. $m->add($memoryid,$data); // This fails if key already exists
  50. $rescode=$m->getResultCode();
  51. if ($rescode == 0)
  52. {
  53. return count($data);
  54. }
  55. else
  56. {
  57. return -$rescode;
  58. }
  59. }
  60. else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
  61. {
  62. $memoryid=session_name().'_'.$memoryid;
  63. $m=new Memcache();
  64. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  65. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  66. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  67. $result=$m->add($memoryid,$data); // This fails if key already exists
  68. if ($result)
  69. {
  70. return count($data);
  71. }
  72. else
  73. {
  74. return -1;
  75. }
  76. }
  77. // Using shmop
  78. else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
  79. {
  80. $result=dol_setshmop($memoryid,$data);
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Read a memory area shared by all users, all sessions on server
  86. *
  87. * @param $memoryid Memory id of shared area
  88. * @return int <0 if KO, data if OK
  89. */
  90. function dol_getcache($memoryid)
  91. {
  92. global $conf;
  93. // Using a memcached server
  94. if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
  95. {
  96. $memoryid=session_name().'_'.$memoryid;
  97. $m=new Memcached();
  98. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  99. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  100. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  101. //print "Get memoryid=".$memoryid;
  102. $data=$m->get($memoryid);
  103. $rescode=$m->getResultCode();
  104. //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
  105. //var_dump($data);
  106. if ($rescode == 0)
  107. {
  108. return $data;
  109. }
  110. else
  111. {
  112. return -$rescode;
  113. }
  114. }
  115. else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
  116. {
  117. $memoryid=session_name().'_'.$memoryid;
  118. $m=new Memcache();
  119. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  120. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  121. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  122. $data=$m->get($memoryid);
  123. //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
  124. //var_dump($data);
  125. if ($data)
  126. {
  127. return $data;
  128. }
  129. else
  130. {
  131. return -1;
  132. }
  133. }
  134. // Using shmop
  135. else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
  136. {
  137. $data=dol_getshmop($memoryid);
  138. return $data;
  139. }
  140. return 0;
  141. }
  142. /**
  143. * \brief Return shared memory address used to store dataset with key memoryid
  144. * \param $memoryid Memory id of shared area
  145. * \return int <0 if KO, Memoy address of shared memory for key
  146. */
  147. function dol_getshmopaddress($memoryid)
  148. {
  149. global $shmkeys,$shmoffset;
  150. if (empty($shmkeys[$memoryid])) return 0;
  151. return $shmkeys[$memoryid]+$shmoffset;
  152. }
  153. /**
  154. * \brief Return list of contents of all memory area shared
  155. * \return int 0=Nothing is done, <0 if KO, >0 if OK
  156. */
  157. function dol_listshmop()
  158. {
  159. global $shmkeys,$shmoffset;
  160. $resarray=array();
  161. foreach($shmkeys as $key => $val)
  162. {
  163. $result=dol_getshmop($key);
  164. if (! is_numeric($result) || $result > 0) $resarray[$key]=$result;
  165. }
  166. return $resarray;
  167. }
  168. /**
  169. * \brief Save data into a memory area shared by all users, all sessions on server
  170. * \param $memoryid Memory id of shared area
  171. * \param $data Data to save
  172. * \return int <0 if KO, Nb of bytes written if OK
  173. */
  174. function dol_setshmop($memoryid,$data)
  175. {
  176. global $shmkeys,$shmoffset;
  177. //print 'dol_setshmop memoryid='.$memoryid."<br>\n";
  178. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0;
  179. $shmkey=dol_getshmopaddress($memoryid);
  180. $newdata=serialize($data);
  181. $size=strlen($newdata);
  182. //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
  183. $handle=shmop_open($shmkey,'c',0644,6+$size);
  184. if ($handle)
  185. {
  186. $shm_bytes_written1=shmop_write($handle,str_pad($size,6),0);
  187. $shm_bytes_written2=shmop_write($handle,$newdata,6);
  188. if (($shm_bytes_written1 + $shm_bytes_written2) != (6+dol_strlen($newdata)))
  189. {
  190. print "Couldn't write the entire length of data\n";
  191. }
  192. shmop_close($handle);
  193. return ($shm_bytes_written1+$shm_bytes_written2);
  194. }
  195. else
  196. {
  197. print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
  198. return -1;
  199. }
  200. }
  201. /**
  202. * \brief Read a memory area shared by all users, all sessions on server
  203. * \param $memoryid Memory id of shared area
  204. * \return int <0 if KO, data if OK
  205. */
  206. function dol_getshmop($memoryid)
  207. {
  208. global $shmkeys,$shmoffset;
  209. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0;
  210. $shmkey=dol_getshmopaddress($memoryid);;
  211. //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
  212. $handle=@shmop_open($shmkey,'a',0,0);
  213. if ($handle)
  214. {
  215. $size=trim(shmop_read($handle,0,6));
  216. if ($size) $data=unserialize(shmop_read($handle,6,$size));
  217. else return -1;
  218. shmop_close($handle);
  219. }
  220. else
  221. {
  222. return -2;
  223. }
  224. return $data;
  225. }
  226. ?>