PacksPhpRedisValues.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Redis;
  4. use RuntimeException;
  5. use UnexpectedValueException;
  6. trait PacksPhpRedisValues
  7. {
  8. /**
  9. * Indicates if Redis supports packing.
  10. *
  11. * @var bool|null
  12. */
  13. protected $supportsPacking;
  14. /**
  15. * Indicates if Redis supports LZF compression.
  16. *
  17. * @var bool|null
  18. */
  19. protected $supportsLzf;
  20. /**
  21. * Indicates if Redis supports Zstd compression.
  22. *
  23. * @var bool|null
  24. */
  25. protected $supportsZstd;
  26. /**
  27. * Prepares the given values to be used with the `eval` command, including serialization and compression.
  28. *
  29. * @param array<int|string,string> $values
  30. * @return array<int|string,string>
  31. */
  32. public function pack(array $values): array
  33. {
  34. if (empty($values)) {
  35. return $values;
  36. }
  37. if ($this->supportsPacking()) {
  38. return array_map([$this->client, '_pack'], $values);
  39. }
  40. if ($this->compressed()) {
  41. if ($this->supportsLzf() && $this->lzfCompressed()) {
  42. if (! function_exists('lzf_compress')) {
  43. throw new RuntimeException("'lzf' extension required to call 'lzf_compress'.");
  44. }
  45. $processor = function ($value) {
  46. return \lzf_compress($this->client->_serialize($value));
  47. };
  48. } elseif ($this->supportsZstd() && $this->zstdCompressed()) {
  49. if (! function_exists('zstd_compress')) {
  50. throw new RuntimeException("'zstd' extension required to call 'zstd_compress'.");
  51. }
  52. $compressionLevel = $this->client->getOption(Redis::OPT_COMPRESSION_LEVEL);
  53. $processor = function ($value) use ($compressionLevel) {
  54. return \zstd_compress(
  55. $this->client->_serialize($value),
  56. $compressionLevel === 0 ? Redis::COMPRESSION_ZSTD_DEFAULT : $compressionLevel
  57. );
  58. };
  59. } else {
  60. throw new UnexpectedValueException(sprintf(
  61. 'Unsupported phpredis compression in use [%d].',
  62. $this->client->getOption(Redis::OPT_COMPRESSION)
  63. ));
  64. }
  65. } else {
  66. $processor = function ($value) {
  67. return $this->client->_serialize($value);
  68. };
  69. }
  70. return array_map($processor, $values);
  71. }
  72. /**
  73. * Determine if compression is enabled.
  74. *
  75. * @return bool
  76. */
  77. public function compressed(): bool
  78. {
  79. return defined('Redis::OPT_COMPRESSION') &&
  80. $this->client->getOption(Redis::OPT_COMPRESSION) !== Redis::COMPRESSION_NONE;
  81. }
  82. /**
  83. * Determine if LZF compression is enabled.
  84. *
  85. * @return bool
  86. */
  87. public function lzfCompressed(): bool
  88. {
  89. return defined('Redis::COMPRESSION_LZF') &&
  90. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZF;
  91. }
  92. /**
  93. * Determine if ZSTD compression is enabled.
  94. *
  95. * @return bool
  96. */
  97. public function zstdCompressed(): bool
  98. {
  99. return defined('Redis::COMPRESSION_ZSTD') &&
  100. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_ZSTD;
  101. }
  102. /**
  103. * Determine if LZ4 compression is enabled.
  104. *
  105. * @return bool
  106. */
  107. public function lz4Compressed(): bool
  108. {
  109. return defined('Redis::COMPRESSION_LZ4') &&
  110. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZ4;
  111. }
  112. /**
  113. * Determine if the current PhpRedis extension version supports packing.
  114. *
  115. * @return bool
  116. */
  117. protected function supportsPacking(): bool
  118. {
  119. if ($this->supportsPacking === null) {
  120. $this->supportsPacking = $this->phpRedisVersionAtLeast('5.3.5');
  121. }
  122. return $this->supportsPacking;
  123. }
  124. /**
  125. * Determine if the current PhpRedis extension version supports LZF compression.
  126. *
  127. * @return bool
  128. */
  129. protected function supportsLzf(): bool
  130. {
  131. if ($this->supportsLzf === null) {
  132. $this->supportsLzf = $this->phpRedisVersionAtLeast('4.3.0');
  133. }
  134. return $this->supportsLzf;
  135. }
  136. /**
  137. * Determine if the current PhpRedis extension version supports Zstd compression.
  138. *
  139. * @return bool
  140. */
  141. protected function supportsZstd(): bool
  142. {
  143. if ($this->supportsZstd === null) {
  144. $this->supportsZstd = $this->phpRedisVersionAtLeast('5.1.0');
  145. }
  146. return $this->supportsZstd;
  147. }
  148. /**
  149. * Determine if the PhpRedis extension version is at least the given version.
  150. *
  151. * @param string $version
  152. * @return bool
  153. */
  154. protected function phpRedisVersionAtLeast(string $version): bool
  155. {
  156. $phpredisVersion = phpversion('redis');
  157. return $phpredisVersion !== false && version_compare($phpredisVersion, $version, '>=');
  158. }
  159. }