1 | <?php |
||
22 | class MemcachedBucketStore extends MemcachedStore |
||
23 | { |
||
24 | /** @var string[] */ |
||
25 | protected $servers; |
||
26 | |||
27 | /** @var array */ |
||
28 | protected $options = [ |
||
29 | CURLOPT_RETURNTRANSFER => false, |
||
30 | CURLOPT_SSL_VERIFYPEER => false, |
||
31 | ]; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $port = 8091; |
||
35 | |||
36 | /** @var int */ |
||
37 | protected $timeout = 1; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $flushEndpoint = ':%s/pools/default/buckets/%s/controller/doFlush'; |
||
41 | |||
42 | /** |
||
43 | * MemcachedBucketStore constructor. |
||
44 | * |
||
45 | * @param \Memcached $memcached |
||
46 | * @param string $prefix |
||
47 | * @param array $servers |
||
48 | */ |
||
49 | 5 | public function __construct($memcached, $prefix = '', array $servers) |
|
54 | |||
55 | /** |
||
56 | * Increment the value of an item in the cache. |
||
57 | * |
||
58 | * @param string $key |
||
59 | * @param mixed $value |
||
60 | * |
||
61 | * @return int|bool |
||
62 | */ |
||
63 | 2 | public function increment($key, $value = 1) |
|
75 | |||
76 | /** |
||
77 | * Decrement the value of an item in the cache. |
||
78 | * |
||
79 | * @param string $key |
||
80 | * @param mixed $value |
||
81 | * |
||
82 | * @return int|bool |
||
83 | */ |
||
84 | 2 | public function decrement($key, $value = 1) |
|
85 | { |
||
86 | 2 | $decrement = 0; |
|
87 | 2 | if ($integer = $this->get($key)) { |
|
88 | 1 | $decrement = $integer - $value; |
|
|
|||
89 | 1 | if ($decrement <= 0) { |
|
90 | 1 | $decrement = 0; |
|
91 | } |
||
92 | 1 | $this->put($key, $decrement, 0); |
|
93 | |||
94 | 1 | return $decrement; |
|
95 | } |
||
96 | |||
97 | 2 | $this->put($key, $decrement, 0); |
|
98 | |||
99 | 2 | return $decrement; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 1 | public function flush() |
|
106 | { |
||
107 | 1 | $handler = curl_multi_init(); |
|
108 | 1 | $initialize = null; |
|
109 | 1 | foreach ($this->servers as $server) { |
|
110 | 1 | $initialize = curl_init(); |
|
111 | 1 | $configureOption = (isset($server['options'])) ? $server['options'] : []; |
|
112 | |||
113 | 1 | $options = array_replace($this->options, [ |
|
114 | 1 | CURLOPT_POST => true, |
|
115 | 1 | CURLOPT_URL => $server['host'].sprintf($this->flushEndpoint, $this->port, $server['bucket']), |
|
116 | ], $configureOption); |
||
117 | 1 | curl_setopt_array($initialize, $options); |
|
118 | 1 | curl_multi_add_handle($handler, $initialize); |
|
119 | } |
||
120 | |||
121 | 1 | $this->callMulti($handler); |
|
122 | 1 | } |
|
123 | |||
124 | /** |
||
125 | * @param $handler |
||
126 | * |
||
127 | * @throws \RuntimeException |
||
128 | */ |
||
129 | 1 | protected function callMulti($handler) |
|
130 | { |
||
131 | 1 | $running = null; |
|
132 | |||
133 | do { |
||
134 | 1 | $stat = curl_multi_exec($handler, $running); |
|
135 | 1 | } while ($stat === CURLM_CALL_MULTI_PERFORM); |
|
136 | 1 | if (!$running || $stat !== CURLM_OK) { |
|
137 | throw new \RuntimeException('failed to initialized cURL'); |
||
138 | } |
||
139 | |||
140 | do { |
||
141 | 1 | curl_multi_select($handler, $this->timeout); |
|
142 | do { |
||
143 | 1 | $stat = curl_multi_exec($handler, $running); |
|
144 | 1 | } while ($stat === CURLM_CALL_MULTI_PERFORM); |
|
145 | do { |
||
146 | 1 | if ($read = curl_multi_info_read($handler, $remains)) { |
|
147 | 1 | $response = curl_multi_getcontent($read['handle']); |
|
148 | |||
149 | 1 | if ($response === false) { |
|
150 | $info = curl_getinfo($read['handle']); |
||
151 | throw new \RuntimeException("error: {$info['url']}: {$info['http_code']}"); |
||
152 | } |
||
153 | 1 | curl_multi_remove_handle($handler, $read['handle']); |
|
154 | 1 | curl_close($read['handle']); |
|
155 | } |
||
156 | 1 | } while ($remains); |
|
157 | 1 | } while ($running); |
|
158 | 1 | curl_multi_close($handler); |
|
159 | 1 | } |
|
160 | |||
161 | /** |
||
162 | * @param int $second |
||
163 | */ |
||
164 | public function timeout($second) |
||
168 | |||
169 | /** |
||
170 | * @param int $port |
||
171 | */ |
||
172 | public function port($port) |
||
176 | } |
||
177 |