PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/pear/Cache_Lite/Lite.php

http://akelosframework.googlecode.com/
PHP | 828 lines | 385 code | 55 blank | 388 comment | 87 complexity | 925013cdecc6c9458a52fb6fac6e74d4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Fast, light and safe Cache Class
  4. *
  5. * Cache_Lite is a fast, light and safe cache system. It's optimized
  6. * for file containers. It is fast and safe (because it uses file
  7. * locking and/or anti-corruption tests).
  8. *
  9. * There are some examples in the 'docs/examples' file
  10. * Technical choices are described in the 'docs/technical' file
  11. *
  12. * Memory Caching is from an original idea of
  13. * Mike BENOIT <ipso@snappymail.ca>
  14. *
  15. * Nota : A chinese documentation (thanks to RainX <china_1982@163.com>) is
  16. * available at :
  17. * http://rainx.phpmore.com/manual/cache_lite.html
  18. *
  19. * @package Cache_Lite
  20. * @category Caching
  21. * @version $Id: Lite.php,v 1.43 2006/05/13 22:11:52 fab Exp $
  22. * @author Fabien MARTY <fab@php.net>
  23. */
  24. define('CACHE_LITE_ERROR_RETURN', 1);
  25. define('CACHE_LITE_ERROR_DIE', 8);
  26. class Cache_Lite
  27. {
  28. // --- Private properties ---
  29. /**
  30. * Directory where to put the cache files
  31. * (make sure to add a trailing slash)
  32. *
  33. * @var string $_cacheDir
  34. */
  35. var $_cacheDir = '/tmp/';
  36. /**
  37. * Enable / disable caching
  38. *
  39. * (can be very usefull for the debug of cached scripts)
  40. *
  41. * @var boolean $_caching
  42. */
  43. var $_caching = true;
  44. /**
  45. * Cache lifetime (in seconds)
  46. *
  47. * If null, the cache is valid forever.
  48. *
  49. * @var int $_lifeTime
  50. */
  51. var $_lifeTime = 3600;
  52. /**
  53. * Enable / disable fileLocking
  54. *
  55. * (can avoid cache corruption under bad circumstances)
  56. *
  57. * @var boolean $_fileLocking
  58. */
  59. var $_fileLocking = true;
  60. /**
  61. * Timestamp of the last valid cache
  62. *
  63. * @var int $_refreshTime
  64. */
  65. var $_refreshTime;
  66. /**
  67. * File name (with path)
  68. *
  69. * @var string $_file
  70. */
  71. var $_file;
  72. /**
  73. * File name (without path)
  74. *
  75. * @var string $_fileName
  76. */
  77. var $_fileName;
  78. /**
  79. * Enable / disable write control (the cache is read just after writing to detect corrupt entries)
  80. *
  81. * Enable write control will lightly slow the cache writing but not the cache reading
  82. * Write control can detect some corrupt cache files but maybe it's not a perfect control
  83. *
  84. * @var boolean $_writeControl
  85. */
  86. var $_writeControl = true;
  87. /**
  88. * Enable / disable read control
  89. *
  90. * If enabled, a control key is embeded in cache file and this key is compared with the one
  91. * calculated after the reading.
  92. *
  93. * @var boolean $_writeControl
  94. */
  95. var $_readControl = true;
  96. /**
  97. * Type of read control (only if read control is enabled)
  98. *
  99. * Available values are :
  100. * 'md5' for a md5 hash control (best but slowest)
  101. * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice)
  102. * 'strlen' for a length only test (fastest)
  103. *
  104. * @var boolean $_readControlType
  105. */
  106. var $_readControlType = 'crc32';
  107. /**
  108. * Pear error mode (when raiseError is called)
  109. *
  110. * (see PEAR doc)
  111. *
  112. * @see setToDebug()
  113. * @var int $_pearErrorMode
  114. */
  115. var $_pearErrorMode = CACHE_LITE_ERROR_RETURN;
  116. /**
  117. * Current cache id
  118. *
  119. * @var string $_id
  120. */
  121. var $_id;
  122. /**
  123. * Current cache group
  124. *
  125. * @var string $_group
  126. */
  127. var $_group;
  128. /**
  129. * Enable / Disable "Memory Caching"
  130. *
  131. * NB : There is no lifetime for memory caching !
  132. *
  133. * @var boolean $_memoryCaching
  134. */
  135. var $_memoryCaching = false;
  136. /**
  137. * Enable / Disable "Only Memory Caching"
  138. * (be carefull, memory caching is "beta quality")
  139. *
  140. * @var boolean $_onlyMemoryCaching
  141. */
  142. var $_onlyMemoryCaching = false;
  143. /**
  144. * Memory caching array
  145. *
  146. * @var array $_memoryCachingArray
  147. */
  148. var $_memoryCachingArray = array();
  149. /**
  150. * Memory caching counter
  151. *
  152. * @var int $memoryCachingCounter
  153. */
  154. var $_memoryCachingCounter = 0;
  155. /**
  156. * Memory caching limit
  157. *
  158. * @var int $memoryCachingLimit
  159. */
  160. var $_memoryCachingLimit = 1000;
  161. /**
  162. * File Name protection
  163. *
  164. * if set to true, you can use any cache id or group name
  165. * if set to false, it can be faster but cache ids and group names
  166. * will be used directly in cache file names so be carefull with
  167. * special characters...
  168. *
  169. * @var boolean $fileNameProtection
  170. */
  171. var $_fileNameProtection = true;
  172. /**
  173. * Enable / disable automatic serialization
  174. *
  175. * it can be used to save directly datas which aren't strings
  176. * (but it's slower)
  177. *
  178. * @var boolean $_serialize
  179. */
  180. var $_automaticSerialization = false;
  181. /**
  182. * Disable / Tune the automatic cleaning process
  183. *
  184. * The automatic cleaning process destroy too old (for the given life time)
  185. * cache files when a new cache file is written.
  186. * 0 => no automatic cache cleaning
  187. * 1 => systematic cache cleaning
  188. * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
  189. *
  190. * @var int $_automaticCleaning
  191. */
  192. var $_automaticCleaningFactor = 0;
  193. /**
  194. * Nested directory level
  195. *
  196. * Set the hashed directory structure level. 0 means "no hashed directory
  197. * structure", 1 means "one level of directory", 2 means "two levels"...
  198. * This option can speed up Cache_Lite only when you have many thousands of
  199. * cache file. Only specific benchs can help you to choose the perfect value
  200. * for you. Maybe, 1 or 2 is a good start.
  201. *
  202. * @var int $_hashedDirectoryLevel
  203. */
  204. var $_hashedDirectoryLevel = 0;
  205. /**
  206. * Umask for hashed directory structure
  207. *
  208. * @var int $_hashedDirectoryUmask
  209. */
  210. var $_hashedDirectoryUmask = 0700;
  211. /**
  212. * API break for error handling in CACHE_LITE_ERROR_RETURN mode
  213. *
  214. * In CACHE_LITE_ERROR_RETURN mode, error handling was not good because
  215. * for example save() method always returned a boolean (a PEAR_Error object
  216. * would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without
  217. * breaking the API, this option (false by default) can change this handling.
  218. *
  219. * @var boolean
  220. */
  221. var $_errorHandlingAPIBreak = false;
  222. // --- Public methods ---
  223. /**
  224. * Constructor
  225. *
  226. * $options is an assoc. Available options are :
  227. * $options = array(
  228. * 'cacheDir' => directory where to put the cache files (string),
  229. * 'caching' => enable / disable caching (boolean),
  230. * 'lifeTime' => cache lifetime in seconds (int),
  231. * 'fileLocking' => enable / disable fileLocking (boolean),
  232. * 'writeControl' => enable / disable write control (boolean),
  233. * 'readControl' => enable / disable read control (boolean),
  234. * 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string),
  235. * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int),
  236. * 'memoryCaching' => enable / disable memory caching (boolean),
  237. * 'onlyMemoryCaching' => enable / disable only memory caching (boolean),
  238. * 'memoryCachingLimit' => max nbr of records to store into memory caching (int),
  239. * 'fileNameProtection' => enable / disable automatic file name protection (boolean),
  240. * 'automaticSerialization' => enable / disable automatic serialization (boolean),
  241. * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int),
  242. * 'hashedDirectoryLevel' => level of the hashed directory system (int),
  243. * 'hashedDirectoryUmask' => umask for hashed directory structure (int),
  244. * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean)
  245. * );
  246. *
  247. * @param array $options options
  248. * @access public
  249. */
  250. function Cache_Lite($options = array(NULL))
  251. {
  252. foreach($options as $key => $value) {
  253. $this->setOption($key, $value);
  254. }
  255. }
  256. /**
  257. * Generic way to set a Cache_Lite option
  258. *
  259. * see Cache_Lite constructor for available options
  260. *
  261. * @var string $name name of the option
  262. * @var mixed $value value of the option
  263. * @access public
  264. */
  265. function setOption($name, $value)
  266. {
  267. $availableOptions = array('errorHandlingAPIBreak', 'hashedDirectoryUmask', 'hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
  268. if (in_array($name, $availableOptions)) {
  269. $property = '_'.$name;
  270. $this->$property = $value;
  271. }
  272. }
  273. /**
  274. * Test if a cache is available and (if yes) return it
  275. *
  276. * @param string $id cache id
  277. * @param string $group name of the cache group
  278. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  279. * @return string data of the cache (else : false)
  280. * @access public
  281. */
  282. function get($id, $group = 'default', $doNotTestCacheValidity = false)
  283. {
  284. $this->_id = $id;
  285. $this->_group = $group;
  286. $data = false;
  287. if ($this->_caching) {
  288. $this->_setRefreshTime();
  289. $this->_setFileName($id, $group);
  290. clearstatcache();
  291. if ($this->_memoryCaching) {
  292. if (isset($this->_memoryCachingArray[$this->_file])) {
  293. if ($this->_automaticSerialization) {
  294. return unserialize($this->_memoryCachingArray[$this->_file]);
  295. }
  296. return $this->_memoryCachingArray[$this->_file];
  297. }
  298. if ($this->_onlyMemoryCaching) {
  299. return false;
  300. }
  301. }
  302. if (($doNotTestCacheValidity) || (is_null($this->_refreshTime))) {
  303. if (file_exists($this->_file)) {
  304. $data = $this->_read();
  305. }
  306. } else {
  307. if ((file_exists($this->_file)) && (@filemtime($this->_file) > $this->_refreshTime)) {
  308. $data = $this->_read();
  309. }
  310. }
  311. if (($data) and ($this->_memoryCaching)) {
  312. $this->_memoryCacheAdd($data);
  313. }
  314. if (($this->_automaticSerialization) and (is_string($data))) {
  315. $data = unserialize($data);
  316. }
  317. return $data;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Save some data in a cache file
  323. *
  324. * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on)
  325. * @param string $id cache id
  326. * @param string $group name of the cache group
  327. * @return boolean true if no problem (else : false or a PEAR_Error object)
  328. * @access public
  329. */
  330. function save($data, $id = NULL, $group = 'default')
  331. {
  332. if ($this->_caching) {
  333. if ($this->_automaticSerialization) {
  334. $data = serialize($data);
  335. }
  336. if (isset($id)) {
  337. $this->_setFileName($id, $group);
  338. }
  339. if ($this->_memoryCaching) {
  340. $this->_memoryCacheAdd($data);
  341. if ($this->_onlyMemoryCaching) {
  342. return true;
  343. }
  344. }
  345. if ($this->_automaticCleaningFactor>0) {
  346. $rand = rand(1, $this->_automaticCleaningFactor);
  347. if ($rand==1) {
  348. $this->clean(false, 'old');
  349. }
  350. }
  351. if ($this->_writeControl) {
  352. $res = $this->_writeAndControl($data);
  353. if (is_bool($res)) {
  354. if ($res) {
  355. return true;
  356. }
  357. // if $res if false, we need to invalidate the cache
  358. @touch($this->_file, time() - 2*abs($this->_lifeTime));
  359. return false;
  360. }
  361. } else {
  362. $res = $this->_write($data);
  363. }
  364. if (is_object($res)) {
  365. // $res is a PEAR_Error object
  366. if (!($this->_errorHandlingAPIBreak)) {
  367. return false; // we return false (old API)
  368. }
  369. }
  370. return $res;
  371. }
  372. return false;
  373. }
  374. /**
  375. * Remove a cache file
  376. *
  377. * @param string $id cache id
  378. * @param string $group name of the cache group
  379. * @return boolean true if no problem
  380. * @access public
  381. */
  382. function remove($id, $group = 'default')
  383. {
  384. $this->_setFileName($id, $group);
  385. if ($this->_memoryCaching) {
  386. if (isset($this->_memoryCachingArray[$this->_file])) {
  387. unset($this->_memoryCachingArray[$this->_file]);
  388. $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
  389. }
  390. if ($this->_onlyMemoryCaching) {
  391. return true;
  392. }
  393. }
  394. return $this->_unlink($this->_file);
  395. }
  396. /**
  397. * Clean the cache
  398. *
  399. * if no group is specified all cache files will be destroyed
  400. * else only cache files of the specified group will be destroyed
  401. *
  402. * @param string $group name of the cache group
  403. * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
  404. * 'callback_myFunction'
  405. * @return boolean true if no problem
  406. * @access public
  407. */
  408. function clean($group = false, $mode = 'ingroup')
  409. {
  410. return $this->_cleanDir($this->_cacheDir, $group, $mode);
  411. }
  412. /**
  413. * Set to debug mode
  414. *
  415. * When an error is found, the script will stop and the message will be displayed
  416. * (in debug mode only).
  417. *
  418. * @access public
  419. */
  420. function setToDebug()
  421. {
  422. $this->setOptions('pearErrorMode', CACHE_LITE_ERROR_DIE);
  423. }
  424. /**
  425. * Set a new life time
  426. *
  427. * @param int $newLifeTime new life time (in seconds)
  428. * @access public
  429. */
  430. function setLifeTime($newLifeTime)
  431. {
  432. $this->_lifeTime = $newLifeTime;
  433. $this->_setRefreshTime();
  434. }
  435. /**
  436. * Save the state of the caching memory array into a cache file cache
  437. *
  438. * @param string $id cache id
  439. * @param string $group name of the cache group
  440. * @access public
  441. */
  442. function saveMemoryCachingState($id, $group = 'default')
  443. {
  444. if ($this->_caching) {
  445. $array = array(
  446. 'counter' => $this->_memoryCachingCounter,
  447. 'array' => $this->_memoryCachingState
  448. );
  449. $data = serialize($array);
  450. $this->save($data, $id, $group);
  451. }
  452. }
  453. /**
  454. * Load the state of the caching memory array from a given cache file cache
  455. *
  456. * @param string $id cache id
  457. * @param string $group name of the cache group
  458. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  459. * @access public
  460. */
  461. function getMemoryCachingState($id, $group = 'default', $doNotTestCacheValidity = false)
  462. {
  463. if ($this->_caching) {
  464. if ($data = $this->get($id, $group, $doNotTestCacheValidity)) {
  465. $array = unserialize($data);
  466. $this->_memoryCachingCounter = $array['counter'];
  467. $this->_memoryCachingArray = $array['array'];
  468. }
  469. }
  470. }
  471. /**
  472. * Return the cache last modification time
  473. *
  474. * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY !
  475. *
  476. * @return int last modification time
  477. */
  478. function lastModified()
  479. {
  480. return @filemtime($this->_file);
  481. }
  482. /**
  483. * Trigger a PEAR error
  484. *
  485. * To improve performances, the PEAR.php file is included dynamically.
  486. * The file is so included only when an error is triggered. So, in most
  487. * cases, the file isn't included and perfs are much better.
  488. *
  489. * @param string $msg error message
  490. * @param int $code error code
  491. * @access public
  492. */
  493. function raiseError($msg, $code)
  494. {
  495. include_once('PEAR.php');
  496. return PEAR::raiseError($msg, $code, $this->_pearErrorMode);
  497. }
  498. /**
  499. * Extend the life of a valid cache file
  500. *
  501. * see http://pear.php.net/bugs/bug.php?id=6681
  502. *
  503. * @access public
  504. */
  505. function extendLife()
  506. {
  507. @touch($this->_file);
  508. }
  509. // --- Private methods ---
  510. /**
  511. * Compute & set the refresh time
  512. *
  513. * @access private
  514. */
  515. function _setRefreshTime()
  516. {
  517. if (is_null($this->_lifeTime)) {
  518. $this->_refreshTime = null;
  519. } else {
  520. $this->_refreshTime = time() - $this->_lifeTime;
  521. }
  522. }
  523. /**
  524. * Remove a file
  525. *
  526. * @param string $file complete file path and name
  527. * @return boolean true if no problem
  528. * @access private
  529. */
  530. function _unlink($file)
  531. {
  532. if (!@unlink($file)) {
  533. return $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
  534. }
  535. return true;
  536. }
  537. /**
  538. * Recursive function for cleaning cache file in the given directory
  539. *
  540. * @param string $dir directory complete path (with a trailing slash)
  541. * @param string $group name of the cache group
  542. * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
  543. 'callback_myFunction'
  544. * @return boolean true if no problem
  545. * @access private
  546. */
  547. function _cleanDir($dir, $group = false, $mode = 'ingroup')
  548. {
  549. if ($this->_fileNameProtection) {
  550. $motif = ($group) ? 'cache_'.md5($group).'_' : 'cache_';
  551. } else {
  552. $motif = ($group) ? 'cache_'.$group.'_' : 'cache_';
  553. }
  554. if ($this->_memoryCaching) {
  555. while (list($key, ) = each($this->_memoryCachingArray)) {
  556. if (strpos($key, $motif, 0)) {
  557. unset($this->_memoryCachingArray[$key]);
  558. $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
  559. }
  560. }
  561. if ($this->_onlyMemoryCaching) {
  562. return true;
  563. }
  564. }
  565. if (!($dh = opendir($dir))) {
  566. return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
  567. }
  568. $result = true;
  569. while ($file = readdir($dh)) {
  570. if (($file != '.') && ($file != '..')) {
  571. if (substr($file, 0, 6)=='cache_') {
  572. $file2 = $dir . $file;
  573. if (is_file($file2)) {
  574. switch (substr($mode, 0, 9)) {
  575. case 'old':
  576. // files older than lifeTime get deleted from cache
  577. if (!is_null($this->_lifeTime)) {
  578. if ((mktime() - @filemtime($file2)) > $this->_lifeTime) {
  579. $result = ($result and ($this->_unlink($file2)));
  580. }
  581. }
  582. break;
  583. case 'notingrou':
  584. if (!strpos($file2, $motif, 0)) {
  585. $result = ($result and ($this->_unlink($file2)));
  586. }
  587. break;
  588. case 'callback_':
  589. $func = substr($mode, 9, strlen($mode) - 9);
  590. if ($func($file2, $group)) {
  591. $result = ($result and ($this->_unlink($file2)));
  592. }
  593. break;
  594. case 'ingroup':
  595. default:
  596. if (strpos($file2, $motif, 0)) {
  597. $result = ($result and ($this->_unlink($file2)));
  598. }
  599. break;
  600. }
  601. }
  602. if ((is_dir($file2)) and ($this->_hashedDirectoryLevel>0)) {
  603. $result = ($result and ($this->_cleanDir($file2 . '/', $group, $mode)));
  604. }
  605. }
  606. }
  607. }
  608. return $result;
  609. }
  610. /**
  611. * Add some date in the memory caching array
  612. *
  613. * @param string $data data to cache
  614. * @access private
  615. */
  616. function _memoryCacheAdd($data)
  617. {
  618. $this->_memoryCachingArray[$this->_file] = $data;
  619. if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) {
  620. list($key, ) = each($this->_memoryCachingArray);
  621. unset($this->_memoryCachingArray[$key]);
  622. } else {
  623. $this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
  624. }
  625. }
  626. /**
  627. * Make a file name (with path)
  628. *
  629. * @param string $id cache id
  630. * @param string $group name of the group
  631. * @access private
  632. */
  633. function _setFileName($id, $group)
  634. {
  635. if ($this->_fileNameProtection) {
  636. $suffix = 'cache_'.md5($group).'_'.md5($id);
  637. } else {
  638. $suffix = 'cache_'.$group.'_'.$id;
  639. }
  640. $root = $this->_cacheDir;
  641. if ($this->_hashedDirectoryLevel>0) {
  642. $hash = md5($suffix);
  643. for ($i=0 ; $i<$this->_hashedDirectoryLevel ; $i++) {
  644. $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/';
  645. }
  646. }
  647. $this->_fileName = $suffix;
  648. $this->_file = $root.$suffix;
  649. }
  650. /**
  651. * Read the cache file and return the content
  652. *
  653. * @return string content of the cache file (else : false or a PEAR_Error object)
  654. * @access private
  655. */
  656. function _read()
  657. {
  658. $fp = @fopen($this->_file, "rb");
  659. if ($this->_fileLocking) @flock($fp, LOCK_SH);
  660. if ($fp) {
  661. clearstatcache();
  662. $length = @filesize($this->_file);
  663. $mqr = get_magic_quotes_runtime();
  664. set_magic_quotes_runtime(0);
  665. if ($this->_readControl) {
  666. $hashControl = @fread($fp, 32);
  667. $length = $length - 32;
  668. }
  669. if ($length) {
  670. $data = @fread($fp, $length);
  671. } else {
  672. $data = '';
  673. }
  674. set_magic_quotes_runtime($mqr);
  675. if ($this->_fileLocking) @flock($fp, LOCK_UN);
  676. @fclose($fp);
  677. if ($this->_readControl) {
  678. $hashData = $this->_hash($data, $this->_readControlType);
  679. if ($hashData != $hashControl) {
  680. if (!(is_null($this->_lifeTime))) {
  681. @touch($this->_file, time() - 2*abs($this->_lifeTime));
  682. } else {
  683. @unlink($this->_file);
  684. }
  685. return false;
  686. }
  687. }
  688. return $data;
  689. }
  690. return $this->raiseError('Cache_Lite : Unable to read cache !', -2);
  691. }
  692. /**
  693. * Write the given data in the cache file
  694. *
  695. * @param string $data data to put in cache
  696. * @return boolean true if ok (a PEAR_Error object else)
  697. * @access private
  698. */
  699. function _write($data)
  700. {
  701. $try = 1;
  702. while ($try<=2) {
  703. $fp = @fopen($this->_file, "wb");
  704. if ($fp) {
  705. if ($this->_fileLocking) @flock($fp, LOCK_EX);
  706. if ($this->_readControl) {
  707. @fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
  708. }
  709. $len = strlen($data);
  710. @fwrite($fp, $data, $len);
  711. if ($this->_fileLocking) @flock($fp, LOCK_UN);
  712. @fclose($fp);
  713. return true;
  714. }
  715. if (($try==1) and ($this->_hashedDirectoryLevel>0)) {
  716. $hash = md5($this->_fileName);
  717. $root = $this->_cacheDir;
  718. for ($i=0 ; $i<$this->_hashedDirectoryLevel ; $i++) {
  719. $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/';
  720. @mkdir($root, $this->_hashedDirectoryUmask);
  721. }
  722. $try = 2;
  723. } else {
  724. $try = 999;
  725. }
  726. }
  727. return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
  728. }
  729. /**
  730. * Write the given data in the cache file and control it just after to avoir corrupted cache entries
  731. *
  732. * @param string $data data to put in cache
  733. * @return boolean true if the test is ok (else : false or a PEAR_Error object)
  734. * @access private
  735. */
  736. function _writeAndControl($data)
  737. {
  738. $result = $this->_write($data);
  739. if (is_object($result)) {
  740. return $result; # We return the PEAR_Error object
  741. }
  742. $dataRead = $this->_read($data);
  743. if (is_object($dataRead)) {
  744. return $result; # We return the PEAR_Error object
  745. }
  746. if ((is_bool($dataRead)) && (!$dataRead)) {
  747. return false;
  748. }
  749. return ($dataRead==$data);
  750. }
  751. /**
  752. * Make a control key with the string containing datas
  753. *
  754. * @param string $data data
  755. * @param string $controlType type of control 'md5', 'crc32' or 'strlen'
  756. * @return string control key
  757. * @access private
  758. */
  759. function _hash($data, $controlType)
  760. {
  761. switch ($controlType) {
  762. case 'md5':
  763. return md5($data);
  764. case 'crc32':
  765. return sprintf('% 32d', crc32($data));
  766. case 'strlen':
  767. return sprintf('% 32d', strlen($data));
  768. default:
  769. return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5);
  770. }
  771. }
  772. }
  773. ?>