Cache.class.php 3.7 KB

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