|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Opine\Cache |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23
|
|
|
* THE SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace Opine\Cache; |
|
26
|
|
|
|
|
27
|
|
|
use Memcache; |
|
28
|
|
|
use Exception; |
|
29
|
|
|
use Closure; |
|
30
|
|
|
use Opine\Interfaces\Cache as CacheInterface; |
|
31
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
32
|
|
|
|
|
33
|
|
|
class Service implements CacheInterface |
|
34
|
|
|
{ |
|
35
|
|
|
private $memcache; |
|
36
|
|
|
private $host = false; |
|
37
|
|
|
private $port = false; |
|
38
|
|
|
private $root; |
|
39
|
|
|
|
|
40
|
8 |
|
public function __construct($root) |
|
41
|
|
|
{ |
|
42
|
8 |
|
$this->root = $root; |
|
43
|
|
|
|
|
44
|
|
|
// determine config environment |
|
45
|
8 |
|
$environment = 'default'; |
|
46
|
8 |
|
$test = getenv('OPINE_ENV'); |
|
47
|
8 |
|
if ($test !== false) { |
|
48
|
|
|
$environment = $test; |
|
49
|
|
|
} |
|
50
|
8 |
|
if ($environment == 'default') { |
|
51
|
8 |
|
$environment = '.'; |
|
52
|
8 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
// determine path of file |
|
55
|
8 |
|
$path = $root . '/config/settings/' . $environment . '/cache.yml'; |
|
56
|
8 |
|
if (!file_exists($path) && $environment != '.') { |
|
57
|
|
|
$path = $root . '/config/settings/cache.yml'; |
|
58
|
|
|
} |
|
59
|
8 |
|
if (!file_exists($path)) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// read configuration |
|
64
|
8 |
|
$config = Yaml::parse(file_get_contents($path)); |
|
65
|
|
|
|
|
66
|
8 |
|
$this->host = $config['settings']['host']; |
|
67
|
8 |
|
$this->port = $config['settings']['port']; |
|
68
|
8 |
|
if (!$this->check()) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
8 |
|
$this->memcache = new Memcache(); |
|
72
|
8 |
|
} |
|
73
|
|
|
|
|
74
|
8 |
|
private function check() |
|
75
|
|
|
{ |
|
76
|
8 |
|
if (!class_exists('Memcache')) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
8 |
|
if ($this->host === false) { |
|
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
8 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function delete($key, $timeout = 0) |
|
86
|
|
|
{ |
|
87
|
1 |
|
if (!$this->check()) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
91
|
1 |
|
if ($result === false) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $this->memcache->delete($key, $timeout); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function set($key, $value, $expire = 0, $flag = 2) |
|
99
|
|
|
{ |
|
100
|
1 |
|
if (!$this->check()) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
104
|
1 |
|
if ($result === false) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this->memcache->set($key, $value, $flag, $expire); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
public function get($key, $flag = 2) |
|
112
|
|
|
{ |
|
113
|
4 |
|
if (!$this->check()) { |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
4 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
117
|
4 |
|
if ($result === false) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
return $this->memcache->get($key, $flag); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function getSetGet($key, Closure $callback, $ttl = 0, $flag = 2) |
|
125
|
|
|
{ |
|
126
|
1 |
|
if (!$this->check()) { |
|
127
|
|
|
return $callback(); |
|
128
|
|
|
} |
|
129
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
130
|
1 |
|
if ($result === false) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
1 |
|
$data = $this->memcache->get($key, $flag); |
|
134
|
1 |
|
if ($data === false) { |
|
135
|
1 |
|
$data = $callback(); |
|
136
|
1 |
|
if ($data !== false) { |
|
137
|
1 |
|
$this->memcache->set($key, $data, $flag, $ttl); |
|
138
|
1 |
|
} |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
return $data; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
public function getSetGetBatch(Array &$items, $ttl = 0, $flag = 2) |
|
145
|
|
|
{ |
|
146
|
2 |
|
foreach ($items as $item) { |
|
147
|
2 |
|
if (!is_callable($item)) { |
|
148
|
1 |
|
throw new Exception('each item must have a callback defined'); |
|
149
|
|
|
} |
|
150
|
1 |
|
} |
|
151
|
1 |
|
if (!$this->check()) { |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
155
|
1 |
|
if ($result === false) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
1 |
|
$data = $this->memcache->get(array_keys($items)); |
|
159
|
1 |
|
foreach ($items as $key => &$item) { |
|
160
|
1 |
|
if (!isset($data[$key]) || $data[$key] === false) { |
|
161
|
1 |
|
$items[$key] = $item(); |
|
162
|
1 |
|
} else { |
|
163
|
1 |
|
$items[$key] = $data[$key]; |
|
164
|
|
|
} |
|
165
|
1 |
|
if ($items[$key] !== false) { |
|
166
|
1 |
|
$this->memcache->set($key, $items[$key], $flag, $ttl); |
|
167
|
1 |
|
} |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
public function getBatch(Array &$items, $flag = 2) |
|
174
|
|
|
{ |
|
175
|
1 |
|
if (!$this->check()) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
1 |
|
$count = sizeof($items); |
|
179
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
180
|
1 |
|
if ($result === false) { |
|
181
|
|
|
return false; |
|
182
|
|
|
} |
|
183
|
1 |
|
$data = $this->memcache->get(array_keys($items), $flag); |
|
184
|
1 |
|
$hits = 0; |
|
185
|
1 |
|
foreach ($items as $key => $item) { |
|
186
|
1 |
|
if (array_key_exists($key, $data)) { |
|
187
|
1 |
|
$items[$key] = $data[$key]; |
|
188
|
1 |
|
$hits++; |
|
189
|
1 |
|
} |
|
190
|
1 |
|
} |
|
191
|
1 |
|
return $hits == $count; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
public function deleteBatch(Array $items, $timeout = 0) |
|
195
|
|
|
{ |
|
196
|
1 |
|
if (!$this->check()) { |
|
197
|
|
|
return false; |
|
198
|
|
|
} |
|
199
|
1 |
|
$result = @$this->memcache->pconnect($this->host, $this->port); |
|
200
|
1 |
|
if ($result === false) { |
|
201
|
|
|
return false; |
|
202
|
|
|
} |
|
203
|
1 |
|
foreach ($items as $item) { |
|
204
|
1 |
|
$this->memcache->delete($item, $timeout); |
|
205
|
1 |
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
return true; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|