1
+ <?php
2
+ /**
3
+ * Created by hisune.com.
4
+
5
+ * Date: 14-8-19
6
+ * Time: 下午5:38
7
+ *
8
+ * 该类来源:http://www.oschina.net/code/snippet_162279_6530
9
+ */
10
+ namespace Tiny ;
11
+
12
+ class Cache
13
+ {
14
+ private static $ _instance = null ;
15
+
16
+ protected $ _options = array (
17
+ 'cache_dir ' => "./ " ,
18
+ 'file_name_prefix ' => 's_cache ' ,
19
+ 'mode ' => '1 ' , //mode 1 为serialize model 2为保存为可执行文件
20
+ );
21
+
22
+ /**
23
+ * 得到本类实例
24
+ *
25
+ * @return Ambiguous
26
+ */
27
+ public static function getInstance ()
28
+ {
29
+ if (self ::$ _instance === null ) {
30
+ self ::$ _instance = new self ();
31
+ }
32
+ return self ::$ _instance ;
33
+ }
34
+
35
+ /**
36
+ * 得到缓存信息
37
+ *
38
+ * @param string $id
39
+ * @return boolean|array
40
+ */
41
+ public static function get ($ id )
42
+ {
43
+ $ instance = self ::getInstance ();
44
+
45
+ //缓存文件不存在
46
+ if (!$ instance ->has ($ id )) {
47
+ return false ;
48
+ }
49
+
50
+ $ file = $ instance ->_file ($ id );
51
+
52
+ $ data = $ instance ->_fileGetContents ($ file );
53
+
54
+ if ($ data ['expire ' ] == 0 || time () < $ data ['expire ' ]) {
55
+ return $ data ['contents ' ];
56
+ }
57
+ return false ;
58
+ }
59
+
60
+ /**
61
+ * 设置一个缓存
62
+ *
63
+ * @param string $id 缓存id
64
+ * @param array $data 缓存内容
65
+ * @param int $cacheLife 缓存生命 默认为0无限生命
66
+ */
67
+ public static function set ($ id , $ data , $ cacheLife = 0 )
68
+ {
69
+ $ instance = self ::getInstance ();
70
+
71
+ $ time = time ();
72
+ $ cache = array ();
73
+ $ cache ['contents ' ] = $ data ;
74
+ $ cache ['expire ' ] = $ cacheLife === 0 ? 0 : $ time + $ cacheLife ;
75
+ $ cache ['mtime ' ] = $ time ;
76
+
77
+ $ file = $ instance ->_file ($ id );
78
+
79
+ return $ instance ->_filePutContents ($ file , $ cache );
80
+ }
81
+
82
+ /**
83
+ * 清除一条缓存
84
+ *
85
+ * @param string cache id
86
+ * @return void
87
+ */
88
+ public static function delete ($ id )
89
+ {
90
+ $ instance = self ::getInstance ();
91
+
92
+ if (!$ instance ->has ($ id )) {
93
+ return false ;
94
+ }
95
+ $ file = $ instance ->_file ($ id );
96
+ //删除该缓存
97
+ return unlink ($ file );
98
+ }
99
+
100
+ /**
101
+ * 判断缓存是否存在
102
+ *
103
+ * @param string $id cache_id
104
+ * @return boolean true 缓存存在 false 缓存不存在
105
+ */
106
+ public static function has ($ id )
107
+ {
108
+ $ instance = self ::getInstance ();
109
+ $ file = $ instance ->_file ($ id );
110
+
111
+ if (!is_file ($ file )) {
112
+ return false ;
113
+ }
114
+ return true ;
115
+ }
116
+
117
+ /**
118
+ * 通过缓存id得到缓存信息路径
119
+ * @param string $id
120
+ * @return string 缓存文件路径
121
+ */
122
+ protected function _file ($ id )
123
+ {
124
+ $ instance = self ::getInstance ();
125
+ $ fileNmae = $ instance ->_idToFileName ($ id );
126
+ return $ instance ->_options ['cache_dir ' ] . $ fileNmae ;
127
+ }
128
+
129
+ /**
130
+ * 通过id得到缓存信息存储文件名
131
+ *
132
+ * @param $id
133
+ * @return string 缓存文件名
134
+ */
135
+ protected function _idToFileName ($ id )
136
+ {
137
+ $ instance = self ::getInstance ();
138
+ $ prefix = $ instance ->_options ['file_name_prefix ' ];
139
+ return $ prefix . '--- ' . $ id ;
140
+ }
141
+
142
+ /**
143
+ * 通过filename得到缓存id
144
+ *
145
+ * @param $id
146
+ * @return string 缓存id
147
+ */
148
+ protected function _fileNameToId ($ fileName )
149
+ {
150
+ $ instance = self ::getInstance ();
151
+ $ prefix = $ instance ->_options ['file_name_prefix ' ];
152
+ return preg_replace ('/^ ' . $ prefix . '---(.*)$/ ' , '$1 ' , $ fileName );
153
+ }
154
+
155
+ /**
156
+ * 把数据写入文件
157
+ *
158
+ * @param string $file 文件名称
159
+ * @param array $contents 数据内容
160
+ * @return bool
161
+ */
162
+ protected function _filePutContents ($ file , $ contents )
163
+ {
164
+ if ($ this ->_options ['mode ' ] == 1 ) {
165
+ $ contents = serialize ($ contents );
166
+ } else {
167
+ $ time = time ();
168
+ $ contents = "<?php \n" .
169
+ " // mktime: " . $ time . "\n" .
170
+ " return " .
171
+ var_export ($ contents , true ) .
172
+ "\n?> " ;
173
+ }
174
+
175
+ $ result = false ;
176
+ $ f = @fopen ($ file , 'w ' );
177
+ if ($ f ) {
178
+ @flock ($ f , LOCK_EX );
179
+ fseek ($ f , 0 );
180
+ ftruncate ($ f , 0 );
181
+ $ tmp = @fwrite ($ f , $ contents );
182
+ if (!($ tmp === false )) {
183
+ $ result = true ;
184
+ }
185
+ @fclose ($ f );
186
+ }
187
+ @chmod ($ file , 0777 );
188
+ return $ result ;
189
+ }
190
+
191
+ /**
192
+ * 从文件得到数据
193
+ *
194
+ * @param sring $file
195
+ * @return boolean|array
196
+ */
197
+ protected function _fileGetContents ($ file )
198
+ {
199
+ if (!is_file ($ file )) {
200
+ return false ;
201
+ }
202
+
203
+ if ($ this ->_options ['mode ' ] == 1 ) {
204
+ $ f = @fopen ($ file , 'r ' );
205
+ @$ data = fread ($ f , filesize ($ file ));
206
+ @fclose ($ f );
207
+ return unserialize ($ data );
208
+ } else {
209
+ return include $ file ;
210
+ }
211
+ }
212
+
213
+ /**
214
+ * 构造函数
215
+ */
216
+ protected function __construct ()
217
+ {
218
+
219
+ }
220
+
221
+ /**
222
+ * 设置缓存路径
223
+ *
224
+ * @param string $path
225
+ * @return self
226
+ */
227
+ public static function setCacheDir ($ path )
228
+ {
229
+ $ instance = self ::getInstance ();
230
+ if (!file_exists ($ path )) {
231
+ mkdir ($ path );
232
+ }
233
+ if (!is_dir ($ path )) {
234
+ exit ('file_cache: ' . $ path . ' 不是一个有效路径 ' );
235
+ }
236
+ if (!is_writable ($ path )) {
237
+ exit ('file_cache: 路径 " ' . $ path . '" 不可写 ' );
238
+ }
239
+
240
+ $ path = rtrim ($ path , '/ ' ) . '/ ' ;
241
+ $ instance ->_options ['cache_dir ' ] = $ path ;
242
+
243
+ return $ instance ;
244
+ }
245
+
246
+ /**
247
+ * 设置缓存文件前缀
248
+ *
249
+ * @param srting $prefix
250
+ * @return self
251
+ */
252
+ public static function setCachePrefix ($ prefix )
253
+ {
254
+ $ instance = self ::getInstance ();
255
+ $ instance ->_options ['file_name_prefix ' ] = $ prefix ;
256
+ return $ instance ;
257
+ }
258
+
259
+ /**
260
+ * 设置缓存存储类型
261
+ *
262
+ * @param int $mode
263
+ * @return self
264
+ */
265
+ public static function setCacheMode ($ mode = 1 )
266
+ {
267
+ $ instance = self ::getInstance ();
268
+ if ($ mode == 1 ) {
269
+ $ instance ->_options ['mode ' ] = 1 ;
270
+ } else {
271
+ $ instance ->_options ['mode ' ] = 2 ;
272
+ }
273
+
274
+ return $ instance ;
275
+ }
276
+
277
+ /**
278
+ * 删除所有缓存
279
+ * @return boolean
280
+ */
281
+ public static function flush ()
282
+ {
283
+ $ instance = self ::getInstance ();
284
+ $ glob = @glob ($ instance ->_options ['cache_dir ' ] . $ instance ->_options ['file_name_prefix ' ] . '--* ' );
285
+
286
+ if (empty ($ glob )) {
287
+ return false ;
288
+ }
289
+
290
+ foreach ($ glob as $ v ) {
291
+ $ fileName = basename ($ v );
292
+ $ id = $ instance ->_fileNameToId ($ fileName );
293
+ $ instance ->delete ($ id );
294
+ }
295
+ return true ;
296
+ }
297
+ }
0 commit comments