Source for file FileStorage.php

Documentation is available at FileStorage.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Caching
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Cache file storage.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Caching
  20. 20:  */
  21. 21: class FileStorage extends Object implements ICacheStorage
  22. 22: {
  23. 23:     /**
  24. 24:      * Atomic thread safe logic:
  25. 25:      *
  26. 26:      * 1) reading: open(r+b), lock(SH), read
  27. 27:      *     - delete?: delete*, close
  28. 28:      * 2) deleting: delete*
  29. 29:      * 3) writing: open(r+b || wb), lock(EX), truncate*, write data, write meta, close
  30. 30:      *
  31. 31:      * delete* = try unlink, if fails (on NTFS) { lock(EX), truncate, close, unlink } else close (on ext3)
  32. 32:      */
  33. 33:  
  34. 34:     /**#@+ @ignore internal cache file structure */
  35. 35:     const META_HEADER_LEN 28// 22b signature + 6b meta-struct size + serialized meta-struct + data
  36. 36:     // meta structure: array of
  37. 37:     const META_TIME 'time'// timestamp
  38. 38:     const META_SERIALIZED 'serialized'// is content serialized?
  39. 39:     const META_EXPIRE 'expire'// expiration timestamp
  40. 40:     const META_DELTA 'delta'// relative (sliding) expiration
  41. 41:     const META_ITEMS 'di'// array of dependent items (file => timestamp)
  42. 42:     const META_CALLBACKS 'callbacks'// array of callbacks (function, args)
  43. 43:     /**#@-*/
  44. 44:  
  45. 45:     /**#@+ additional cache structure */
  46. 46:     const FILE = 'file';
  47. 47:     const HANDLE = 'handle';
  48. 48:     /**#@-*/
  49. 49:  
  50. 50:  
  51. 51:     /** @var float  probability that the clean() routine is started */
  52. 52:     public static $gcProbability 0.001;
  53. 53:  
  54. 54:     /** @var bool */
  55. 55:     public static $useDirectories;
  56. 56:  
  57. 57:     /** @var string */
  58. 58:     private $dir;
  59. 59:  
  60. 60:     /** @var bool */
  61. 61:     private $useDirs;
  62. 62:  
  63. 63:     /** @var resource */
  64. 64:     private $db;
  65. 65:  
  66. 66:  
  67. 67:  
  68. 68:     public function __construct($dir)
  69. 69:     {
  70. 70:         if (self::$useDirectories === NULL{
  71. 71:             self::$useDirectories !ini_get('safe_mode');
  72. 72:  
  73. 73:             // checks whether directory is writable
  74. 74:             $uniq uniqid('_'TRUE);
  75. 75:             umask(0000);
  76. 76:             if (!@mkdir("$dir/$uniq"0777)) // intentionally @
  77. 77:                 throw new InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
  78. 78:             }
  79. 79:  
  80. 80:             // tests subdirectory mode
  81. 81:             if (!self::$useDirectories && @file_put_contents("$dir/$uniq/_"''!== FALSE// intentionally @
  82. 82:                 self::$useDirectories TRUE;
  83. 83:                 unlink("$dir/$uniq/_");
  84. 84:             }
  85. 85:             rmdir("$dir/$uniq");
  86. 86:         }
  87. 87:  
  88. 88:         $this->dir $dir;
  89. 89:         $this->useDirs = (bool) self::$useDirectories;
  90. 90:  
  91. 91:         if (mt_rand(mt_getrandmax(self::$gcProbability{
  92. 92:             $this->clean(array());
  93. 93:         }
  94. 94:     }
  95. 95:  
  96. 96:  
  97. 97:  
  98. 98:     /**
  99. 99:      * Read from cache.
  100. 100:      * @param  string key
  101. 101:      * @return mixed|NULL
  102. 102:      */
  103. 103:     public function read($key)
  104. 104:     {
  105. 105:         $meta $this->readMeta($this->getCacheFile($key)LOCK_SH);
  106. 106:         if ($meta && $this->verify($meta)) {
  107. 107:             return $this->readData($meta)// calls fclose()
  108. 108:  
  109. 109:         else {
  110. 110:             return NULL;
  111. 111:         }
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Verifies dependencies.
  118. 118:      * @param  array 
  119. 119:      * @return bool 
  120. 120:      */
  121. 121:     private function verify($meta)
  122. 122:     {
  123. 123:         do {
  124. 124:             if (!empty($meta[self::META_DELTA])) {
  125. 125:                 // meta[file] was added by readMeta()
  126. 126:                 if (filemtime($meta[self::FILE]$meta[self::META_DELTAtime()) break;
  127. 127:                 touch($meta[self::FILE]);
  128. 128:  
  129. 129:             elseif (!empty($meta[self::META_EXPIRE]&& $meta[self::META_EXPIREtime()) {
  130. 130:                 break;
  131. 131:             }
  132. 132:  
  133. 133:             if (!empty($meta[self::META_CALLBACKS]&& !Cache::checkCallbacks($meta[self::META_CALLBACKS])) {
  134. 134:                 break;
  135. 135:             }
  136. 136:  
  137. 137:             if (!empty($meta[self::META_ITEMS])) {
  138. 138:                 foreach ($meta[self::META_ITEMSas $depFile => $time{
  139. 139:                     $m $this->readMeta($depFileLOCK_SH);
  140. 140:                     if ($m[self::META_TIME!== $timebreak 2;
  141. 141:                     if ($m && !$this->verify($m)) break 2;
  142. 142:                 }
  143. 143:             }
  144. 144:  
  145. 145:             return TRUE;
  146. 146:         while (FALSE);
  147. 147:  
  148. 148:         $this->delete($meta[self::FILE]$meta[self::HANDLE])// meta[handle] & meta[file] was added by readMeta()
  149. 149:         return FALSE;
  150. 150:     }
  151. 151:  
  152. 152:  
  153. 153:  
  154. 154:     /**
  155. 155:      * Writes item into the cache.
  156. 156:      * @param  string key
  157. 157:      * @param  mixed  data
  158. 158:      * @param  array  dependencies
  159. 159:      * @return void 
  160. 160:      */
  161. 161:     public function write($key$dataarray $dp)
  162. 162:     {
  163. 163:         $meta array(
  164. 164:             self::META_TIME => microtime(),
  165. 165:         );
  166. 166:  
  167. 167:         if (!is_string($data)) {
  168. 168:             $data serialize($data);
  169. 169:             $meta[self::META_SERIALIZEDTRUE;
  170. 170:         }
  171. 171:  
  172. 172:         if (!empty($dp[Cache::EXPIRE])) {
  173. 173:             if (empty($dp[Cache::SLIDING])) {
  174. 174:                 $meta[self::META_EXPIRE$dp[Cache::EXPIREtime()// absolute time
  175. 175:             else {
  176. 176:                 $meta[self::META_DELTA= (int) $dp[Cache::EXPIRE]// sliding time
  177. 177:             }
  178. 178:         }
  179. 179:  
  180. 180:         if (!empty($dp[Cache::ITEMS])) {
  181. 181:             foreach ((array) $dp[Cache::ITEMSas $item{
  182. 182:                 $depFile $this->getCacheFile($item);
  183. 183:                 $m $this->readMeta($depFileLOCK_SH);
  184. 184:                 $meta[self::META_ITEMS][$depFile$m[self::META_TIME];
  185. 185:                 unset($m);
  186. 186:             }
  187. 187:         }
  188. 188:  
  189. 189:         if (!empty($dp[Cache::CALLBACKS])) {
  190. 190:             $meta[self::META_CALLBACKS$dp[Cache::CALLBACKS];
  191. 191:         }
  192. 192:  
  193. 193:         $cacheFile $this->getCacheFile($key);
  194. 194:         if ($this->useDirs && !is_dir($dir dirname($cacheFile))) {
  195. 195:             umask(0000);
  196. 196:             if (!mkdir($dir0777TRUE)) {
  197. 197:                 return;
  198. 198:             }
  199. 199:         }
  200. 200:         $handle @fopen($cacheFile'r+b')// intentionally @
  201. 201:         if (!$handle{
  202. 202:             $handle fopen($cacheFile'wb')// intentionally @
  203. 203:             if (!$handle{
  204. 204:                 return;
  205. 205:             }
  206. 206:         }
  207. 207:  
  208. 208:         if (!empty($dp[Cache::TAGS]|| isset($dp[Cache::PRIORITY])) {
  209. 209:             $db $this->getDb();
  210. 210:             $dbFile sqlite_escape_string($cacheFile);
  211. 211:             $query '';
  212. 212:             if (!empty($dp[Cache::TAGS])) {
  213. 213:                 foreach ((array) $dp[Cache::TAGSas $tag{
  214. 214:                     $query .= "INSERT INTO cache (file, tag) VALUES ('$dbFile', 'sqlite_escape_string($tag"');";
  215. 215:                 }
  216. 216:             }
  217. 217:             if (isset($dp[Cache::PRIORITY])) {
  218. 218:                 $query .= "INSERT INTO cache (file, priority) VALUES ('$dbFile', '. (int) $dp[Cache::PRIORITY"');";
  219. 219:             }
  220. 220:             if (!sqlite_exec($db"BEGIN; DELETE FROM cache WHERE file = '$dbFile'; $query COMMIT;")) {
  221. 221:                 return;
  222. 222:             }
  223. 223:         }
  224. 224:  
  225. 225:         flock($handleLOCK_EX);
  226. 226:         ftruncate($handle0);
  227. 227:  
  228. 228:         $head serialize($meta'?>';
  229. 229:         $head '<?php //netteCache[01]' str_pad((string) strlen($head)6'0'STR_PAD_LEFT$head;
  230. 230:         $headLen strlen($head);
  231. 231:         $dataLen strlen($data);
  232. 232:  
  233. 233:         do {
  234. 234:             if (fwrite($handlestr_repeat("\x00"$headLen)$headLen!== $headLen{
  235. 235:                 break;
  236. 236:             }
  237. 237:  
  238. 238:             if (fwrite($handle$data$dataLen!== $dataLen{
  239. 239:                 break;
  240. 240:             }
  241. 241:  
  242. 242:             fseek($handle0);
  243. 243:             if (fwrite($handle$head$headLen!== $headLen{
  244. 244:                 break;
  245. 245:             }
  246. 246:  
  247. 247:             fclose($handle);
  248. 248:             return TRUE;
  249. 249:         while (FALSE);
  250. 250:  
  251. 251:         $this->delete($cacheFile$handle);
  252. 252:     }
  253. 253:  
  254. 254:  
  255. 255:  
  256. 256:     /**
  257. 257:      * Removes item from the cache.
  258. 258:      * @param  string key
  259. 259:      * @return void 
  260. 260:      */
  261. 261:     public function remove($key)
  262. 262:     {
  263. 263:         $this->delete($this->getCacheFile($key));
  264. 264:     }
  265. 265:  
  266. 266:  
  267. 267:  
  268. 268:     /**
  269. 269:      * Removes items from the cache by conditions & garbage collector.
  270. 270:      * @param  array  conditions
  271. 271:      * @return void 
  272. 272:      */
  273. 273:     public function clean(array $conds)
  274. 274:     {
  275. 275:         $all !empty($conds[Cache::ALL]);
  276. 276:         $collector empty($conds);
  277. 277:  
  278. 278:         // cleaning using file iterator
  279. 279:         if ($all || $collector{
  280. 280:             $now time();
  281. 281:             $base $this->dir . DIRECTORY_SEPARATOR 'c';
  282. 282:             $iterator new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->dir)RecursiveIteratorIterator::CHILD_FIRST);
  283. 283:             foreach ($iterator as $entry{
  284. 284:                 $path = (string) $entry;
  285. 285:                 if (strncmp($path$basestrlen($base))) // skip files out of cache
  286. 286:                     continue;
  287. 287:                 }
  288. 288:                 if ($entry->isDir()) // collector: remove empty dirs
  289. 289:                     @rmdir($path)// intentionally @
  290. 290:                     continue;
  291. 291:                 }
  292. 292:                 if ($all{
  293. 293:                     $this->delete($path);
  294. 294:  
  295. 295:                 else // collector
  296. 296:                     $meta $this->readMeta($pathLOCK_SH);
  297. 297:                     if (!$metacontinue;
  298. 298:  
  299. 299:                     if (!empty($meta[self::META_EXPIRE]&& $meta[self::META_EXPIRE$now{
  300. 300:                         $this->delete($path$meta[self::HANDLE]);
  301. 301:                         continue;
  302. 302:                     }
  303. 303:  
  304. 304:                     fclose($meta[self::HANDLE]);
  305. 305:                 }
  306. 306:             }
  307. 307:  
  308. 308:             if ($all && extension_loaded('sqlite')) {
  309. 309:                 sqlite_exec("DELETE FROM cache"$this->getDb());
  310. 310:             }
  311. 311:             return;
  312. 312:         }
  313. 313:  
  314. 314:         // cleaning using journal
  315. 315:         if (!empty($conds[Cache::TAGS])) {
  316. 316:             $db $this->getDb();
  317. 317:             foreach ((array) $conds[Cache::TAGSas $tag{
  318. 318:                 $tmp["'" sqlite_escape_string($tag"'";
  319. 319:             }
  320. 320:             $query["tag IN (" implode(','$tmp")";
  321. 321:         }
  322. 322:  
  323. 323:         if (isset($conds[Cache::PRIORITY])) {
  324. 324:             $query["priority <= " . (int) $conds[Cache::PRIORITY];
  325. 325:         }
  326. 326:  
  327. 327:         if (isset($query)) {
  328. 328:             $db $this->getDb();
  329. 329:             $query implode(' OR '$query);
  330. 330:             $files sqlite_single_query("SELECT file FROM cache WHERE $query"$dbFALSE);
  331. 331:             foreach ($files as $file{
  332. 332:                 $this->delete($file);
  333. 333:             }
  334. 334:             sqlite_exec("DELETE FROM cache WHERE $query"$db);
  335. 335:         }
  336. 336:     }
  337. 337:  
  338. 338:  
  339. 339:  
  340. 340:     /**
  341. 341:      * Reads cache data from disk.
  342. 342:      * @param  string  file path
  343. 343:      * @param  int     lock mode
  344. 344:      * @return array|NULL
  345. 345:      */
  346. 346:     protected function readMeta($file$lock)
  347. 347:     {
  348. 348:         $handle @fopen($file'r+b')// intentionally @
  349. 349:         if (!$handlereturn NULL;
  350. 350:  
  351. 351:         flock($handle$lock);
  352. 352:  
  353. 353:         $head stream_get_contents($handleself::META_HEADER_LEN);
  354. 354:         if ($head && strlen($head=== self::META_HEADER_LEN{
  355. 355:             $size = (int) substr($head-6);
  356. 356:             $meta stream_get_contents($handle$sizeself::META_HEADER_LEN);
  357. 357:             $meta @unserialize($meta)// intentionally @
  358. 358:             if (is_array($meta)) {
  359. 359:                 fseek($handle$size self::META_HEADER_LEN)// needed by PHP < 5.2.6
  360. 360:                 $meta[self::FILE$file;
  361. 361:                 $meta[self::HANDLE$handle;
  362. 362:                 return $meta;
  363. 363:             }
  364. 364:         }
  365. 365:  
  366. 366:         fclose($handle);
  367. 367:         return NULL;
  368. 368:     }
  369. 369:  
  370. 370:  
  371. 371:  
  372. 372:     /**
  373. 373:      * Reads cache data from disk and closes cache file handle.
  374. 374:      * @param  array 
  375. 375:      * @return mixed 
  376. 376:      */
  377. 377:     protected function readData($meta)
  378. 378:     {
  379. 379:         $data stream_get_contents($meta[self::HANDLE]);
  380. 380:         fclose($meta[self::HANDLE]);
  381. 381:  
  382. 382:         if (empty($meta[self::META_SERIALIZED])) {
  383. 383:             return $data;
  384. 384:         else {
  385. 385:             return @unserialize($data)// intentionally @
  386. 386:         }
  387. 387:     }
  388. 388:  
  389. 389:  
  390. 390:  
  391. 391:     /**
  392. 392:      * Returns file name.
  393. 393:      * @param  string 
  394. 394:      * @return string 
  395. 395:      */
  396. 396:     protected function getCacheFile($key)
  397. 397:     {
  398. 398:         if ($this->useDirs{
  399. 399:             $key explode(Cache::NAMESPACE_SEPARATOR$key2);
  400. 400:             return $this->dir . '/c' (isset($key[1]'-' urlencode($key[0]'/_' urlencode($key[1]'_' urlencode($key[0]));
  401. 401:         else {
  402. 402:             return $this->dir . '/c_' urlencode($key);
  403. 403:         }
  404. 404:     }
  405. 405:  
  406. 406:  
  407. 407:  
  408. 408:     /**
  409. 409:      * Deletes and closes file.
  410. 410:      * @param  string 
  411. 411:      * @param  resource 
  412. 412:      * @return void 
  413. 413:      */
  414. 414:     private static function delete($file$handle NULL)
  415. 415:     {
  416. 416:         if (@unlink($file)) // intentionally @
  417. 417:             if ($handlefclose($handle);
  418. 418:             return;
  419. 419:         }
  420. 420:  
  421. 421:         if (!$handle{
  422. 422:             $handle @fopen($file'r+')// intentionally @
  423. 423:         }
  424. 424:         if ($handle{
  425. 425:             flock($handleLOCK_EX);
  426. 426:             ftruncate($handle0);
  427. 427:             fclose($handle);
  428. 428:             @unlink($file)// intentionally @; not atomic
  429. 429:         }
  430. 430:     }
  431. 431:  
  432. 432:  
  433. 433:  
  434. 434:     /**
  435. 435:      * Returns SQLite resource.
  436. 436:      * @return resource 
  437. 437:      */
  438. 438:     protected function getDb()
  439. 439:     {
  440. 440:         if ($this->db === NULL{
  441. 441:             if (!extension_loaded('sqlite')) {
  442. 442:                 throw new InvalidStateException("SQLite extension is required for storing tags and priorities.");
  443. 443:             }
  444. 444:             $this->db sqlite_open($this->dir . '/cachejournal.sdb');
  445. 445:             @sqlite_exec($this->db'CREATE TABLE cache (file VARCHAR NOT NULL, priority, tag VARCHAR);
  446. 446:             CREATE INDEX IDX_FILE ON cache (file); CREATE INDEX IDX_PRI ON cache (priority); CREATE INDEX IDX_TAG ON cache (tag);')// intentionally @
  447. 447:         }
  448. 448:         return $this->db;
  449. 449:     }
  450. 450: