You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.6 KiB

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