File.class.php
6.03 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Cache\Driver;
use Think\Cache;
defined('THINK_PATH') or exit();
/**
* 文件类型缓存类
*/
class File extends Cache
{
/**
* 架构函数
*
* @access public
*/
public function __construct($options = array())
{
if (! empty($options)) {
$this->options = $options;
}
$this->options['temp'] = ! empty($options['temp']) ? $options['temp'] : C('DATA_CACHE_PATH');
$this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
$this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
$this->options['length'] = isset($options['length']) ? $options['length'] : 0;
if (substr($this->options['temp'], - 1) != '/') {
$this->options['temp'] .= '/';
}
$this->init();
}
/**
* 初始化检查
*
* @access private
* @return boolean
*/
private function init()
{
// 创建应用缓存目录
if (! is_dir($this->options['temp'])) {
mkdir($this->options['temp']);
}
}
/**
* 取得变量的存储文件名
*
* @access private
* @param string $name
* 缓存变量名
* @return string
*/
private function filename($name)
{
// $name = md5(C('DATA_CACHE_KEY') . $name);
if (C('DATA_CACHE_SUBDIR')) {
// 使用子目录
$dir = '';
for ($i = 0; $i < C('DATA_PATH_LEVEL'); $i ++) {
$dir .= $name{$i} . '/';
}
if (! is_dir($this->options['temp'] . $dir)) {
mkdir($this->options['temp'] . $dir, 0755, true);
}
$filename = $dir . $this->options['prefix'] . $name . '.php';
} else {
$filename = $this->options['prefix'] . $name . '.php';
}
return $this->options['temp'] . $filename;
}
/**
* 读取缓存
*
* @access public
* @param string $name
* 缓存变量名
* @return mixed
*/
public function get($name)
{
$filename = $this->filename($name);
if (! is_file($filename)) {
return false;
}
N('cache_read', 1);
$content = file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if ($expire != 0 && time() > filemtime($filename) + $expire) {
// 缓存过期删除缓存文件
unlink($filename);
return false;
}
if (C('DATA_CACHE_CHECK')) { // 开启数据校验
$check = substr($content, 20, 32);
$content = substr($content, 52, - 3);
if ($check != md5($content)) { // 校验错误
return false;
}
} else {
$content = substr($content, 20, - 3);
}
if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
// 启用数据压缩
$content = gzuncompress($content);
}
$content = $content ? unserialize($content) : array();
return $content;
} else {
return false;
}
}
/**
* 写入缓存
*
* @access public
* @param string $name
* 缓存变量名
* @param mixed $value
* 存储数据
* @param int $expire
* 有效时间 0为永久
* @return boolean
*/
public function set($name, $value, $expire = null)
{
N('cache_write', 1);
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$filename = $this->filename($name);
$data = serialize($value);
if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
// 数据压缩
$data = gzcompress($data, 3);
}
if (C('DATA_CACHE_CHECK')) { // 开启数据校验
$check = md5($data);
} else {
$check = '';
}
$data = "<?php\n//" . sprintf('%012d', $expire) . $check . $data . "\n?>";
$result = file_put_contents($filename, $data);
if ($result) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$this->queue($name);
}
clearstatcache();
return true;
} else {
return false;
}
}
/**
* 删除缓存
*
* @access public
* @param string $name
* 缓存变量名
* @return boolean
*/
public function rm($name)
{
$filename = $this->filename($name);
return is_file($filename) && unlink($filename);
}
/**
* 清除缓存
*
* @access public
* @param string $name
* 缓存变量名
* @return boolean
*/
public function clear()
{
$path = $this->options['temp'];
$files = scandir($path);
if ($files) {
foreach ($files as $file) {
if ($file != '.' && $file != '..' && is_dir($path . $file)) {
array_map('unlink', glob($path . $file . '/*.*'));
} elseif (is_file($path . $file)) {
unlink($path . $file);
}
}
return true;
}
return false;
}
}