Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

MemcachedDriver::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Cache\Stores\Memcache;
10
11
/**
12
 * Two sisters.
13
 */
14
class MemcachedDriver extends AbstractDriver
15
{
16
    /**
17
     * @var \Memcached
18
     */
19
    protected $driver = null;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function __construct(array $servers)
25
    {
26
        $this->servers = $servers;
27
        $this->driver = new \Memcached();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    public function connect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
34
    {
35
        foreach ($this->servers as $server) {
36
37
            //Merging default options
38
            $server = $server + $this->defaultServer;
39
40
            $this->driver->addServer(
41
                $server['host'],
42
                $server['port'],
43
                $server['weight']
44
            );
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function has(string $name): bool
52
    {
53
        if ($this->driver->get($name) === false) {
54
            return $this->driver->getResultCode() != \Memcached::RES_NOTFOUND;
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get(string $name)
64
    {
65
        return $this->driver->get($name);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws \MemcachedException
72
     */
73
    public function set(string $name, $data, $ttl = null)
74
    {
75
        return $this->driver->set($name, $data, $this->lifetime($ttl));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function delete(string $name)
82
    {
83
        $this->driver->delete($name);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 View Code Duplication
    public function inc(string $name, int $delta = 1): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
90
    {
91
        if (!$this->has($name)) {
92
            $this->set($name, $delta);
93
94
            return $delta;
95
        }
96
97
        return $this->driver->increment($name, $delta);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function dec(string $name, int $delta = 1): int
104
    {
105
        return $this->driver->decrement($name, $delta);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function clear()
112
    {
113
        $this->driver->flush();
114
    }
115
}
116