BxDolCacheEAccelerator.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolCache');
  9. class BxDolCacheEAccelerator extends BxDolCache {
  10. var $iTTL = 3600;
  11. /**
  12. * constructor
  13. */
  14. function BxDolCacheEAccelerator() {
  15. parent::BxDolCache();
  16. }
  17. /**
  18. * Get data from shared memory cache
  19. *
  20. * @param string $sKey - file name
  21. * @param int $iTTL - time to live
  22. * @return the data is got from cache.
  23. */
  24. function getData($sKey, $iTTL = false) {
  25. $sData = eaccelerator_get($sKey);
  26. return null === $sData ? null : unserialize($sData);
  27. }
  28. /**
  29. * Save data in shared memory cache
  30. *
  31. * @param string $sKey - file name
  32. * @param mixed $mixedData - the data to be cached in the file
  33. * @param int $iTTL - time to live
  34. * @return boolean result of operation.
  35. */
  36. function setData($sKey, $mixedData, $iTTL = false) {
  37. $bResult = eaccelerator_put($sKey, serialize($mixedData), false === $iTTL ? $this->iTTL : $iTTL);
  38. return $bResult;
  39. }
  40. /**
  41. * Delete cache from shared memory
  42. *
  43. * @param string $sKey - file name
  44. * @return result of the operation
  45. */
  46. function delData($sKey) {
  47. eaccelerator_lock($sKey);
  48. eaccelerator_rm($sKey);
  49. eaccelerator_unlock($sKey);
  50. return true;
  51. }
  52. /**
  53. * Check if eAccelerator is available
  54. * @return boolean
  55. */
  56. function isAvailable() {
  57. return function_exists('eaccelerator_put');
  58. }
  59. /**
  60. * Check if eaccelerator extension is loaded
  61. * @return boolean
  62. */
  63. function isInstalled() {
  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. $l = strlen($s);
  72. $aKeys = eaccelerator_list_keys();
  73. foreach ($aKeys as $aKey) {
  74. $sKey = 0 === strpos($aKey['name'], ':') ? substr($aKey['name'], 1) : $aKey['name'];
  75. if (0 == strncmp($sKey, $s, $l))
  76. $this->delData($sKey);
  77. }
  78. return true;
  79. }
  80. }