Cache.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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;
  12. /**
  13. * 缓存管理类
  14. */
  15. class Cache
  16. {
  17. /**
  18. * 操作句柄
  19. * @var string
  20. * @access protected
  21. */
  22. protected $handler;
  23. /**
  24. * 缓存连接参数
  25. * @var integer
  26. * @access protected
  27. */
  28. protected $options = array();
  29. /**
  30. * 连接缓存
  31. * @access public
  32. * @param string $type 缓存类型
  33. * @param array $options 配置数组
  34. * @return object
  35. */
  36. public function connect($type = '', $options = array())
  37. {
  38. if (empty($type)) {
  39. $type = C('DATA_CACHE_TYPE');
  40. }
  41. $class = strpos($type, '\\') ? $type : 'Think\\Cache\\Driver\\' . ucwords(strtolower($type));
  42. if (class_exists($class)) {
  43. $cache = new $class($options);
  44. } else {
  45. E(L('_CACHE_TYPE_INVALID_') . ':' . $type);
  46. }
  47. return $cache;
  48. }
  49. /**
  50. * 取得缓存类实例
  51. * @static
  52. * @access public
  53. * @return mixed
  54. */
  55. public static function getInstance($type = '', $options = array())
  56. {
  57. static $_instance = array();
  58. $guid = $type . to_guid_string($options);
  59. if (!isset($_instance[$guid])) {
  60. $obj = new Cache();
  61. $_instance[$guid] = $obj->connect($type, $options);
  62. }
  63. return $_instance[$guid];
  64. }
  65. public function __get($name)
  66. {
  67. return $this->get($name);
  68. }
  69. public function __set($name, $value)
  70. {
  71. return $this->set($name, $value);
  72. }
  73. public function __unset($name)
  74. {
  75. $this->rm($name);
  76. }
  77. public function setOptions($name, $value)
  78. {
  79. $this->options[$name] = $value;
  80. }
  81. public function getOptions($name)
  82. {
  83. return $this->options[$name];
  84. }
  85. /**
  86. * 队列缓存
  87. * @access protected
  88. * @param string $key 队列名
  89. * @return mixed
  90. */
  91. //
  92. protected function queue($key)
  93. {
  94. static $_handler = array(
  95. 'file' => array('F', 'F'),
  96. 'xcache' => array('xcache_get', 'xcache_set'),
  97. 'apc' => array('apc_fetch', 'apc_store'),
  98. );
  99. $queue = isset($this->options['queue']) ? $this->options['queue'] : 'file';
  100. $fun = isset($_handler[$queue]) ? $_handler[$queue] : $_handler['file'];
  101. $queue_name = isset($this->options['queue_name']) ? $this->options['queue_name'] : 'think_queue';
  102. $value = $fun[0]($queue_name);
  103. if (!$value) {
  104. $value = array();
  105. }
  106. // 进列
  107. if (false === array_search($key, $value)) {
  108. array_push($value, $key);
  109. }
  110. if (count($value) > $this->options['length']) {
  111. // 出列
  112. $key = array_shift($value);
  113. // 删除缓存
  114. $this->rm($key);
  115. if (APP_DEBUG) {
  116. //调试模式下,记录出列次数
  117. N($queue_name . '_out_times', 1);
  118. }
  119. }
  120. return $fun[1]($queue_name, $value);
  121. }
  122. public function __call($method, $args)
  123. {
  124. //调用缓存类型自己的方法
  125. if (method_exists($this->handler, $method)) {
  126. return call_user_func_array(array($this->handler, $method), $args);
  127. } else {
  128. E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
  129. return;
  130. }
  131. }
  132. }