BxDolCacheMemcache.php 2.8 KB

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