BxDolCacheFileHtml.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $sFileName = $this->sPath . $sKey;
  48. $sFileNameTmp = $this->sPath . '.' . uniqid('', true) . '.tmp';
  49. if(file_exists($sFileName) && !is_writable($sFileName))
  50. return false;
  51. if (false === file_put_contents($sFileNameTmp, $mixedData))
  52. return false;
  53. rename($sFileNameTmp, $sFileName);
  54. @chmod($sFileName, 0666);
  55. if (function_exists('opcache_invalidate')) opcache_invalidate($sFileName, true);
  56. return true;
  57. }
  58. }
  59. /** @} */