|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Predis\ClientInterface; |
|
6
|
|
|
use function array_combine; |
|
7
|
|
|
use function array_filter; |
|
8
|
|
|
use function array_map; |
|
9
|
|
|
use function call_user_func_array; |
|
10
|
|
|
use function serialize; |
|
11
|
|
|
use function unserialize; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Predis cache provider. |
|
15
|
|
|
*/ |
|
16
|
|
|
class PredisCache extends CacheProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ClientInterface */ |
|
19
|
|
|
private $client; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ClientInterface $client) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->client = $client; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function doFetch($id) |
|
30
|
|
|
{ |
|
31
|
|
|
$result = $this->client->get($id); |
|
32
|
|
|
if ($result === null) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return unserialize($result); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
78 |
|
*/ |
|
42
|
|
|
protected function doFetchMultiple(array $keys) |
|
43
|
78 |
|
{ |
|
44
|
78 |
|
$fetchedItems = call_user_func_array([$this->client, 'mget'], $keys); |
|
45
|
|
|
|
|
46
|
|
|
return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems))); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
74 |
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
74 |
|
*/ |
|
52
|
74 |
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
|
53
|
64 |
|
{ |
|
54
|
|
|
if ($lifetime) { |
|
55
|
|
|
$success = true; |
|
56
|
69 |
|
|
|
57
|
|
|
// Keys have lifetime, use SETEX for each of them |
|
58
|
|
|
foreach ($keysAndValues as $key => $value) { |
|
59
|
|
|
$response = (string) $this->client->setex($key, $lifetime, serialize($value)); |
|
60
|
|
|
|
|
61
|
|
|
if ($response == 'OK') { |
|
62
|
2 |
|
continue; |
|
63
|
|
|
} |
|
64
|
2 |
|
|
|
65
|
|
|
$success = false; |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $success; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// No lifetime, use MSET |
|
72
|
1 |
|
$response = $this->client->mset(array_map(function ($value) { |
|
73
|
|
|
return serialize($value); |
|
74
|
1 |
|
}, $keysAndValues)); |
|
75
|
|
|
|
|
76
|
|
|
return (string) $response == 'OK'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function doContains($id) |
|
83
|
|
|
{ |
|
84
|
|
|
return (bool) $this->client->exists($id); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
1 |
|
protected function doSave($id, $data, $lifeTime = 0) |
|
91
|
1 |
|
{ |
|
92
|
1 |
|
$data = serialize($data); |
|
93
|
|
|
if ($lifeTime > 0) { |
|
94
|
1 |
|
$response = $this->client->setex($id, $lifeTime, $data); |
|
95
|
|
|
} else { |
|
96
|
|
|
$response = $this->client->set($id, $data); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $response === true || $response == 'OK'; |
|
100
|
69 |
|
} |
|
101
|
|
|
|
|
102
|
69 |
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function doDelete($id) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->client->del($id) >= 0; |
|
108
|
72 |
|
} |
|
109
|
|
|
|
|
110
|
72 |
|
/** |
|
111
|
72 |
|
* {@inheritdoc} |
|
112
|
3 |
|
*/ |
|
113
|
|
|
protected function doDeleteMultiple(array $keys) |
|
114
|
70 |
|
{ |
|
115
|
|
|
return $this->client->del($keys) >= 0; |
|
116
|
|
|
} |
|
117
|
72 |
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function doFlush() |
|
122
|
|
|
{ |
|
123
|
43 |
|
$response = $this->client->flushdb(); |
|
124
|
|
|
|
|
125
|
43 |
|
return $response === true || $response == 'OK'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
1 |
|
protected function doGetStats() |
|
132
|
|
|
{ |
|
133
|
1 |
|
$info = $this->client->info(); |
|
134
|
|
|
|
|
135
|
|
|
return [ |
|
136
|
|
|
Cache::STATS_HITS => $info['Stats']['keyspace_hits'], |
|
137
|
|
|
Cache::STATS_MISSES => $info['Stats']['keyspace_misses'], |
|
138
|
|
|
Cache::STATS_UPTIME => $info['Server']['uptime_in_seconds'], |
|
139
|
2 |
|
Cache::STATS_MEMORY_USAGE => $info['Memory']['used_memory'], |
|
140
|
|
|
Cache::STATS_MEMORY_AVAILABLE => false, |
|
141
|
2 |
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|