memory.lib.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /**
  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 string $memoryid Memory id of shared area
  33. * @param string $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 string $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. * Return shared memory address used to store dataset with key memoryid
  144. *
  145. * @param string $memoryid Memory id of shared area
  146. * @return int <0 if KO, Memoy address of shared memory for key
  147. */
  148. function dol_getshmopaddress($memoryid)
  149. {
  150. global $shmkeys,$shmoffset;
  151. if (empty($shmkeys[$memoryid])) return 0;
  152. return $shmkeys[$memoryid]+$shmoffset;
  153. }
  154. /**
  155. * Return list of contents of all memory area shared
  156. *
  157. * @return int 0=Nothing is done, <0 if KO, >0 if OK
  158. */
  159. function dol_listshmop()
  160. {
  161. global $shmkeys,$shmoffset;
  162. $resarray=array();
  163. foreach($shmkeys as $key => $val)
  164. {
  165. $result=dol_getshmop($key);
  166. if (! is_numeric($result) || $result > 0) $resarray[$key]=$result;
  167. }
  168. return $resarray;
  169. }
  170. /**
  171. * Save data into a memory area shared by all users, all sessions on server
  172. *
  173. * @param int $memoryid Memory id of shared area
  174. * @param string $data Data to save
  175. * @return int <0 if KO, Nb of bytes written if OK
  176. */
  177. function dol_setshmop($memoryid,$data)
  178. {
  179. global $shmkeys,$shmoffset;
  180. //print 'dol_setshmop memoryid='.$memoryid."<br>\n";
  181. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0;
  182. $shmkey=dol_getshmopaddress($memoryid);
  183. $newdata=serialize($data);
  184. $size=strlen($newdata);
  185. //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
  186. $handle=shmop_open($shmkey,'c',0644,6+$size);
  187. if ($handle)
  188. {
  189. $shm_bytes_written1=shmop_write($handle,str_pad($size,6),0);
  190. $shm_bytes_written2=shmop_write($handle,$newdata,6);
  191. if (($shm_bytes_written1 + $shm_bytes_written2) != (6+dol_strlen($newdata)))
  192. {
  193. print "Couldn't write the entire length of data\n";
  194. }
  195. shmop_close($handle);
  196. return ($shm_bytes_written1+$shm_bytes_written2);
  197. }
  198. else
  199. {
  200. print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
  201. return -1;
  202. }
  203. }
  204. /**
  205. * Read a memory area shared by all users, all sessions on server
  206. *
  207. * @param string $memoryid Memory id of shared area
  208. * @return int <0 if KO, data if OK
  209. */
  210. function dol_getshmop($memoryid)
  211. {
  212. global $shmkeys,$shmoffset;
  213. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0;
  214. $shmkey=dol_getshmopaddress($memoryid);;
  215. //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
  216. $handle=@shmop_open($shmkey,'a',0,0);
  217. if ($handle)
  218. {
  219. $size=trim(shmop_read($handle,0,6));
  220. if ($size) $data=unserialize(shmop_read($handle,6,$size));
  221. else return -1;
  222. shmop_close($handle);
  223. }
  224. else
  225. {
  226. return -2;
  227. }
  228. return $data;
  229. }