BxDolCacheUtilities.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. */
  6. class BxDolCacheUtilities extends BxDolMistake
  7. {
  8. protected $oMemberMenu;
  9. protected $oCacheMemberMenu;
  10. protected $oCachePb;
  11. protected $oCacheDb;
  12. protected $oCacheTemplates;
  13. function __construct ()
  14. {
  15. parent::BxDolMistake();
  16. // member menu
  17. bx_import('BxDolMemberMenu');
  18. $this->oMemberMenu = new BxDolMemberMenu();
  19. $this->oCacheMemberMenu = $this->oMemberMenu->getCacheObject();
  20. // page blocks
  21. bx_import('BxDolPageViewAdmin');
  22. $oPageViewCacher = new BxDolPageViewCacher ('', '');
  23. $this->oCachePb = $oPageViewCacher->getBlocksCacheObject ();
  24. // DB
  25. $this->oCacheDb = $GLOBALS['MySQL']->getDbCacheObject();
  26. // templates
  27. $this->oCacheTemplates = $GLOBALS['oSysTemplate']->getTemplatesCacheObject();
  28. }
  29. function clear($sCache)
  30. {
  31. return $this->_action($sCache, 'clear');
  32. }
  33. function size($sCache, $isFormatted = false)
  34. {
  35. $iSize = $this->_action($sCache, 'size');
  36. return $isFormatted ? sprintf("%.2f", $iSize / 1024 / 1024) : $iSize;
  37. }
  38. function _action($sCache, $sMode = 'clear')
  39. {
  40. $sFuncCacheObject = ('clear' == $sMode ? '_clearCacheObject' : '_getSizeCacheObject');
  41. $sFuncCacheFile = ('clear' == $sMode ? '_clearCache' : '_getSizeCache');
  42. switch ($sCache) {
  43. case 'member_menu':
  44. $mixedResult = $this->$sFuncCacheObject ($this->oCacheMemberMenu, $this->oMemberMenu->sMenuMemberKeysCache);
  45. break;
  46. case 'pb':
  47. $mixedResult = $this->$sFuncCacheObject ($this->oCacheDb, 'pb_');
  48. break;
  49. case 'users':
  50. $mixedResult = $this->$sFuncCacheFile('user', BX_DIRECTORY_PATH_CACHE);
  51. break;
  52. case 'db':
  53. $mixedResult = $this->$sFuncCacheObject ($this->oCacheDb, 'db_');
  54. break;
  55. case 'template':
  56. $mixedResult = $this->$sFuncCacheObject ($this->oCacheTemplates, $GLOBALS['oSysTemplate']->_sCacheFilePrefix);
  57. break;
  58. case 'css':
  59. $mixedResult = $this->$sFuncCacheFile($GLOBALS['oSysTemplate']->_sCssCachePrefix, BX_DIRECTORY_PATH_CACHE_PUBLIC);
  60. break;
  61. case 'js':
  62. $mixedResult = $this->$sFuncCacheFile($GLOBALS['oSysTemplate']->_sJsCachePrefix, BX_DIRECTORY_PATH_CACHE_PUBLIC);
  63. break;
  64. }
  65. return $mixedResult;
  66. }
  67. function _clearCacheObject($oCache, $sPrefix)
  68. {
  69. if ('db_' == $sPrefix)
  70. $GLOBALS['MySQL']->oParams->clearCache();
  71. elseif ($this->oMemberMenu->sMenuMemberKeysCache == $sPrefix)
  72. $this->oMemberMenu->deleteMemberMenuCaches();
  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. }