SharedMemory.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jenner
  5. * Date: 2015/8/12
  6. * Time: 15:00
  7. */
  8. namespace Jenner\SimpleFork\Cache;
  9. /**
  10. * shared memory cache
  11. *
  12. * @package Jenner\SimpleFork\Cache
  13. */
  14. class SharedMemory implements CacheInterface
  15. {
  16. /**
  17. * holds shared memory resource
  18. * @var resource
  19. */
  20. protected $shm;
  21. /**
  22. * shared memory ipc key
  23. * @var string
  24. */
  25. protected $client_count_key = 'system_client_count';
  26. /**
  27. * memory size
  28. * @var int
  29. */
  30. protected $size;
  31. /**
  32. * @param int $size memory size
  33. * @param string $file
  34. */
  35. public function __construct($size = 33554432, $file = __FILE__)
  36. {
  37. $this->size = $size;
  38. if (function_exists("shm_attach") === false) {
  39. $message = "\nYour PHP configuration needs adjustment. " .
  40. "See: http://us2.php.net/manual/en/shmop.setup.php. " .
  41. "To enable the System V shared memory support compile " .
  42. " PHP with the option --enable-sysvshm.";
  43. throw new \RuntimeException($message);
  44. }
  45. $this->attach($file); //create resources (shared memory)
  46. }
  47. /**
  48. * connect shared memory
  49. *
  50. * @param string $file
  51. */
  52. public function attach($file = __FILE__)
  53. {
  54. if (!file_exists($file)) {
  55. $touch = touch($file);
  56. if (!$touch) {
  57. throw new \RuntimeException("file is not exists and it can not be created. file: {$file}");
  58. }
  59. }
  60. $key = ftok($file, 'a');
  61. $this->shm = shm_attach($key, $this->size); //allocate shared memory
  62. }
  63. /**
  64. * remove shared memory.
  65. * you should know that it maybe does not work.
  66. *
  67. * @return bool
  68. */
  69. public function remove()
  70. {
  71. //dallocate shared memory
  72. if (!shm_remove($this->shm)) {
  73. return false;
  74. }
  75. $this->dettach();
  76. // shm_remove maybe not working. it likes a php bug.
  77. unset($this->shm);
  78. return true;
  79. }
  80. /**
  81. * @return bool
  82. */
  83. public function dettach()
  84. {
  85. return shm_detach($this->shm); //allocate shared memory
  86. }
  87. /**
  88. * set var
  89. *
  90. * @param $key
  91. * @param $value
  92. * @return bool
  93. */
  94. public function set($key, $value)
  95. {
  96. return shm_put_var($this->shm, $this->shm_key($key), $value); //store var
  97. }
  98. /**
  99. * generate shm key
  100. *
  101. * @param $val
  102. * @return mixed
  103. */
  104. public function shm_key($val)
  105. { // enable all world langs and chars !
  106. // text to number system.
  107. return preg_replace("/[^0-9]/", "", (preg_replace("/[^0-9]/", "", md5($val)) / 35676248) / 619876);
  108. }
  109. /**
  110. * get var
  111. *
  112. * @param $key
  113. * @param null $default
  114. * @return bool|mixed
  115. */
  116. public function get($key, $default = null)
  117. {
  118. if ($this->has($key)) {
  119. return shm_get_var($this->shm, $this->shm_key($key));
  120. } else {
  121. return $default;
  122. }
  123. }
  124. /**
  125. * has var ?
  126. *
  127. * @param $key
  128. * @return bool
  129. */
  130. public function has($key)
  131. {
  132. if (shm_has_var($this->shm, $this->shm_key($key))) { // check is isset
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. /**
  139. * delete var
  140. *
  141. * @param $key
  142. * @return bool
  143. */
  144. public function delete($key)
  145. {
  146. if ($this->has($key)) {
  147. return shm_remove_var($this->shm, $this->shm_key($key));
  148. } else {
  149. return false;
  150. }
  151. }
  152. /**
  153. * init when wakeup
  154. */
  155. public function __wakeup()
  156. {
  157. $this->attach();
  158. }
  159. }