1
0

BxDolCacheFile.php 3.3 KB

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