BxDolCacheEAccelerator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. bx_import('BxDolCache');
  10. class BxDolCacheEAccelerator extends BxDolCache
  11. {
  12. protected $iTTL = 3600;
  13. /**
  14. * Get data from shared memory cache
  15. *
  16. * @param string $sKey - file name
  17. * @param int $iTTL - time to live
  18. * @return the data is got from cache.
  19. */
  20. function getData($sKey, $iTTL = false)
  21. {
  22. $sData = eaccelerator_get($sKey);
  23. return null === $sData ? null : unserialize($sData);
  24. }
  25. /**
  26. * Save data in shared memory cache
  27. *
  28. * @param string $sKey - file name
  29. * @param mixed $mixedData - the data to be cached in the file
  30. * @param int $iTTL - time to live
  31. * @return boolean result of operation.
  32. */
  33. function setData($sKey, $mixedData, $iTTL = false)
  34. {
  35. $bResult = eaccelerator_put($sKey, serialize($mixedData), false === $iTTL ? $this->iTTL : $iTTL);
  36. return $bResult;
  37. }
  38. /**
  39. * Delete cache from shared memory
  40. *
  41. * @param string $sKey - file name
  42. * @return result of the operation
  43. */
  44. function delData($sKey)
  45. {
  46. eaccelerator_lock($sKey);
  47. eaccelerator_rm($sKey);
  48. eaccelerator_unlock($sKey);
  49. return true;
  50. }
  51. /**
  52. * Check if eAccelerator is available
  53. * @return boolean
  54. */
  55. function isAvailable()
  56. {
  57. return function_exists('eaccelerator_put');
  58. }
  59. /**
  60. * Check if eaccelerator extension is loaded
  61. * @return boolean
  62. */
  63. function isInstalled()
  64. {
  65. return extension_loaded('eaccelerator');
  66. }
  67. /**
  68. * remove all data from cache by key prefix
  69. * @return true on success
  70. */
  71. function removeAllByPrefix ($s)
  72. {
  73. $l = strlen($s);
  74. $aKeys = eaccelerator_list_keys();
  75. foreach ($aKeys as $aKey) {
  76. $sKey = 0 === strpos($aKey['name'], ':') ? substr($aKey['name'], 1) : $aKey['name'];
  77. if (0 == strncmp($sKey, $s, $l))
  78. $this->delData($sKey);
  79. }
  80. return true;
  81. }
  82. }
  83. /** @} */