BxDolCacheFile.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolCache');
  9. class BxDolCacheFile extends BxDolCache {
  10. var $sPath;
  11. /**
  12. * constructor
  13. */
  14. function BxDolCacheFile() {
  15. parent::BxDolCache();
  16. $this->sPath = BX_DIRECTORY_PATH_CACHE;
  17. }
  18. /**
  19. * Get all data from the cache file.
  20. *
  21. * @param string $sKey - file name
  22. * @param int $iTTL - time to live
  23. * @return the data is got from cache.
  24. */
  25. function getData($sKey, $iTTL = false) {
  26. if (!file_exists($this->sPath . $sKey))
  27. return null;
  28. if ($iTTL > 0 && $this->_removeFileIfTtlExpired ($this->sPath . $sKey, $iTTL))
  29. return null;
  30. include($this->sPath . $sKey);
  31. return $mixedData;
  32. }
  33. /**
  34. * Save all data in cache file.
  35. *
  36. * @param string $sKey - file name
  37. * @param mixed $mixedData - the data to be cached in the file
  38. * @param int $iTTL - time to live
  39. * @return boolean result of operation.
  40. */
  41. function setData($sKey, $mixedData, $iTTL = false) {
  42. if(file_exists($this->sPath . $sKey) && !is_writable($this->sPath . $sKey))
  43. return false;
  44. if(!($rHandler = fopen($this->sPath . $sKey, 'w')))
  45. return false;
  46. fwrite($rHandler, '<?php $mixedData=' . var_export($mixedData, true) . '; ?>');
  47. fclose($rHandler);
  48. @chmod($this->sPath . $sKey, 0666);
  49. return true;
  50. }
  51. /**
  52. * Delete cache file.
  53. *
  54. * @param string $sKey - file name
  55. * @return result of the operation
  56. */
  57. function delData($sKey) {
  58. $sFile = $this->sPath . $sKey;
  59. return !file_exists($sFile) || @unlink($sFile);
  60. }
  61. /**
  62. * remove all data from cache by key prefix
  63. * @return true on success
  64. */
  65. function removeAllByPrefix ($s) {
  66. if (!($rHandler = opendir($this->sPath)))
  67. return false;
  68. $l = strlen($s);
  69. while (($sFile = readdir($rHandler)) !== false)
  70. if (0 == strncmp($sFile, $s, $l))
  71. @unlink ($this->sPath . $sFile);
  72. closedir($rHandler);
  73. return true;
  74. }
  75. /**
  76. * get size of cached data by name prefix
  77. */
  78. function getSizeByPrefix ($s) {
  79. if (!($rHandler = opendir($this->sPath)))
  80. return false;
  81. $iSize = 0;
  82. $l = strlen($s);
  83. while (($sFile = readdir($rHandler)) !== false)
  84. if (0 == strncmp($sFile, $s, $l))
  85. $iSize += @filesize ($this->sPath . $sFile);
  86. closedir($rHandler);
  87. return $iSize;
  88. }
  89. /**
  90. * remove file from dist if TTL expored
  91. * @param string $sFile - full path to filename
  92. * @param int $iTTL - time to live in seconds
  93. * @return true if TTL is expired and file is deleted or false otherwise
  94. */
  95. function _removeFileIfTtlExpired ($sFile, $iTTL) {
  96. $iTimeDiff = time() - filectime($sFile);
  97. if ($iTimeDiff > $iTTL) {
  98. @unlink ($sFile);
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104. }