1
|
|
|
<?php namespace Comodojo\SimpleCache\Providers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Cache\Drivers\Memcached as MemcachedDriver; |
4
|
|
|
use \Comodojo\Foundation\Validation\DataValidation; |
5
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
6
|
|
|
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats; |
7
|
|
|
use \Psr\Log\LoggerInterface; |
8
|
|
|
use \Comodojo\Exception\SimpleCacheException; |
9
|
|
|
use \Comodojo\Exception\InvalidSimpleCacheArgumentException; |
10
|
|
|
use \Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package Comodojo Cache |
14
|
|
|
* @author Marco Giovinazzi <[email protected]> |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* LICENSE: |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25
|
|
|
* THE SOFTWARE. |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
class Memcached extends AbstractEnhancedProvider { |
29
|
|
|
|
30
|
|
|
protected $default_properties = [ |
31
|
|
|
"server" => '127.0.0.1', |
32
|
|
|
"port" => 11211, |
33
|
|
|
"weight" => 0, |
34
|
|
|
"persistent_id" => null, |
35
|
|
|
"username" => null, |
36
|
|
|
"password" => null |
37
|
|
|
]; |
38
|
|
|
|
39
|
19 |
|
public function __construct(array $properties = [], LoggerInterface $logger = null) { |
40
|
|
|
|
41
|
19 |
|
parent::__construct($properties, $logger); |
42
|
|
|
|
43
|
19 |
|
$properties = $this->getProperties(); |
44
|
|
|
|
45
|
19 |
|
if ( empty($properties->server) ) { |
46
|
|
|
throw new InvalidSimpleCacheArgumentException("Invalid or unspecified memcached server"); |
47
|
|
|
} |
48
|
|
|
|
49
|
19 |
|
if ( $properties->persistent_id !== null && DataValidation::validateString($properties->persistent_id) === false ) { |
50
|
|
|
throw new InvalidSimpleCacheArgumentException("Invalid persistent id"); |
51
|
|
|
} |
52
|
|
|
|
53
|
19 |
|
$port = DataFilter::filterPort($properties->port, 11211); |
|
|
|
|
54
|
19 |
|
$weight = DataFilter::filterInteger($properties->weight); |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
|
58
|
19 |
|
$this->driver = new MemcachedDriver([ |
59
|
19 |
|
'persistent_id' => $properties->persistent_id, |
60
|
19 |
|
'server' => $properties->server, |
61
|
19 |
|
'port' => $port, |
62
|
19 |
|
'weight' => $weight, |
63
|
19 |
|
"username" => $properties->username, |
64
|
19 |
|
"password" => $properties->password |
65
|
|
|
]); |
66
|
|
|
|
67
|
19 |
|
$this->test(); |
68
|
|
|
|
69
|
|
|
} catch (Exception $e) { |
70
|
|
|
|
71
|
|
|
throw new SimpleCacheException($e->getMessage()); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
19 |
|
} |
76
|
|
|
|
77
|
|
|
public function getInstance() { |
78
|
|
|
return $this->driver->getInstance(); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function getStats() { |
82
|
|
|
|
83
|
1 |
|
$info = $this->driver->stats(); |
84
|
|
|
|
85
|
1 |
|
$objects = 0; |
86
|
|
|
|
87
|
1 |
|
foreach ( $info as $key => $value ) { |
88
|
|
|
|
89
|
1 |
|
$objects = max($objects, $value['curr_items']); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return new EnhancedCacheItemPoolStats( |
94
|
1 |
|
$this->getId(), |
95
|
1 |
|
$this->driver->getName(), |
96
|
1 |
|
$this->getState(), |
97
|
1 |
|
$objects, |
98
|
1 |
|
$info |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|