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