|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sitetheory\Bundle\ProfilerStorageBundle\Profiler; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Memcache Profiler Storage. |
|
16
|
|
|
* |
|
17
|
|
|
* Class MemcacheProfilerStorage |
|
18
|
|
|
* |
|
19
|
|
|
* @author Andrej Hudec <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class MemcacheProfilerStorage extends BaseMemcacheProfilerStorage |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Memcache |
|
25
|
|
|
*/ |
|
26
|
|
|
private $memcache; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Internal convenience method that returns the instance of the Memcache. |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \RuntimeException |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Memcache |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getMemcache() |
|
36
|
|
|
{ |
|
37
|
|
|
if (null === $this->memcache) { |
|
38
|
|
|
if (!preg_match('#^memcache://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) { |
|
39
|
|
|
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcache with an invalid dsn "%s". The expected format is "memcache://[host]:port".', $this->dsn)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$host = $matches[1] ?: $matches[2]; |
|
43
|
|
|
$port = $matches[3]; |
|
44
|
|
|
|
|
45
|
|
|
$memcache = new \Memcache(); |
|
46
|
|
|
$memcache->addServer($host, $port); |
|
47
|
|
|
|
|
48
|
|
|
$this->memcache = $memcache; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->memcache; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set instance of the Memcache. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Memcache $memcache |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setMemcache($memcache) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->memcache = $memcache; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getValue($key) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->getMemcache()->get($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function setValue($key, $value, $expiration = 0) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->getMemcache()->set($key, $value, false, time() + $expiration); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function delete($key) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getMemcache()->delete($key); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function appendValue($key, $value, $expiration = 0) |
|
92
|
|
|
{ |
|
93
|
|
|
$memcache = $this->getMemcache(); |
|
94
|
|
|
|
|
95
|
|
|
if (method_exists($memcache, 'append')) { |
|
96
|
|
|
// Memcache v3.0 |
|
97
|
|
|
if (!$result = $memcache->append($key, $value, false, $expiration)) { |
|
|
|
|
|
|
98
|
|
|
return $memcache->set($key, $value, false, $expiration); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// simulate append in Memcache <3.0 |
|
105
|
|
|
$content = $memcache->get($key); |
|
106
|
|
|
|
|
107
|
|
|
return $memcache->set($key, $content.$value, false, $expiration); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|