@@ -28,14 +28,14 @@ function __construct($config = array()) {
28
28
}
29
29
30
30
}
31
-
31
+
32
32
private function encodeFilename ($ keyword ) {
33
- return trim (trim (preg_replace ("/[^a-zA-Z0-9]+/ " ,"_ " ,$ keyword ),"_ " ));
33
+ return trim (trim (preg_replace ("/[^a-zA-Z0-9]+/ " ,"_ " ,$ keyword ),"_ " ));
34
34
// return rtrim(base64_encode($keyword), '=');
35
35
}
36
-
36
+
37
37
private function decodeFilename ($ filename ) {
38
- return $ filename ;
38
+ return $ filename ;
39
39
// return base64_decode($filename);
40
40
}
41
41
@@ -44,41 +44,39 @@ private function decodeFilename($filename) {
44
44
*/
45
45
private function getFilePath ($ keyword , $ skip = false ) {
46
46
$ path = $ this ->getPath ();
47
-
48
- $ filename = $ this ->encodeFilename ($ keyword );
47
+
48
+ $ filename = $ this ->encodeFilename ($ keyword );
49
49
$ folder = substr ($ filename ,0 ,2 );
50
50
$ path = rtrim ($ path ,"/ " )."/ " .$ folder ;
51
51
/*
52
52
* Skip Create Sub Folders;
53
53
*/
54
54
if ($ skip == false ) {
55
- if (!file_exists ($ path )) {
56
- if (!@mkdir ($ path ,$ this ->__setChmodAuto ())) {
55
+ //if it doesn't exist, I can't create it, and nobody beat me to creating it:
56
+ if (!@is_dir ($ path ) && !@mkdir ($ path ,$ this ->__setChmodAuto ()) && !@is_dir ($ path )) {
57
+ throw new Exception ("PLEASE CHMOD " .$ this ->getPath ()." - 0777 OR ANY WRITABLE PERMISSION! " ,92 );
58
+ }
59
+ //if it does exist (after someone beat me to it, perhaps), but isn't writable or fixable:
60
+ if (@is_dir ($ path ) && !is_writeable ($ path ) && !@chmod ($ path ,$ this ->__setChmodAuto ())) {
57
61
throw new Exception ("PLEASE CHMOD " .$ this ->getPath ()." - 0777 OR ANY WRITABLE PERMISSION! " ,92 );
58
- }
59
-
60
- } elseif (!is_writeable ($ path )) {
61
- if (!chmod ($ path ,$ this ->__setChmodAuto ())) {
62
- die ("PLEASE CHMOD " .$ this ->getPath ()." - 0777 OR ANY WRITABLE PERMISSION! MAKE SURE PHP/Apache/WebServer have Write Permission " );
63
- }
64
62
}
65
63
}
66
64
67
65
$ file_path = $ path ."/ " .$ filename .".txt " ;
68
66
return $ file_path ;
69
67
}
70
68
71
-
72
69
function driver_set ($ keyword , $ value = "" , $ time = 300 , $ option = array () ) {
73
70
$ file_path = $ this ->getFilePath ($ keyword );
71
+ $ tmp_path = $ file_path . ".tmp " ;
74
72
// echo "<br>DEBUG SET: ".$keyword." - ".$value." - ".$time."<br>";
75
73
$ data = $ this ->encode ($ value );
76
74
77
75
$ toWrite = true ;
78
76
/*
79
77
* Skip if Existing Caching in Options
80
78
*/
81
- if (isset ($ option ['skipExisting ' ]) && $ option ['skipExisting ' ] == true && file_exists ($ file_path )) {
79
+ if (isset ($ option ['skipExisting ' ]) && $ option ['skipExisting ' ] == true && @ file_exists ($ file_path )) {
82
80
$ content = $ this ->readfile ($ file_path );
83
81
$ old = $ this ->decode ($ content );
84
82
$ toWrite = false ;
@@ -87,25 +85,39 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
87
85
}
88
86
}
89
87
90
- if ($ toWrite == true ) {
88
+ $ written = true ;
89
+ /*
90
+ * write to intent file to prevent race during read; race during write is ok
91
+ * because first-to-lock wins and the file will exist before the writer attempts
92
+ * to write.
93
+ */
94
+ if ($ toWrite == true && !@file_exists ($ tmp_path ) && !@file_exists ($ file_path )) {
91
95
try {
92
- $ f = fopen ($ file_path , "w+ " );
93
- fwrite ($ f , $ data );
94
- fclose ($ f );
96
+ $ f = @fopen ($ tmp_path , "c " );
97
+ if ($ f ) {
98
+ if (flock ($ f ,LOCK_EX | LOCK_NB )) {
99
+ $ written = ($ written && fwrite ($ f , $ data ));
100
+ $ written = ($ written && fflush ($ f ));
101
+ $ written = ($ written && flock ($ f , LOCK_UN ));
102
+ } else {
103
+ //arguably the file is being written to so the job is done
104
+ $ written = false ;
105
+ }
106
+ $ written = ($ written && @fclose ($ f ));
107
+ $ written = ($ written && @rename ($ tmp_path ,$ file_path ));
108
+ }
95
109
} catch (Exception $ e ) {
96
110
// miss cache
97
- return false ;
111
+ $ written = false ;
98
112
}
99
113
}
114
+ return $ written ;
100
115
}
101
116
102
-
103
-
104
-
105
117
function driver_get ($ keyword , $ option = array ()) {
106
118
107
119
$ file_path = $ this ->getFilePath ($ keyword );
108
- if (!file_exists ($ file_path )) {
120
+ if (!@ file_exists ($ file_path )) {
109
121
return null ;
110
122
}
111
123
@@ -122,7 +134,7 @@ function driver_get($keyword, $option = array()) {
122
134
123
135
function driver_delete ($ keyword , $ option = array ()) {
124
136
$ file_path = $ this ->getFilePath ($ keyword ,true );
125
- if (@unlink ($ file_path )) {
137
+ if (file_exists ( $ file_path ) && @unlink ($ file_path )) {
126
138
return true ;
127
139
} else {
128
140
return false ;
@@ -147,35 +159,47 @@ function driver_stats($option = array()) {
147
159
148
160
$ total = 0 ;
149
161
$ removed = 0 ;
150
- while ($ file =readdir ($ dir )) {
162
+ $ content = array ();
163
+ while ($ file =@readdir ($ dir )) {
151
164
if ($ file !=". " && $ file !=".. " && is_dir ($ path ."/ " .$ file )) {
152
165
// read sub dir
153
166
$ subdir = @opendir ($ path ."/ " .$ file );
154
167
if (!$ subdir ) {
155
168
throw new Exception ("Can't read path: " .$ path ."/ " .$ file ,93 );
156
169
}
157
170
158
- while ($ f = readdir ($ subdir )) {
171
+ while ($ f = @ readdir ($ subdir )) {
159
172
if ($ f !=". " && $ f !=".. " ) {
160
173
$ file_path = $ path ."/ " .$ file ."/ " .$ f ;
161
- $ size = filesize ($ file_path );
174
+ $ size = @ filesize ($ file_path );
162
175
$ object = $ this ->decode ($ this ->readfile ($ file_path ));
176
+
177
+ if (strpos ($ f ,". " ) === false ) {
178
+ $ key = $ f ;
179
+ }
180
+ else {
181
+ //Because PHP 5.3, this cannot be written in single line
182
+ $ key = explode (". " , $ f );
183
+ $ key = $ key [0 ];
184
+ }
185
+ $ content [$ key ] = array ("size " =>$ size ,"write_time " =>$ object ["write_time " ]);
163
186
if ($ this ->isExpired ($ object )) {
164
187
@unlink ($ file_path );
165
- $ removed = $ removed + $ size ;
188
+ $ removed += $ size ;
166
189
}
167
- $ total = $ total + $ size ;
190
+ $ total += $ size ;
168
191
}
169
192
} // end read subdir
170
193
} // end if
171
194
} // end while
172
195
173
- $ res ['size ' ] = $ total - $ removed ;
196
+ $ res ['size ' ] = $ total - $ removed ;
174
197
$ res ['info ' ] = array (
175
- "Total " => $ total ,
176
- "Removed " => $ removed ,
177
- "Current " => $ res ['size ' ],
198
+ "Total [bytes] " => $ total ,
199
+ "Expired and removed [bytes] " => $ removed ,
200
+ "Current [bytes] " => $ res ['size ' ],
178
201
);
202
+ $ res ["data " ] = $ content ;
179
203
return $ res ;
180
204
}
181
205
@@ -195,15 +219,15 @@ function driver_clean($option = array()) {
195
219
throw new Exception ("Can't read PATH: " .$ path ,94 );
196
220
}
197
221
198
- while ($ file =readdir ($ dir )) {
222
+ while ($ file =@ readdir ($ dir )) {
199
223
if ($ file !=". " && $ file !=".. " && is_dir ($ path ."/ " .$ file )) {
200
224
// read sub dir
201
225
$ subdir = @opendir ($ path ."/ " .$ file );
202
226
if (!$ subdir ) {
203
227
throw new Exception ("Can't read path: " .$ path ."/ " .$ file ,93 );
204
228
}
205
229
206
- while ($ f = readdir ($ subdir )) {
230
+ while ($ f = @ readdir ($ subdir )) {
207
231
if ($ f !=". " && $ f !=".. " ) {
208
232
$ file_path = $ path ."/ " .$ file ."/ " .$ f ;
209
233
@unlink ($ file_path );
@@ -212,13 +236,11 @@ function driver_clean($option = array()) {
212
236
} // end if
213
237
} // end while
214
238
215
-
216
239
}
217
240
218
-
219
241
function driver_isExisting ($ keyword ) {
220
242
$ file_path = $ this ->getFilePath ($ keyword ,true );
221
- if (!file_exists ($ file_path )) {
243
+ if (!@ file_exists ($ file_path )) {
222
244
return false ;
223
245
} else {
224
246
// check expired or not
@@ -232,15 +254,11 @@ function driver_isExisting($keyword) {
232
254
}
233
255
234
256
function isExpired ($ object ) {
235
-
236
- if (isset ($ object ['expired_time ' ]) && @date ("U " ) >= $ object ['expired_time ' ]) {
257
+ if (isset ($ object ['expired_time ' ]) && time () >= $ object ['expired_time ' ]) {
237
258
return true ;
238
259
} else {
239
260
return false ;
240
261
}
241
262
}
242
263
243
-
244
-
245
-
246
264
}
0 commit comments