BxDolCacheMemcache.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 BxDolCacheMemcache extends BxDolCache
  11. {
  12. protected $iTTL = 3600;
  13. protected $iStoreFlag = 0;
  14. protected $oMemcache = null;
  15. /**
  16. * constructor
  17. */
  18. function __construct()
  19. {
  20. parent::__construct();
  21. if (class_exists('Memcache')) {
  22. $this->oMemcache = new Memcache();
  23. if (!$this->oMemcache->connect (getParam('sys_cache_memcache_host'), getParam('sys_cache_memcache_port')))
  24. $this->oMemcache = null;
  25. }
  26. }
  27. /**
  28. * Get data from cache server
  29. *
  30. * @param string $sKey - file name
  31. * @param int $iTTL - time to live
  32. * @return the data is got from cache.
  33. */
  34. function getData($sKey, $iTTL = false)
  35. {
  36. $mixedData = $this->oMemcache->get($sKey);
  37. return false === $mixedData ? null : $mixedData;
  38. }
  39. /**
  40. * Save data in cache server
  41. *
  42. * @param string $sKey - file name
  43. * @param mixed $mixedData - the data to be cached in the file
  44. * @param int $iTTL - time to live
  45. * @return boolean result of operation.
  46. */
  47. function setData($sKey, $mixedData, $iTTL = false)
  48. {
  49. return $this->oMemcache->set($sKey, $mixedData, $this->iStoreFlag, false === $iTTL ? $this->iTTL : $iTTL);
  50. }
  51. /**
  52. * Delete cache from cache server
  53. *
  54. * @param string $sKey - file name
  55. * @return result of the operation
  56. */
  57. function delData($sKey)
  58. {
  59. $this->oMemcache->delete($sKey);
  60. return true;
  61. }
  62. /**
  63. * Check if memcache is available
  64. * @return boolean
  65. */
  66. function isAvailable()
  67. {
  68. return $this->oMemcache == null ? false : true;
  69. }
  70. /**
  71. * Check if memcache extension is loaded
  72. * @return boolean
  73. */
  74. function isInstalled()
  75. {
  76. return extension_loaded('memcache');
  77. }
  78. /**
  79. * remove all data from cache by key prefix
  80. * @return true on success
  81. */
  82. function removeAllByPrefix ($s)
  83. {
  84. // not implemented for current cache
  85. return false;
  86. }
  87. }
  88. /** @} */