1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Drivers\Predis; |
16
|
|
|
|
17
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
18
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
19
|
|
|
use phpFastCache\Entities\DriverStatistic; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
23
|
|
|
use Predis\Client as PredisClient; |
24
|
|
|
use Psr\Cache\CacheItemInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Driver |
28
|
|
|
* @package phpFastCache\Drivers |
29
|
|
|
* @property PredisClient $instance Instance of driver service |
30
|
|
|
*/ |
31
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
32
|
|
|
{ |
33
|
|
|
use DriverBaseTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Driver constructor. |
37
|
|
|
* @param array $config |
38
|
|
|
* @throws phpFastCacheDriverException |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function __construct(array $config = []) |
41
|
|
|
{ |
42
|
|
|
$this->setup($config); |
43
|
|
|
|
44
|
|
|
if (!$this->driverCheck()) { |
45
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
46
|
|
|
} else { |
47
|
|
|
$this->driverConnect(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function driverCheck() |
55
|
|
|
{ |
56
|
|
|
if (extension_loaded('Redis')) { |
57
|
|
|
trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return class_exists('Predis\Client'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
65
|
|
|
* @return mixed |
66
|
|
|
* @throws phpFastCacheInvalidArgumentException |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
protected function driverWrite(CacheItemInterface $item) |
69
|
|
|
{ |
70
|
|
|
/** |
71
|
|
|
* Check for Cross-Driver type confusion |
72
|
|
|
*/ |
73
|
|
|
if ($item instanceof Item) { |
74
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
75
|
|
|
|
76
|
|
|
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
77
|
|
|
} else { |
78
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
87
|
|
|
{ |
88
|
|
|
$val = $this->instance->get($item->getKey()); |
89
|
|
|
if ($val == false) { |
90
|
|
|
return null; |
91
|
|
|
} else { |
92
|
|
|
return $this->decode($val); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
98
|
|
|
* @return bool |
99
|
|
|
* @throws phpFastCacheInvalidArgumentException |
100
|
|
|
*/ |
101
|
|
|
protected function driverDelete(CacheItemInterface $item) |
102
|
|
|
{ |
103
|
|
|
/** |
104
|
|
|
* Check for Cross-Driver type confusion |
105
|
|
|
*/ |
106
|
|
|
if ($item instanceof Item) { |
107
|
|
|
return $this->instance->del($item->getKey()); |
108
|
|
|
} else { |
109
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function driverClear() |
117
|
|
|
{ |
118
|
|
|
return $this->instance->flushdb(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function driverConnect() |
125
|
|
|
{ |
126
|
|
|
$config = isset($this->config[ 'predis' ]) ? $this->config[ 'predis' ] : []; |
127
|
|
|
|
128
|
|
|
$this->instance = new PredisClient(array_merge([ |
129
|
|
|
'host' => '127.0.0.1', |
130
|
|
|
'port' => 6379, |
131
|
|
|
'password' => null, |
132
|
|
|
'database' => null, |
133
|
|
|
], $config)); |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/******************** |
139
|
|
|
* |
140
|
|
|
* PSR-6 Extended Methods |
141
|
|
|
* |
142
|
|
|
*******************/ |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getHelp() |
149
|
|
|
{ |
150
|
|
|
return <<<HELP |
151
|
|
|
<p> |
152
|
|
|
To install the Predis library via Composer: |
153
|
|
|
<code>composer require "predis/predis" "~1.1.0"</code> |
154
|
|
|
</p> |
155
|
|
|
HELP; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return DriverStatistic |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function getStats() |
162
|
|
|
{ |
163
|
|
|
$info = $this->instance->info(); |
164
|
|
|
$size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0); |
165
|
|
|
$version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0); |
166
|
|
|
$date = (isset($info['Server'][ 'uptime_in_seconds' ]) ? (new \DateTime())->setTimestamp(time() - $info['Server'][ 'uptime_in_seconds' ]) : 'unknown date'); |
167
|
|
|
|
168
|
|
|
return (new DriverStatistic()) |
169
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
170
|
|
|
->setRawData($info) |
171
|
|
|
->setSize($size) |
172
|
|
|
->setInfo(sprintf("The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", $version, $date->format(DATE_RFC2822))); |
173
|
|
|
} |
174
|
|
|
} |