1
0

functions.cache.shm.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* $Id: functions.cache.shm.php 2099 2007-01-02 12:11:52Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  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. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. // size of shared memory to allocate
  16. define("_WEBAPP_CACHE_SHM_SIZE_CONFIG", 16384); // (ok : 16384)
  17. define("_WEBAPP_CACHE_SHM_SIZE_TRANSFERS", 8192); // (depends on transfer-count)
  18. // shm-ids
  19. define("_WEBAPP_CACHE_SHM_ID_CONFIG", 0x8457); // ftok(__FILE__, 'b')
  20. define("_WEBAPP_CACHE_SHM_ID_TRANSFERS", 0x7544); // ftok('index.php', 'b')
  21. /*******************************************************************************
  22. * config
  23. ******************************************************************************/
  24. /**
  25. * check if cache set
  26. *
  27. * @param $username
  28. * @return boolean
  29. */
  30. function cacheIsSet($username) {
  31. return isset($_SESSION['SHM_ID_CONFIG']);
  32. }
  33. /**
  34. * init cfg from cache
  35. *
  36. * @param $username
  37. */
  38. function cacheInit($username) {
  39. global $cfg;
  40. // attach
  41. if (!($mkey = shm_attach(_WEBAPP_CACHE_SHM_ID_CONFIG)))
  42. error("shmem_attach failed", "", "");
  43. // get var from shared mem
  44. $cfg = shm_get_var($mkey, 1);
  45. }
  46. /**
  47. * set the cache
  48. *
  49. * @param $username
  50. */
  51. function cacheSet($username) {
  52. global $cfg;
  53. // add to cache
  54. _cacheShmSet('SHM_ID_CONFIG', _WEBAPP_CACHE_SHM_ID_CONFIG, _WEBAPP_CACHE_SHM_SIZE_CONFIG, $cfg);
  55. }
  56. /**
  57. * flush the cache
  58. *
  59. * @param $username
  60. */
  61. function cacheFlush($username = "") {
  62. _cacheShmFlush('SHM_ID_CONFIG', _WEBAPP_CACHE_SHM_ID_CONFIG);
  63. }
  64. /*******************************************************************************
  65. * transfers
  66. ******************************************************************************/
  67. /**
  68. * check if cache set
  69. *
  70. * @return boolean
  71. */
  72. function cacheTransfersIsSet() {
  73. return isset($_SESSION['SHM_ID_TRANSFERS']);
  74. }
  75. /**
  76. * init transfers from cache
  77. */
  78. function cacheTransfersInit() {
  79. global $transfers;
  80. // attach
  81. if (!($mkey = shm_attach(_WEBAPP_CACHE_SHM_ID_TRANSFERS)))
  82. error("shmem_attach failed", "", "");
  83. // get var from shared mem
  84. $transfers = shm_get_var($mkey, 1);
  85. }
  86. /**
  87. * set the cache
  88. */
  89. function cacheTransfersSet() {
  90. global $transfers;
  91. initGlobalTransfersArray();
  92. // add to cache
  93. _cacheShmSet('SHM_ID_TRANSFERS', _WEBAPP_CACHE_SHM_ID_TRANSFERS, _WEBAPP_CACHE_SHM_SIZE_TRANSFERS, $transfers);
  94. }
  95. /**
  96. * flush the cache
  97. */
  98. function cacheTransfersFlush() {
  99. _cacheShmFlush('SHM_ID_TRANSFERS', _WEBAPP_CACHE_SHM_ID_TRANSFERS);
  100. }
  101. /*******************************************************************************
  102. * common shm-functions
  103. ******************************************************************************/
  104. /**
  105. * generic shm set
  106. *
  107. * @param $shmname
  108. * @param $shmid
  109. * @param $shmsize
  110. * @param &$var
  111. */
  112. function _cacheShmSet($shmname, $shmid, $shmsize, &$var) {
  113. // attach
  114. if (!($mkey = shm_attach($shmid, $shmsize, 0666)))
  115. error("shmem_attach failed", "", "");
  116. // save id in session-var
  117. $_SESSION[$shmname] = $mkey;
  118. // get sem
  119. if (!($skey = sem_get($shmid, 1, 0666)))
  120. error("sem_get failed", "", "");
  121. // acquire sem
  122. if (!sem_acquire($skey))
  123. error("sem_acquire failed", "", "");
  124. // put var to shared mem
  125. if (!shm_put_var($mkey, 1, $var))
  126. error("Fail to put var to Shared memory ".$mkey.".", "", "");
  127. // release sem
  128. if (!sem_release($skey))
  129. error("sem_release failed", "", "");
  130. }
  131. /**
  132. * generic shm flush
  133. *
  134. * @param $shmname
  135. * @param $shmid
  136. */
  137. function _cacheShmFlush($shmname, $shmid) {
  138. // keys
  139. if (!($skey = sem_get($shmid, 1)))
  140. error("sem_get failed", "", "");
  141. if (!($mkey = shm_attach($shmid)))
  142. error("shmem_attach failed", "", "");
  143. // remove var
  144. @shm_remove_var($shmid, 1);
  145. // Release semaphore
  146. @sem_release($skey);
  147. // remove shared memory segment from SysV
  148. @shm_remove($mkey);
  149. // detach
  150. @shm_detach($mkey);
  151. // Remove semaphore
  152. @sem_remove($skey);
  153. // session-id
  154. unset($_SESSION[$shmname]);
  155. }
  156. ?>