CacheShmop.class.php 5.7 KB

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