Cache.class.php
3.94 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Cache.class.php
* 缓存
* $Author$
*/
namespace Com;
class Cache
{
/**
* 实例化
*
* @return Cache
*/
public static function &instance()
{
static $instance;
if (empty($instance)) {
$instance = new self();
}
return $instance;
}
public function __construct()
{
// do nothing.
}
/**
* 获取缓存数据
*
* @param mixed $name 缓存名称
* @param mixed $value 缓存值
* @param mixed $options 缓存配置
*
* @return mixed
*/
public function get($name, $value = '', $options = null)
{
$init = false;
if (! is_array($options) && is_array($name)) {
// 初始化缓存操作
$this->init_options($name);
$init = true;
} else {
// 检查缓存名称是否符合规范
if (!is_array($options)) {
$options = [];
}
$names = explode('.', $name);
if (1 == count($names)) {
array_unshift($names, MODULE_NAME);
$name = implode('.', $names);
// 是否跳过添加应用标识 skipAddIdentifier true 跳过
} elseif (!in_array($name, \Common\Common\Cache::CACHE_COMMON_FIELD) &&
empty($options['skipAddIdentifier'])) {
// 追加应用标识
$names[1] = APP_DIR . $names[1];
$name = implode('.', $names);
}
if (empty($options['prefix'])) {
// 追加企业标识
$options['prefix'] = QY_DOMAIN . '_';
}
$this->init_options($options);
}
$result = S($name, $value, $options);
// 如果缓存不存在或者已过期
$auto_create = isset($options['auto_create']) && false === $options['auto_create'] ? false : true;
if (! $init && ! is_null($value) && false === $result && $auto_create) {
$result = $this->__create($name, $options);
}
return $result;
}
/**
* 更新缓存
*
* @param mixed $name 缓存名称
* @param mixed $value 缓存值
* @param mixed $options 缓存配置
*
* @return boolean
*/
public function set($name, $value, $options = array())
{
if (!is_array($options)) {
$options = [];
}
$options['auto_create'] = true;
$this->get($name, $value, $options);
return true;
}
/**
* 创建缓存
*
* @param string $name 缓存名称
* @param array $options 缓存设置
*/
private function __create($name, $options)
{
$names = explode('.', $name);
$func = $names[1];
// 去除应用标识
$func = str_replace(APP_DIR, '', $func);
// 如果是公共缓存
if ('Common' == $names[0]) {
$return = $this->$func();
} else {
$class = '\\' . $names[0] . '\Common\Cache';
$cache = new $class();
$return = $cache->$func();
}
S($name, $return, $options);
return $return;
}
/**
* 初始化, 保证缓存文件都在同一个目录下
*
* @param mixed $options 缓存配置
*
* @return boolean
*/
public function init_options(&$options = null)
{
// 如果配置为空
if (empty($options)) {
$options = array();
}
if (!is_array($options)) {
$options = [];
}
// 如果缓存目录不存在
if (empty($options['temp'])) {
$options['temp'] = get_sitedir();
}
return true;
}
/**
* 默认处理方法
*
* @param $method
* @param $args
*
* @return array
*/
public function __call($method, $args)
{
E('_ERR_CACHE_UNDEFINED');
return array();
}
}