BxDolCacheUtilities.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. // TODO: check if it is used or maybe usefull for future use, also it needs some refactoring
  10. class BxDolCacheUtilities extends BxDol
  11. {
  12. protected $oMemberMenu;
  13. protected $oCacheMemberMenu;
  14. protected $oCachePb;
  15. protected $oCacheDb;
  16. protected $oCacheTemplates;
  17. function __construct ()
  18. {
  19. parent::__construct();
  20. // member menu
  21. bx_import('BxDolMemberMenu');
  22. $this->oMemberMenu = new BxDolMemberMenu();
  23. $this->oCacheMemberMenu = $this->oMemberMenu->getCacheObject();
  24. // page blocks
  25. bx_import('BxDolPageViewAdmin');
  26. $oPageViewCacher = new BxDolPageViewCacher ('', '');
  27. $this->oCachePb = $oPageViewCacher->getBlocksCacheObject ();
  28. // DB
  29. $this->oCacheDb = $GLOBALS['MySQL']->getDbCacheObject();
  30. // templates
  31. $this->oCacheTemplates = $GLOBALS['oSysTemplate']->getTemplatesCacheObject();
  32. }
  33. function clear($sCache)
  34. {
  35. return $this->_action($sCache, 'clear');
  36. }
  37. function size($sCache, $isFormatted = false)
  38. {
  39. $iSize = $this->_action($sCache, 'size');
  40. return $isFormatted ? sprintf("%.2f", $iSize / 1024 / 1024) : $iSize;
  41. }
  42. function _action($sCache, $sMode = 'clear')
  43. {
  44. $sFuncCacheObject = ('clear' == $sMode ? '_clearCacheObject' : '_getSizeCacheObject');
  45. $sFuncCacheFile = ('clear' == $sMode ? '_clearCache' : '_getSizeCache');
  46. switch ($sCache) {
  47. case 'member_menu':
  48. $mixedResult = $this->$sFuncCacheObject ($this->oCacheMemberMenu, $this->oMemberMenu->sMenuMemberKeysCache);
  49. break;
  50. case 'pb':
  51. $mixedResult = $this->$sFuncCacheObject ($this->oCacheDb, 'pb_');
  52. break;
  53. case 'users':
  54. $mixedResult = $this->$sFuncCacheFile('user', BX_DIRECTORY_PATH_CACHE);
  55. break;
  56. case 'db':
  57. $mixedResult = $this->$sFuncCacheObject ($this->oCacheDb, 'db_');
  58. break;
  59. case 'template':
  60. $mixedResult = $this->$sFuncCacheObject ($this->oCacheTemplates, $GLOBALS['oSysTemplate']->_sCacheFilePrefix);
  61. break;
  62. case 'css':
  63. $mixedResult = $this->$sFuncCacheFile($GLOBALS['oSysTemplate']->_sCssCachePrefix, BX_DIRECTORY_PATH_CACHE_PUBLIC);
  64. break;
  65. case 'js':
  66. $mixedResult = $this->$sFuncCacheFile($GLOBALS['oSysTemplate']->_sJsCachePrefix, BX_DIRECTORY_PATH_CACHE_PUBLIC);
  67. break;
  68. }
  69. return $mixedResult;
  70. }
  71. function _clearCacheObject($oCache, $sPrefix)
  72. {
  73. if (!$oCache->removeAllByPrefix ($sPrefix))
  74. return array('code' => 1, 'message' => _t('_adm_txt_dashboard_cache_clean_failed'));
  75. else
  76. return array('code' => 0, 'message' => _t('_adm_txt_dashboard_cache_clean_success'));
  77. }
  78. function _getSizeCacheObject($oCache, $sPrefix)
  79. {
  80. return $oCache->getSizeByPrefix ($sPrefix);
  81. }
  82. function _clearCache($sPrefix, $sPath)
  83. {
  84. if (!($rHandler = opendir($sPath)))
  85. return array('code' => 1, 'message' => _t('_adm_txt_dashboard_cache_clean_failed'));
  86. $l = strlen($sPrefix);
  87. while (($sFile = readdir($rHandler)) !== false)
  88. if (0 == strncmp($sFile, $sPrefix, $l))
  89. @unlink($sPath . $sFile);
  90. closedir($rHandler);
  91. return array('code' => 0, 'message' => _t('_adm_txt_dashboard_cache_clean_success'));
  92. }
  93. function _getSizeCache($sPrefix, $sPath)
  94. {
  95. if (!($rHandler = opendir($sPath)))
  96. return 0;
  97. $iSize = 0;
  98. $l = strlen($sPrefix);
  99. while (($sFile = readdir($rHandler)) !== false)
  100. if (0 == strncmp($sFile, $sPrefix, $l))
  101. $iSize += filesize($sPath . $sFile);
  102. closedir($rHandler);
  103. return $iSize;
  104. }
  105. }
  106. /** @} */