BxDolCacheAPC.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 BxDolCacheAPC 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. $isSucess = false;
  23. $mixedData = apc_fetch ($sKey, $isSucess);
  24. if (!$isSucess)
  25. return null;
  26. return $mixedData;
  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. {
  38. return apc_store ($sKey, $mixedData, false === $iTTL ? $this->iTTL : $iTTL);
  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. {
  48. $isSucess = false;
  49. apc_fetch ($sKey, $isSucess);
  50. if (!$isSucess)
  51. return true;
  52. return apc_delete($sKey);
  53. }
  54. /**
  55. * Check if apc cache functions are available
  56. * @return boolean
  57. */
  58. function isAvailable()
  59. {
  60. return function_exists('apc_store');
  61. }
  62. /**
  63. * Check if apc extension is loaded
  64. * @return boolean
  65. */
  66. function isInstalled()
  67. {
  68. return extension_loaded('apc');
  69. }
  70. /**
  71. * remove all data from cache by key prefix
  72. * @return true on success
  73. */
  74. function removeAllByPrefix ($s)
  75. {
  76. $l = strlen($s);
  77. $aKeys = apc_cache_info('user');
  78. if (isset($aKeys['cache_list']) && is_array($aKeys['cache_list'])) {
  79. foreach ($aKeys['cache_list'] as $r) {
  80. $sKey = $r['info'];
  81. if (0 == strncmp($sKey, $s, $l))
  82. $this->delData($sKey);
  83. }
  84. }
  85. return true;
  86. }
  87. }
  88. /** @} */