1
|
|
|
<?php namespace Comodojo\Cache\Providers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Cache\Drivers\PhpRedis as PhpRedisDriver; |
4
|
|
|
use \Comodojo\Cache\Item; |
5
|
|
|
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats; |
6
|
|
|
use \Comodojo\Foundation\Validation\DataValidation; |
7
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
8
|
|
|
use \Psr\Log\LoggerInterface; |
9
|
|
|
use \Comodojo\Exception\CacheException; |
10
|
|
|
use \Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Memcached provider |
14
|
|
|
* |
15
|
|
|
* @package Comodojo Spare Parts |
16
|
|
|
* @author Marco Giovinazzi <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* LICENSE: |
20
|
|
|
* |
21
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
24
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
27
|
|
|
* THE SOFTWARE. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
View Code Duplication |
class PhpRedis extends AbstractEnhancedProvider { |
|
|
|
|
31
|
|
|
|
32
|
28 |
|
public function __construct( |
33
|
|
|
$server = '127.0.0.1', |
34
|
|
|
$port = 6379, |
35
|
|
|
$timeout = 0, |
36
|
|
|
LoggerInterface $logger = null |
37
|
|
|
) { |
38
|
|
|
|
39
|
28 |
|
if ( empty($server) ) { |
40
|
|
|
throw new InvalidCacheArgumentException("Invalid or unspecified memcached server"); |
41
|
|
|
} |
42
|
|
|
|
43
|
28 |
|
$port = DataFilter::filterPort($port, 6379); |
44
|
28 |
|
$timeout = DataFilter::filterInteger($timeout, 0, PHP_INT_MAX, 0); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
|
48
|
28 |
|
$this->driver = new PhpRedisDriver([ |
49
|
28 |
|
'server' => $server, |
50
|
28 |
|
'port' => $port, |
51
|
28 |
|
'timeout' => $timeout |
52
|
|
|
]); |
53
|
|
|
|
54
|
28 |
|
parent::__construct($logger); |
55
|
|
|
|
56
|
|
|
} catch (Exception $e) { |
57
|
|
|
|
58
|
|
|
throw new CacheException($e->getMessage()); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
28 |
|
} |
63
|
|
|
|
64
|
|
|
public function getInstance() { |
65
|
|
|
return $this->driver->getInstance(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function getStats() { |
69
|
|
|
|
70
|
1 |
|
$info = $this->driver->stats(); |
71
|
|
|
|
72
|
1 |
|
return new EnhancedCacheItemPoolStats( |
73
|
1 |
|
$this->getId(), |
74
|
1 |
|
$this->driver->getName(), |
75
|
1 |
|
$this->getState(), |
76
|
1 |
|
$info['objects'], |
77
|
1 |
|
$info['stats'] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.