BxDolCacheAPC.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 BxDolCacheAPC extends BxDolCache {
  10. var $iTTL = 3600;
  11. /**
  12. * constructor
  13. */
  14. function BxDolCacheAPC() {
  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. $isSucess = false;
  26. $mixedData = apc_fetch ($sKey, $isSucess);
  27. if (!$isSucess)
  28. return null;
  29. return $mixedData;
  30. }
  31. /**
  32. * Save data in shared memory cache
  33. *
  34. * @param string $sKey - file name
  35. * @param mixed $mixedData - the data to be cached in the file
  36. * @param int $iTTL - time to live
  37. * @return boolean result of operation.
  38. */
  39. function setData($sKey, $mixedData, $iTTL = false) {
  40. return apc_store ($sKey, $mixedData, false === $iTTL ? $this->iTTL : $iTTL);
  41. }
  42. /**
  43. * Delete cache from shared memory
  44. *
  45. * @param string $sKey - file name
  46. * @return result of the operation
  47. */
  48. function delData($sKey) {
  49. $isSucess = false;
  50. apc_fetch ($sKey, $isSucess);
  51. if (!$isSucess)
  52. return true;
  53. return apc_delete($sKey);
  54. }
  55. /**
  56. * Check if apc cache functions are available
  57. * @return boolean
  58. */
  59. function isAvailable() {
  60. return function_exists('apc_store');
  61. }
  62. /**
  63. * Check if apc extension is loaded
  64. * @return boolean
  65. */
  66. function isInstalled() {
  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. $l = strlen($s);
  75. $aKeys = apc_cache_info('user');
  76. if (isset($aKeys['cache_list']) && is_array($aKeys['cache_list'])) {
  77. foreach ($aKeys['cache_list'] as $r) {
  78. $sKey = $r['info'];
  79. if (0 == strncmp($sKey, $s, $l))
  80. $this->delData($sKey);
  81. }
  82. }
  83. return true;
  84. }
  85. }