Cache.class.php 3.6 KB

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