BxDolCacheEAccelerator.php 2.2 KB

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