shm.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
  16. // | Sebastian Bergmann <sb@sebastian-bergmann.de> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: shm.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  20. require_once 'Cache/Container.php';
  21. /**
  22. * Stores cache data into shared memory.
  23. *
  24. * Well, this is not a very efficient implementation. Indeed it's much
  25. * slower than the file container as far as my tests showed. Files are
  26. * cached by most operating systems and it will be hard to write a faster
  27. * caching algorithm using PHP.
  28. *
  29. * @author Ulf Wendel <ulf.wendel@phpdoc.de>
  30. * @version $Id: shm.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  31. * @package Cache
  32. */
  33. class Cache_Container_shm extends Cache_Container {
  34. /**
  35. * Key of the semaphore used to sync the SHM access
  36. *
  37. * @var int
  38. */
  39. var $sem_key = NULL;
  40. /**
  41. * Permissions of the semaphore used to sync the SHM access
  42. *
  43. * @var int
  44. */
  45. var $sem_perm = 0644;
  46. /**
  47. * Semaphore handler
  48. *
  49. * @var resource
  50. */
  51. var $sem_id = NULL;
  52. /**
  53. * Key of the shared memory block used to store cache data
  54. *
  55. * @var int
  56. */
  57. var $shm_key = NULL;
  58. /**
  59. * Size of the shared memory block used
  60. *
  61. * Note: the container does only use _one_ shm block no more!
  62. *
  63. * @var int
  64. */
  65. var $shm_size = 131072;
  66. /**
  67. * Permissions of the shared memory block
  68. *
  69. * @var int
  70. */
  71. var $shm_perm = 0644;
  72. /**
  73. * Shared memory handler
  74. *
  75. * @var resource
  76. */
  77. var $shm_id = NULL;
  78. /**
  79. * Hash of cache entries
  80. *
  81. * Used by the garbage collection to find old entries.
  82. *
  83. * @var array
  84. */
  85. var $entries = array();
  86. /**
  87. * Number of bytes consumed by the cache
  88. *
  89. * @var int
  90. */
  91. var $total_size = 0;
  92. /**
  93. * Creates a shared memory container
  94. *
  95. * @param array shm_key, sem_key, shm_size, sem_perm, shm_perm
  96. */
  97. function Cache_Container_shm($options = '') {
  98. if (is_array($options))
  99. $this->setOptions($options, array_merge($this->allowed_options,
  100. array('shm_key', 'sem_key',
  101. 'shm_size', 'sem_perm',
  102. 'shm_perm'
  103. )
  104. )
  105. );
  106. // Cache::Container high- and lowwater defaults should be overridden if
  107. // not already done by the user
  108. if (!isset($options['highwater']))
  109. $this->highwater = round(0.75 * 131072);
  110. if (!isset($options['lowwater']))
  111. $this->lowwater = round(0.5 * 131072);
  112. if (!isset($options['shm_size']))
  113. $this->shm_size = 131072;
  114. //get SHM and Semaphore handles
  115. if (!($this->shm_id = shmop_open($this->shm_key, 'c', $this->shm_perm, $this->shm_size)))
  116. new Cache_Error("Can't open SHM segment '{$this->shm_key}', size '{$this->shm_size}'.",
  117. __FILE__,
  118. __LINE__
  119. );
  120. if (!($this->sem_id = sem_get($this->sem_key, 1, $this->sem_perm)))
  121. new Cache_Error("Can't get semaphore '{$this->sem_key}' using perms '{$this->sem_perm}'.",
  122. __FILE__,
  123. __LINE__
  124. );
  125. } // end constructor
  126. function fetch($id, $group) {
  127. sem_acquire($this->sem_id);
  128. $cachedata = shmop_read($this->shm_id, 0, $this->shm_size);
  129. sem_release($this->sem_id);
  130. $cachedata = $this->decode($cachedata);
  131. if (!isset($cachedata[$group][$id]))
  132. return array(NULL, NULL, NULL);
  133. else
  134. $cachedata = $cachedata[$group][$id];
  135. return array($cachedata['expire'],
  136. $cachedata['cachedata'],
  137. $cachedata['userdata']
  138. );
  139. } // end func fetch
  140. function save($id, $data, $expire, $group, $userdata) {
  141. $this->flushPreload($id, $group);
  142. sem_acquire($this->sem_id);
  143. $cachedata = $this->decode(shmop_read($this->shm_id, 0, $this->shm_size));
  144. $cachedata[$group][$id] = array('expire' => $this->getExpiresAbsolute($expire),
  145. 'cachedata' => $data,
  146. 'userdata' => $userdata,
  147. 'changed' => time()
  148. );
  149. if (strlen($newdata = $this->encode($cachedata)) > $this->shm_size)
  150. $cachedata = $this->garbageCollection(time(), $cachedata);
  151. shmop_write($this->shm_id, $newdata, 0);
  152. sem_release($this->sem_id);
  153. return true;
  154. } // end func save
  155. function remove($id, $group) {
  156. $this->flushPreload($id, $group);
  157. sem_acquire($this->sem_id);
  158. $cachedata = $this->decode(shmop_read($this->shm_id, 0, $this->shm_size));
  159. unset($cachedata[$group][$id]);
  160. shmop_write($this->shm_id, $this->encode($cachedata), 0);
  161. sem_release($this->sem_id);
  162. } // end func remove
  163. function flush($group = '') {
  164. $this->flushPreload();
  165. sem_acquire($this->sem_id);
  166. shmop_write($this->shm_id, $this->encode(array()), 0);
  167. sem_release($this->sem_id);
  168. } // end func flush
  169. function idExists($id, $group) {
  170. sem_acquire($this->sem_id);
  171. $cachedata = shm_read($this->shm_id, 0, $this->shm_size);
  172. sem_release($this->sem_id);
  173. $cachedata = $this->decode($cachedata);
  174. return isset($cachedata[$group][$id]);
  175. } // end func isExists
  176. function garbageCollection($maxlifetime, $cachedata = array()) {
  177. if ($lock = empty($cachedata)) {
  178. sem_acquire($this->sem_id);
  179. $cachedata = $this->decode(shmop_read($this->shm_id, 0, $this->shm_size));
  180. }
  181. $this->doGarbageCollection($maxlifetime, &$cachedata);
  182. if ($this->total_size > $this->highwater) {
  183. krsort($this->entries);
  184. reset($this->entries);
  185. while ($this->total_size > $this->lowwater && list($size, $entries) = each($this->entries)) {
  186. reset($entries);
  187. while (list($k, $entry) = each($entries)) {
  188. unset($cachedata[$entry['group']][$entry['id']]);
  189. $this->total_size -= $size;
  190. }
  191. }
  192. }
  193. if ($lock)
  194. sem_release($this->sem_id);
  195. $this->entries = array();
  196. $this->total_size = 0;
  197. return $cachedata;
  198. } // end func garbageCollection
  199. function doGarbageCollection($maxlifetime, &$cachedata) {
  200. $changed = time() - $maxlifetime;
  201. $removed = 0;
  202. reset($cachedata);
  203. while (list($group, $groupdata) = each($cachedata)) {
  204. reset($groupdata);
  205. while (list($id, $data) = each($groupdata)) {
  206. if ($data['expire'] < time() || $data['changed'] < $changed) {
  207. unset($cachedata[$group][$id]);
  208. }
  209. }
  210. // ugly but simple to implement :/
  211. $size = strlen($this->encode($data));
  212. $this->entries[$size][] = array('group' => $group,
  213. 'id' => $id
  214. );
  215. $this->total_size += $size;
  216. }
  217. return $removed;
  218. } // end func doGarbageCollection
  219. } // end class Cache_Container_shm
  220. ?>