BxDolCacheFile.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA 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. if (function_exists('opcache_invalidate')) opcache_invalidate($this->sPath . $sKey);
  54. return true;
  55. }
  56. /**
  57. * Delete cache file.
  58. *
  59. * @param string $sKey - file name
  60. * @return result of the operation
  61. */
  62. function delData($sKey)
  63. {
  64. $sFile = $this->sPath . $sKey;
  65. return !file_exists($sFile) || @unlink($sFile);
  66. }
  67. /**
  68. * remove all data from cache by key prefix
  69. * @return true on success
  70. */
  71. function removeAllByPrefix ($s)
  72. {
  73. if (!($rHandler = opendir($this->sPath)))
  74. return false;
  75. $l = strlen($s);
  76. while (($sFile = readdir($rHandler)) !== false)
  77. if (0 === strncmp($sFile, $s, $l))
  78. @unlink ($this->sPath . $sFile);
  79. closedir($rHandler);
  80. return true;
  81. }
  82. /**
  83. * get size of cached data by name prefix
  84. */
  85. function getSizeByPrefix ($s)
  86. {
  87. if (!($rHandler = opendir($this->sPath)))
  88. return false;
  89. $iSize = 0;
  90. $l = strlen($s);
  91. while (($sFile = readdir($rHandler)) !== false)
  92. if (0 === strncmp($sFile, $s, $l))
  93. $iSize += @filesize ($this->sPath . $sFile);
  94. closedir($rHandler);
  95. return $iSize;
  96. }
  97. /**
  98. * remove file from dist if TTL expored
  99. * @param string $sFile - full path to filename
  100. * @param int $iTTL - time to live in seconds
  101. * @return true if TTL is expired and file is deleted or false otherwise
  102. */
  103. function _removeFileIfTtlExpired ($sFile, $iTTL)
  104. {
  105. $iTimeDiff = time() - filectime($sFile);
  106. if ($iTimeDiff > $iTTL) {
  107. @unlink ($sFile);
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. }
  114. /** @} */