Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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