BxDolCacheAPC.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolCacheAPC 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. $isSucess = false;
  22. $mixedData = apc_fetch ($sKey, $isSucess);
  23. if (!$isSucess)
  24. return null;
  25. return $mixedData;
  26. }
  27. /**
  28. * Save data in shared memory cache
  29. *
  30. * @param string $sKey - file name
  31. * @param mixed $mixedData - the data to be cached in the file
  32. * @param int $iTTL - time to live
  33. * @return boolean result of operation.
  34. */
  35. function setData($sKey, $mixedData, $iTTL = false)
  36. {
  37. return apc_store ($sKey, $mixedData, false === $iTTL ? $this->iTTL : $iTTL);
  38. }
  39. /**
  40. * Delete cache from shared memory
  41. *
  42. * @param string $sKey - file name
  43. * @return result of the operation
  44. */
  45. function delData($sKey)
  46. {
  47. $isSucess = false;
  48. apc_fetch ($sKey, $isSucess);
  49. if (!$isSucess)
  50. return true;
  51. return apc_delete($sKey);
  52. }
  53. /**
  54. * Check if apc cache functions are available
  55. * @return boolean
  56. */
  57. function isAvailable()
  58. {
  59. return function_exists('apc_store');
  60. }
  61. /**
  62. * Check if apc extension is loaded
  63. * @return boolean
  64. */
  65. function isInstalled()
  66. {
  67. return extension_loaded('apc');
  68. }
  69. /**
  70. * remove all data from cache by key prefix
  71. * @return true on success
  72. */
  73. function removeAllByPrefix ($s)
  74. {
  75. $l = strlen($s);
  76. $aKeys = apc_cache_info('user');
  77. if (isset($aKeys['cache_list']) && is_array($aKeys['cache_list'])) {
  78. foreach ($aKeys['cache_list'] as $r) {
  79. $sKey = $r['info'];
  80. if (0 === strncmp($sKey, $s, $l))
  81. $this->delData($sKey);
  82. }
  83. }
  84. return true;
  85. }
  86. }
  87. /** @} */