BxDolCacheFileHtml.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 BxDolCacheFileHtml extends BxDolCacheFile
  10. {
  11. /**
  12. * Get all data from the cache file.
  13. *
  14. * @param string $sKey - file name
  15. * @param int $iTTL - time to live
  16. * @return the data is got from cache.
  17. */
  18. function getData($sKey, $iTTL = false)
  19. {
  20. if(!file_exists($this->sPath . $sKey))
  21. return null;
  22. if ($iTTL > 0 && $this->_removeFileIfTtlExpired ($this->sPath . $sKey, $iTTL))
  23. return null;
  24. return file_get_contents($this->sPath . $sKey);
  25. }
  26. /**
  27. * Get full path to cache file
  28. */
  29. function getDataFilePath($sKey, $iTTL = false)
  30. {
  31. if (!file_exists($this->sPath . $sKey))
  32. return null;
  33. if ($iTTL > 0 && $this->_removeFileIfTtlExpired ($this->sPath . $sKey, $iTTL))
  34. return null;
  35. return $this->sPath . $sKey;
  36. }
  37. /**
  38. * Save all data in cache file.
  39. *
  40. * @param string $sKey - file name
  41. * @param mixed $mixedData - the data to be cached in the file
  42. * @param int $iTTL - time to live
  43. * @return boolean result of operation.
  44. */
  45. function setData($sKey, $mixedData, $iTTL = false)
  46. {
  47. if(file_exists($this->sPath . $sKey) && !is_writable($this->sPath . $sKey))
  48. return false;
  49. if(!($rHandler = fopen($this->sPath . $sKey, 'w')))
  50. return false;
  51. fwrite($rHandler, $mixedData);
  52. fclose($rHandler);
  53. @chmod($this->sPath . $sKey, 0666);
  54. if (function_exists('opcache_invalidate')) opcache_invalidate($this->sPath . $sKey);
  55. return true;
  56. }
  57. }
  58. /** @} */