PacksPhpRedisValues.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * @throws \RuntimeException
  33. * @throws \UnexpectedValueException
  34. */
  35. public function pack(array $values): array
  36. {
  37. if (empty($values)) {
  38. return $values;
  39. }
  40. if ($this->supportsPacking()) {
  41. return array_map($this->client->_pack(...), $values);
  42. }
  43. if ($this->compressed()) {
  44. if ($this->supportsLzf() && $this->lzfCompressed()) {
  45. if (! function_exists('lzf_compress')) {
  46. throw new RuntimeException("'lzf' extension required to call 'lzf_compress'.");
  47. }
  48. $processor = function ($value) {
  49. return \lzf_compress($this->client->_serialize($value));
  50. };
  51. } elseif ($this->supportsZstd() && $this->zstdCompressed()) {
  52. if (! function_exists('zstd_compress')) {
  53. throw new RuntimeException("'zstd' extension required to call 'zstd_compress'.");
  54. }
  55. $compressionLevel = $this->client->getOption(Redis::OPT_COMPRESSION_LEVEL);
  56. $processor = function ($value) use ($compressionLevel) {
  57. return \zstd_compress(
  58. $this->client->_serialize($value),
  59. $compressionLevel === 0 ? Redis::COMPRESSION_ZSTD_DEFAULT : $compressionLevel
  60. );
  61. };
  62. } else {
  63. throw new UnexpectedValueException(sprintf(
  64. 'Unsupported phpredis compression in use [%d].',
  65. $this->client->getOption(Redis::OPT_COMPRESSION)
  66. ));
  67. }
  68. } else {
  69. $processor = function ($value) {
  70. return $this->client->_serialize($value);
  71. };
  72. }
  73. return array_map($processor, $values);
  74. }
  75. /**
  76. * Execute the given callback without serialization or compression when applicable.
  77. *
  78. * @param callable $callback
  79. * @return mixed
  80. */
  81. public function withoutSerializationOrCompression(callable $callback)
  82. {
  83. $client = $this->client;
  84. $oldSerializer = null;
  85. if ($this->serialized()) {
  86. $oldSerializer = $client->getOption(Redis::OPT_SERIALIZER);
  87. $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
  88. }
  89. $oldCompressor = null;
  90. if ($this->compressed()) {
  91. $oldCompressor = $client->getOption(Redis::OPT_COMPRESSION);
  92. $client->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_NONE);
  93. }
  94. try {
  95. return $callback();
  96. } finally {
  97. if ($oldSerializer !== null) {
  98. $client->setOption(Redis::OPT_SERIALIZER, $oldSerializer);
  99. }
  100. if ($oldCompressor !== null) {
  101. $client->setOption(Redis::OPT_COMPRESSION, $oldCompressor);
  102. }
  103. }
  104. }
  105. /**
  106. * Determine if serialization is enabled.
  107. *
  108. * @return bool
  109. */
  110. public function serialized(): bool
  111. {
  112. return defined('Redis::OPT_SERIALIZER') &&
  113. $this->client->getOption(Redis::OPT_SERIALIZER) !== Redis::SERIALIZER_NONE;
  114. }
  115. /**
  116. * Determine if compression is enabled.
  117. *
  118. * @return bool
  119. */
  120. public function compressed(): bool
  121. {
  122. return defined('Redis::OPT_COMPRESSION') &&
  123. $this->client->getOption(Redis::OPT_COMPRESSION) !== Redis::COMPRESSION_NONE;
  124. }
  125. /**
  126. * Determine if LZF compression is enabled.
  127. *
  128. * @return bool
  129. */
  130. public function lzfCompressed(): bool
  131. {
  132. return defined('Redis::COMPRESSION_LZF') &&
  133. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZF;
  134. }
  135. /**
  136. * Determine if ZSTD compression is enabled.
  137. *
  138. * @return bool
  139. */
  140. public function zstdCompressed(): bool
  141. {
  142. return defined('Redis::COMPRESSION_ZSTD') &&
  143. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_ZSTD;
  144. }
  145. /**
  146. * Determine if LZ4 compression is enabled.
  147. *
  148. * @return bool
  149. */
  150. public function lz4Compressed(): bool
  151. {
  152. return defined('Redis::COMPRESSION_LZ4') &&
  153. $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZ4;
  154. }
  155. /**
  156. * Determine if the current PhpRedis extension version supports packing.
  157. *
  158. * @return bool
  159. */
  160. protected function supportsPacking(): bool
  161. {
  162. if ($this->supportsPacking === null) {
  163. $this->supportsPacking = $this->phpRedisVersionAtLeast('5.3.5');
  164. }
  165. return $this->supportsPacking;
  166. }
  167. /**
  168. * Determine if the current PhpRedis extension version supports LZF compression.
  169. *
  170. * @return bool
  171. */
  172. protected function supportsLzf(): bool
  173. {
  174. if ($this->supportsLzf === null) {
  175. $this->supportsLzf = $this->phpRedisVersionAtLeast('4.3.0');
  176. }
  177. return $this->supportsLzf;
  178. }
  179. /**
  180. * Determine if the current PhpRedis extension version supports Zstd compression.
  181. *
  182. * @return bool
  183. */
  184. protected function supportsZstd(): bool
  185. {
  186. if ($this->supportsZstd === null) {
  187. $this->supportsZstd = $this->phpRedisVersionAtLeast('5.1.0');
  188. }
  189. return $this->supportsZstd;
  190. }
  191. /**
  192. * Determine if the PhpRedis extension version is at least the given version.
  193. *
  194. * @param string $version
  195. * @return bool
  196. */
  197. protected function phpRedisVersionAtLeast(string $version): bool
  198. {
  199. $phpredisVersion = phpversion('redis');
  200. return $phpredisVersion !== false && version_compare($phpredisVersion, $version, '>=');
  201. }
  202. }