BxDolCacheRedis.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 BxDolCacheRedis extends BxDolCache
  10. {
  11. protected $iTTL = 3600;
  12. protected $oRedis = null;
  13. /**
  14. * constructor
  15. */
  16. function __construct()
  17. {
  18. parent::__construct();
  19. $s = getParam('sys_cache_redis_connection_string');
  20. if (class_exists('Redis') && false !== ($a = parse_url($s))) {
  21. $this->oRedis = new Redis();
  22. $sHost = $a['host'] ?? 'localhost';
  23. $iPort = isset($a['port']) && (int)$a['port'] != 0 ? (int)$a['port'] : 6379;
  24. $iDatabase = $a['path'] ? (int)trim($a['path'], '/') : 0;
  25. $sPassword = $a['user'] ?? '';
  26. try {
  27. if (!$this->oRedis->connect($sHost, $iPort)) {
  28. $this->oRedis = null;
  29. } else {
  30. if ($sPassword && !$this->oRedis->auth($sPassword)) {
  31. $this->oRedis = null;
  32. } elseif (!$this->oRedis->select($iDatabase)) {
  33. $this->oRedis = null;
  34. }
  35. }
  36. } catch (Exception $e) {
  37. $this->oRedis = null;
  38. }
  39. if ($this->oRedis != null) {
  40. $serializer = (
  41. defined('Redis::SERIALIZER_IGBINARY') &&
  42. extension_loaded('igbinary')
  43. ) ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP;
  44. $this->oRedis->setOption(Redis::OPT_SERIALIZER, $serializer);
  45. //echoDbgLog("Serializer:" . $this->oRedis->getOption(Redis::OPT_SERIALIZER));
  46. }
  47. }
  48. }
  49. /**
  50. * Get data from cache server
  51. *
  52. * @param string $sKey - cache key
  53. * @param int $iTTL - time to live
  54. * @return the data is got from cache.
  55. */
  56. function getData($sKey, $iTTL = false)
  57. {
  58. if (!$this->oRedis) {
  59. return null;
  60. }
  61. $mixedData = $this->oRedis->get($sKey);
  62. return false === $mixedData ? null : $mixedData;
  63. }
  64. /**
  65. * Save data in cache server
  66. *
  67. * @param string $sKey - cache key
  68. * @param mixed $mixedData - the data to be cached
  69. * @param int $iTTL - time to live
  70. * @return boolean result of operation.
  71. */
  72. function setData($sKey, $mixedData, $iTTL = false)
  73. {
  74. if (!$this->oRedis) {
  75. return false;
  76. }
  77. $iExpire = false === $iTTL ? $this->iTTL : $iTTL;
  78. if ($iExpire) {
  79. return $this->oRedis->setex($sKey, $iExpire, $mixedData);
  80. } else {
  81. return $this->oRedis->set($sKey, $mixedData);
  82. }
  83. }
  84. /**
  85. * Delete cache from cache server
  86. *
  87. * @param string $sKey - cache key
  88. * @return result of the operation
  89. */
  90. function delData($sKey)
  91. {
  92. if (!$this->oRedis) {
  93. return false;
  94. }
  95. $this->oRedis->del($sKey);
  96. return true;
  97. }
  98. /**
  99. * Remove all keys by prefix
  100. *
  101. * @param string $sPrefix - key prefix
  102. * @return boolean result of operation.
  103. */
  104. function removeAllByPrefix($sPrefix)
  105. {
  106. if (!$this->oRedis) {
  107. return false;
  108. }
  109. $aKeys = $this->oRedis->keys($sPrefix . '*');
  110. if ($aKeys && count($aKeys) > 0) {
  111. return $this->oRedis->del($aKeys) > 0;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Get size of all keys by prefix
  117. *
  118. * @param string $sPrefix - key prefix
  119. * @return int size in bytes or 0 if no keys found.
  120. */
  121. function getSizeByPrefix($sPrefix)
  122. {
  123. if (!$this->oRedis) {
  124. return 0;
  125. }
  126. $aKeys = $this->oRedis->keys($sPrefix . '*');
  127. if (!$aKeys) {
  128. return 0;
  129. }
  130. $iSize = 0;
  131. foreach ($aKeys as $sKey) {
  132. $iSize += strlen($this->oRedis->rawCommand('MEMORY', 'USAGE', $sKey));
  133. }
  134. return $iSize;
  135. }
  136. /**
  137. * Check if Redis is available
  138. * @return boolean
  139. */
  140. function isAvailable()
  141. {
  142. return $this->oRedis == null ? false : true;
  143. }
  144. /**
  145. * Check if Redis extension is loaded
  146. * @return boolean
  147. */
  148. function isInstalled()
  149. {
  150. return extension_loaded('redis');
  151. }
  152. }
  153. /** @} */