Shmop.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. /**
  14. * Shmop缓存驱动
  15. */
  16. class Shmop extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!extension_loaded('shmop')) {
  26. E(L('_NOT_SUPPORT_') . ':shmop');
  27. }
  28. if (!empty($options)) {
  29. $options = array(
  30. 'size' => C('SHARE_MEM_SIZE'),
  31. 'temp' => TEMP_PATH,
  32. 'project' => 's',
  33. 'length' => 0,
  34. );
  35. }
  36. $this->options = $options;
  37. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  38. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  39. $this->handler = $this->_ftok($this->options['project']);
  40. }
  41. /**
  42. * 读取缓存
  43. * @access public
  44. * @param string $name 缓存变量名
  45. * @return mixed
  46. */
  47. public function get($name = false)
  48. {
  49. N('cache_read', 1);
  50. $id = shmop_open($this->handler, 'c', 0600, 0);
  51. if (false !== $id) {
  52. $ret = unserialize(shmop_read($id, 0, shmop_size($id)));
  53. shmop_close($id);
  54. if (false === $name) {
  55. return $ret;
  56. }
  57. $name = $this->options['prefix'] . $name;
  58. if (isset($ret[$name])) {
  59. $content = $ret[$name];
  60. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  61. //启用数据压缩
  62. $content = gzuncompress($content);
  63. }
  64. return $content;
  65. } else {
  66. return null;
  67. }
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * 写入缓存
  74. * @access public
  75. * @param string $name 缓存变量名
  76. * @param mixed $value 存储数据
  77. * @return boolean
  78. */
  79. public function set($name, $value)
  80. {
  81. N('cache_write', 1);
  82. $lh = $this->_lock();
  83. $val = $this->get();
  84. if (!is_array($val)) {
  85. $val = array();
  86. }
  87. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  88. //数据压缩
  89. $value = gzcompress($value, 3);
  90. }
  91. $name = $this->options['prefix'] . $name;
  92. $val[$name] = $value;
  93. $val = serialize($val);
  94. if ($this->_write($val, $lh)) {
  95. if ($this->options['length'] > 0) {
  96. // 记录缓存队列
  97. $this->queue($name);
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**
  104. * 删除缓存
  105. * @access public
  106. * @param string $name 缓存变量名
  107. * @return boolean
  108. */
  109. public function rm($name)
  110. {
  111. $lh = $this->_lock();
  112. $val = $this->get();
  113. if (!is_array($val)) {
  114. $val = array();
  115. }
  116. $name = $this->options['prefix'] . $name;
  117. unset($val[$name]);
  118. $val = serialize($val);
  119. return $this->_write($val, $lh);
  120. }
  121. /**
  122. * 生成IPC key
  123. * @access private
  124. * @param string $project 项目标识名
  125. * @return integer
  126. */
  127. private function _ftok($project)
  128. {
  129. if (function_exists('ftok')) {
  130. return ftok(__FILE__, $project);
  131. }
  132. if (strtoupper(PHP_OS) == 'WINNT') {
  133. $s = stat(__FILE__);
  134. return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) |
  135. (($project & 0xff) << 24)));
  136. } else {
  137. $filename = __FILE__ . (string) $project;
  138. for ($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1)));
  139. return dechex(array_sum($key));
  140. }
  141. }
  142. /**
  143. * 写入操作
  144. * @access private
  145. * @param string $name 缓存变量名
  146. * @return integer|boolean
  147. */
  148. private function _write(&$val, &$lh)
  149. {
  150. $id = shmop_open($this->handler, 'c', 0600, $this->options['size']);
  151. if ($id) {
  152. $ret = shmop_write($id, $val, 0) == strlen($val);
  153. shmop_close($id);
  154. $this->_unlock($lh);
  155. return $ret;
  156. }
  157. $this->_unlock($lh);
  158. return false;
  159. }
  160. /**
  161. * 共享锁定
  162. * @access private
  163. * @param string $name 缓存变量名
  164. * @return boolean
  165. */
  166. private function _lock()
  167. {
  168. if (function_exists('sem_get')) {
  169. $fp = sem_get($this->handler, 1, 0600, 1);
  170. sem_acquire($fp);
  171. } else {
  172. $fp = fopen($this->options['temp'] . $this->options['prefix'] . md5($this->handler), 'w');
  173. flock($fp, LOCK_EX);
  174. }
  175. return $fp;
  176. }
  177. /**
  178. * 解除共享锁定
  179. * @access private
  180. * @param string $name 缓存变量名
  181. * @return boolean
  182. */
  183. private function _unlock(&$fp)
  184. {
  185. if (function_exists('sem_release')) {
  186. sem_release($fp);
  187. } else {
  188. fclose($fp);
  189. }
  190. }
  191. }