BxDolCacheFile.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (!isset($mixedData))
  35. return null;
  36. return $mixedData;
  37. }
  38. /**
  39. * Save all data in cache file.
  40. *
  41. * @param string $sKey - file name
  42. * @param mixed $mixedData - the data to be cached in the file
  43. * @param int $iTTL - time to live
  44. * @return boolean result of operation.
  45. */
  46. function setData($sKey, $mixedData, $iTTL = false)
  47. {
  48. if(file_exists($this->sPath . $sKey) && !is_writable($this->sPath . $sKey))
  49. return false;
  50. if(!($rHandler = fopen($this->sPath . $sKey, 'w')))
  51. return false;
  52. fwrite($rHandler, '<?php $mixedData=' . var_export($mixedData, true) . '; ?>');
  53. fclose($rHandler);
  54. @chmod($this->sPath . $sKey, 0666);
  55. if (function_exists('opcache_invalidate')) opcache_invalidate($this->sPath . $sKey);
  56. return true;
  57. }
  58. /**
  59. * Delete cache file.
  60. *
  61. * @param string $sKey - file name
  62. * @return result of the operation
  63. */
  64. function delData($sKey)
  65. {
  66. $sFile = $this->sPath . $sKey;
  67. return !file_exists($sFile) || @unlink($sFile);
  68. }
  69. /**
  70. * remove all data from cache by key prefix
  71. * @return true on success
  72. */
  73. function removeAllByPrefix ($s)
  74. {
  75. if (!($rHandler = opendir($this->sPath)))
  76. return false;
  77. $l = strlen($s);
  78. while (($sFile = readdir($rHandler)) !== false)
  79. if (0 === strncmp($sFile, $s, $l))
  80. @unlink ($this->sPath . $sFile);
  81. closedir($rHandler);
  82. return true;
  83. }
  84. /**
  85. * get size of cached data by name prefix
  86. */
  87. function getSizeByPrefix ($s)
  88. {
  89. if (!($rHandler = opendir($this->sPath)))
  90. return false;
  91. $iSize = 0;
  92. $l = strlen($s);
  93. while (($sFile = readdir($rHandler)) !== false)
  94. if (0 === strncmp($sFile, $s, $l))
  95. $iSize += @filesize ($this->sPath . $sFile);
  96. closedir($rHandler);
  97. return $iSize;
  98. }
  99. /**
  100. * remove file from dist if TTL expored
  101. * @param string $sFile - full path to filename
  102. * @param int $iTTL - time to live in seconds
  103. * @return true if TTL is expired and file is deleted or false otherwise
  104. */
  105. function _removeFileIfTtlExpired ($sFile, $iTTL)
  106. {
  107. $iTimeDiff = time() - filectime($sFile);
  108. if ($iTimeDiff > $iTTL) {
  109. @unlink ($sFile);
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }
  115. }
  116. /** @} */