BxDolCacheMemcached.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 BxDolCacheMemcached extends BxDolCache
  10. {
  11. protected $iTTL = 3600;
  12. protected $oMemcached = null;
  13. /**
  14. * Constructor
  15. */
  16. function __construct()
  17. {
  18. parent::__construct();
  19. if (class_exists('Memcached')) {
  20. // Persistent connection
  21. $this->oMemcached = new Memcached('una_cache_pool');
  22. // Avoid adding servers repeatedly
  23. if (!count($this->oMemcached->getServerList())) {
  24. $sHost = getParam('sys_cache_memcache_host');
  25. $iPort = (int)getParam('sys_cache_memcache_port');
  26. if (false === strpos($sHost, ',')) {
  27. $this->oMemcached->addServer(
  28. trim($sHost),
  29. $iPort
  30. );
  31. } else {
  32. $aHosts = explode(',', $sHost);
  33. if ($aHosts) {
  34. $this->oMemcached->setOption(
  35. Memcached::OPT_DISTRIBUTION,
  36. Memcached::DISTRIBUTION_CONSISTENT
  37. );
  38. $this->oMemcached->setOption(
  39. Memcached::OPT_LIBKETAMA_COMPATIBLE,
  40. true
  41. );
  42. foreach ($aHosts as $s) {
  43. $this->oMemcached->addServer(
  44. trim($s),
  45. $iPort
  46. );
  47. }
  48. } else {
  49. $this->oMemcached = null;
  50. }
  51. }
  52. }
  53. // Verify connection
  54. if ($this->oMemcached) {
  55. $stats = $this->oMemcached->getStats();
  56. if (empty($stats)) {
  57. $this->oMemcached = null;
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * Get data from cache
  64. */
  65. function getData($sKey, $iTTL = false)
  66. {
  67. $mixedData = $this->oMemcached->get($sKey);
  68. if ($mixedData === false && $this->oMemcached->getResultCode() === Memcached::RES_NOTFOUND) {
  69. return null;
  70. }
  71. return $mixedData;
  72. }
  73. /**
  74. * Save data to cache
  75. */
  76. function setData($sKey, $mixedData, $iTTL = false)
  77. {
  78. return $this->oMemcached->set(
  79. $sKey,
  80. $mixedData,
  81. false === $iTTL ? $this->iTTL : $iTTL
  82. );
  83. }
  84. /**
  85. * Delete cache item
  86. */
  87. function delData($sKey)
  88. {
  89. $this->oMemcached->delete($sKey);
  90. return true;
  91. }
  92. /**
  93. * Check if Memcached is available
  94. */
  95. function isAvailable()
  96. {
  97. return $this->oMemcached !== null;
  98. }
  99. /**
  100. * Check if extension is installed
  101. */
  102. function isInstalled()
  103. {
  104. return extension_loaded('memcached');
  105. }
  106. /**
  107. * Flush all cache
  108. * Note: ignores prefix parameter
  109. */
  110. function removeAllByPrefix($s)
  111. {
  112. return $this->oMemcached->flush();
  113. }
  114. }
  115. /** @} */