1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Ytake\LaravelCouchbase\Cache; |
14
|
|
|
|
15
|
|
|
use Illuminate\Cache\MemcachedStore; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MemcachedBucketStore. |
19
|
|
|
* |
20
|
|
|
* @author Yuuki Takezawa<[email protected]> |
21
|
|
|
*/ |
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
|
|
|
public function __construct($memcached, $prefix = '', array $servers) |
50
|
|
|
{ |
51
|
|
|
parent::__construct($memcached, $prefix); |
52
|
|
|
$this->servers = $servers; |
53
|
|
|
} |
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
|
|
|
public function increment($key, $value = 1) |
64
|
|
|
{ |
65
|
|
|
if ($integer = $this->get($key)) { |
66
|
|
|
$this->put($key, $integer + $value, 0); |
67
|
|
|
|
68
|
|
|
return $integer + $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->put($key, $value, 0); |
72
|
|
|
|
73
|
|
|
return $value; |
74
|
|
|
} |
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
|
|
|
public function decrement($key, $value = 1) |
85
|
|
|
{ |
86
|
|
|
$decrement = 0; |
87
|
|
|
if ($integer = $this->get($key)) { |
88
|
|
|
$decrement = $integer - $value; |
|
|
|
|
89
|
|
|
if ($decrement <= 0) { |
90
|
|
|
$decrement = 0; |
91
|
|
|
} |
92
|
|
|
$this->put($key, $decrement, 0); |
93
|
|
|
|
94
|
|
|
return $decrement; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->put($key, $decrement, 0); |
98
|
|
|
|
99
|
|
|
return $decrement; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function flush() |
106
|
|
|
{ |
107
|
|
|
$handler = curl_multi_init(); |
108
|
|
|
$initialize = null; |
|
|
|
|
109
|
|
|
foreach ($this->servers as $server) { |
110
|
|
|
$initialize = curl_init(); |
111
|
|
|
$configureOption = (isset($server['options'])) ? $server['options'] : []; |
112
|
|
|
|
113
|
|
|
$options = array_replace($this->options, [ |
114
|
|
|
CURLOPT_POST => true, |
115
|
|
|
CURLOPT_URL => $server['host'].sprintf($this->flushEndpoint, $this->port, $server['bucket']), |
116
|
|
|
], $configureOption); |
117
|
|
|
curl_setopt_array($initialize, $options); |
118
|
|
|
curl_multi_add_handle($handler, $initialize); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->callMulti($handler); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $handler |
126
|
|
|
* |
127
|
|
|
* @throws \RuntimeException |
128
|
|
|
*/ |
129
|
|
|
protected function callMulti($handler) |
130
|
|
|
{ |
131
|
|
|
$running = null; |
132
|
|
|
|
133
|
|
|
do { |
134
|
|
|
$stat = curl_multi_exec($handler, $running); |
135
|
|
|
} while ($stat === CURLM_CALL_MULTI_PERFORM); |
136
|
|
|
if (!$running || $stat !== CURLM_OK) { |
137
|
|
|
throw new \RuntimeException('failed to initialized cURL'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
do { |
141
|
|
|
curl_multi_select($handler, $this->timeout); |
142
|
|
|
do { |
143
|
|
|
$stat = curl_multi_exec($handler, $running); |
144
|
|
|
} while ($stat === CURLM_CALL_MULTI_PERFORM); |
145
|
|
|
do { |
146
|
|
|
if ($read = curl_multi_info_read($handler, $remains)) { |
147
|
|
|
$response = curl_multi_getcontent($read['handle']); |
148
|
|
|
|
149
|
|
|
if ($response === false) { |
150
|
|
|
$info = curl_getinfo($read['handle']); |
151
|
|
|
throw new \RuntimeException("error: {$info['url']}: {$info['http_code']}"); |
152
|
|
|
} |
153
|
|
|
curl_multi_remove_handle($handler, $read['handle']); |
154
|
|
|
curl_close($read['handle']); |
155
|
|
|
} |
156
|
|
|
} while ($remains); |
|
|
|
|
157
|
|
|
} while ($running); |
158
|
|
|
curl_multi_close($handler); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param int $second |
163
|
|
|
*/ |
164
|
|
|
public function timeout($second) |
165
|
|
|
{ |
166
|
|
|
$this->timeout = $second; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param int $port |
171
|
|
|
*/ |
172
|
|
|
public function port($port) |
173
|
|
|
{ |
174
|
|
|
$this->port = $port; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|