MemcachedProfilerStorage::getMemcached()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 0
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
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
 * Memcached Profiler Storage.
16
 *
17
 * Class MemcachedProfilerStorage
18
 *
19
 * @author Andrej Hudec <[email protected]>
20
 */
21
class MemcachedProfilerStorage extends BaseMemcacheProfilerStorage
22
{
23
    /**
24
     * @var \Memcached
25
     */
26
    private $memcached;
27
28
    /**
29
     * Internal convenience method that returns the instance of the Memcached.
30
     *
31
     * @throws \RuntimeException
32
     *
33
     * @return \Memcached
34
     */
35
    protected function getMemcached()
36
    {
37
        if (null === $this->memcached) {
38
            if (!preg_match('#^memcached://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
39
                throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcached with an invalid dsn "%s". The expected format is "memcached://[host]:port".', $this->dsn));
40
            }
41
42
            $host = $matches[1] ?: $matches[2];
43
            $port = $matches[3];
44
45
            $memcached = new \Memcached();
46
47
            // disable compression to allow appending
48
            $memcached->setOption(\Memcached::OPT_COMPRESSION, false);
49
50
            $memcached->addServer($host, $port);
51
52
            $this->memcached = $memcached;
53
        }
54
55
        return $this->memcached;
56
    }
57
58
    /**
59
     * Set instance of the Memcached.
60
     *
61
     * @param \Memcached $memcached
62
     */
63
    public function setMemcached($memcached)
64
    {
65
        $this->memcached = $memcached;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getValue($key)
72
    {
73
        return $this->getMemcached()->get($key);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function setValue($key, $value, $expiration = 0)
80
    {
81
        return $this->getMemcached()->set($key, $value, time() + $expiration);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function delete($key)
88
    {
89
        return $this->getMemcached()->delete($key);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function appendValue($key, $value, $expiration = 0)
96
    {
97
        $memcached = $this->getMemcached();
98
99
        if (!$result = $memcached->append($key, $value)) {
100
            return $memcached->set($key, $value, $expiration);
101
        }
102
103
        return $result;
104
    }
105
}
106