memory.lib.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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=1000; // Max number of entries found into a language file. If too low, some entries will be overwritten.
  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. global $dolmemcache;
  44. if (empty($dolmemcache) || ! is_object($dolmemcache))
  45. {
  46. $dolmemcache=new Memcached();
  47. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  48. $result=$dolmemcache->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  49. if (! $result) return -1;
  50. }
  51. $memoryid=session_name().'_'.$memoryid;
  52. //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
  53. $dolmemcache->add($memoryid,$data); // This fails if key already exists
  54. $rescode=$dolmemcache->getResultCode();
  55. if ($rescode == 0)
  56. {
  57. return count($data);
  58. }
  59. else
  60. {
  61. return -$rescode;
  62. }
  63. }
  64. else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
  65. {
  66. global $dolmemcache;
  67. if (empty($dolmemcache) || ! is_object($dolmemcache))
  68. {
  69. $dolmemcache=new Memcache();
  70. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  71. $result=$dolmemcache->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  72. if (! $result) return -1;
  73. }
  74. $memoryid=session_name().'_'.$memoryid;
  75. //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
  76. $result=$dolmemcache->add($memoryid,$data); // This fails if key already exists
  77. if ($result)
  78. {
  79. return count($data);
  80. }
  81. else
  82. {
  83. return -1;
  84. }
  85. }
  86. // Using shmop
  87. else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
  88. {
  89. $result=dol_setshmop($memoryid,$data);
  90. }
  91. return $result;
  92. }
  93. /**
  94. * Read a memory area shared by all users, all sessions on server
  95. *
  96. * @param string $memoryid Memory id of shared area
  97. * @return int <0 if KO, data if OK
  98. */
  99. function dol_getcache($memoryid)
  100. {
  101. global $conf;
  102. // Using a memcached server
  103. if (! empty($conf->memcached->enabled) && class_exists('Memcached'))
  104. {
  105. global $m;
  106. if (empty($m) || ! is_object($m))
  107. {
  108. $m=new Memcached();
  109. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  110. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  111. if (! $result) return -1;
  112. }
  113. $memoryid=session_name().'_'.$memoryid;
  114. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  115. //print "Get memoryid=".$memoryid;
  116. $data=$m->get($memoryid);
  117. $rescode=$m->getResultCode();
  118. //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
  119. //var_dump($data);
  120. if ($rescode == 0)
  121. {
  122. return $data;
  123. }
  124. else
  125. {
  126. return -$rescode;
  127. }
  128. }
  129. else if (! empty($conf->memcached->enabled) && class_exists('Memcache'))
  130. {
  131. global $m;
  132. if (empty($m) || ! is_object($m))
  133. {
  134. $m=new Memcache();
  135. $tmparray=explode(':',$conf->global->MEMCACHED_SERVER);
  136. $result=$m->addServer($tmparray[0], $tmparray[1]?$tmparray[1]:11211);
  137. if (! $result) return -1;
  138. }
  139. $memoryid=session_name().'_'.$memoryid;
  140. //$m->setOption(Memcached::OPT_COMPRESSION, false);
  141. $data=$m->get($memoryid);
  142. //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
  143. //var_dump($data);
  144. if ($data)
  145. {
  146. return $data;
  147. }
  148. else
  149. {
  150. return -1;
  151. }
  152. }
  153. // Using shmop
  154. else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02))
  155. {
  156. $data=dol_getshmop($memoryid);
  157. return $data;
  158. }
  159. return 0;
  160. }
  161. /**
  162. * Return shared memory address used to store dataset with key memoryid
  163. *
  164. * @param string $memoryid Memory id of shared area ('main', 'agenda', ...)
  165. * @return int <0 if KO, Memoy address of shared memory for key
  166. */
  167. function dol_getshmopaddress($memoryid)
  168. {
  169. global $shmkeys,$shmoffset;
  170. if (empty($shmkeys[$memoryid])) return 0;
  171. return $shmkeys[$memoryid]+$shmoffset;
  172. }
  173. /**
  174. * Return list of contents of all memory area shared
  175. *
  176. * @return array
  177. */
  178. function dol_listshmop()
  179. {
  180. global $shmkeys,$shmoffset;
  181. $resarray=array();
  182. foreach($shmkeys as $key => $val)
  183. {
  184. $result=dol_getshmop($key);
  185. if (! is_numeric($result) || $result > 0) $resarray[$key]=$result;
  186. }
  187. return $resarray;
  188. }
  189. /**
  190. * Save data into a memory area shared by all users, all sessions on server
  191. *
  192. * @param int $memoryid Memory id of shared area ('main', 'agenda', ...)
  193. * @param string $data Data to save
  194. * @return int <0 if KO, Nb of bytes written if OK
  195. */
  196. function dol_setshmop($memoryid,$data)
  197. {
  198. global $shmkeys,$shmoffset;
  199. //print 'dol_setshmop memoryid='.$memoryid."<br>\n";
  200. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0;
  201. $shmkey=dol_getshmopaddress($memoryid);
  202. $newdata=serialize($data);
  203. $size=strlen($newdata);
  204. //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
  205. $handle=shmop_open($shmkey,'c',0644,6+$size);
  206. if ($handle)
  207. {
  208. $shm_bytes_written1=shmop_write($handle,str_pad($size,6),0);
  209. $shm_bytes_written2=shmop_write($handle,$newdata,6);
  210. if (($shm_bytes_written1 + $shm_bytes_written2) != (6+dol_strlen($newdata)))
  211. {
  212. print "Couldn't write the entire length of data\n";
  213. }
  214. shmop_close($handle);
  215. return ($shm_bytes_written1+$shm_bytes_written2);
  216. }
  217. else
  218. {
  219. print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size;
  220. return -1;
  221. }
  222. }
  223. /**
  224. * Read a memory area shared by all users, all sessions on server
  225. *
  226. * @param string $memoryid Memory id of shared area ('main', 'agenda', ...)
  227. * @return int <0 if KO, data if OK
  228. */
  229. function dol_getshmop($memoryid)
  230. {
  231. global $shmkeys,$shmoffset;
  232. if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0;
  233. $shmkey=dol_getshmopaddress($memoryid);
  234. //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
  235. $handle=@shmop_open($shmkey,'a',0,0);
  236. if ($handle)
  237. {
  238. $size=trim(shmop_read($handle,0,6));
  239. if ($size) $data=unserialize(shmop_read($handle,6,$size));
  240. else return -1;
  241. shmop_close($handle);
  242. }
  243. else
  244. {
  245. return -2;
  246. }
  247. return $data;
  248. }