Completed
Pull Request — master (#374)
by Anton
08:00
created

Memcached::doGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Cache\Adapter;
13
14
use Bluz\Cache\Cache;
15
use Bluz\Common\Exception\ComponentException;
16
use Bluz\Common\Exception\ConfigurationException;
17
18
/**
19
 * Memcached cache adapter
20
 *
21
 * @package Bluz\Cache\Adapter
22
 * @see     \Memcached
23
 */
24
class Memcached extends AbstractAdapter
25
{
26
    /**
27
     * @var \Memcached instance of memcached
28
     */
29
    protected $handler = null;
30
31
    /**
32
     * Check and setup memcached servers
33
     *
34
     * @param  array $settings
35
     * @throws ComponentException
36
     * @throws ConfigurationException
37
     */
38
    public function __construct($settings = [])
39
    {
40
        // check Memcached extension
41
        if (!extension_loaded('memcached')) {
42
            throw new ComponentException(
43
                "Memcached extension not installed/enabled.
44
                Install and/or enable memcached extension. See phpinfo() for more information"
45
            );
46
        }
47
48
        // check Memcached settings
49
        if (!is_array($settings) || empty($settings) || !isset($settings['servers'])) {
50
            throw new ConfigurationException(
51
                "Memcached configuration is missed. Please check 'cache' configuration section"
52
            );
53
        }
54
55
        parent::__construct($settings);
56
    }
57
58
    /**
59
     * Get Mamcached Handler
60
     *
61
     * @return \Memcached
62
     */
63
    public function getHandler()
64
    {
65
        if (!$this->handler) {
66
            $persistentId = $this->settings['persistent'] ?? null;
67
68
            $this->handler = new \Memcached($persistentId);
69
70
            if (!$this->handler->getServerList()) {
71
                $this->handler->addServers($this->settings['servers']);
72
            }
73
74
            if (isset($this->settings['options'])) {
75
                $this->handler->setOptions($this->settings['options']);
76
            }
77
        }
78
        return $this->handler;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @param  string $id
85
     * @return mixed
86
     */
87
    protected function doGet($id)
88
    {
89
        return $this->getHandler()->get($id);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     * @param  string  $id
96
     * @param  mixed   $data
97
     * @param  integer $ttl
98
     * @return bool
99
     */
100
    protected function doAdd($id, $data, $ttl = Cache::TTL_NO_EXPIRY)
101
    {
102
        return $this->getHandler()->add($id, $data, $ttl);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @param  string  $id
109
     * @param  mixed   $data
110
     * @param  integer $ttl
111
     * @return bool
112
     */
113
    protected function doSet($id, $data, $ttl = Cache::TTL_NO_EXPIRY)
114
    {
115
        return $this->getHandler()->set($id, $data, $ttl);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121
     * @param  string $id
122
     * @return bool
123
     */
124
    protected function doContains($id)
125
    {
126
        $this->getHandler()->get($id);
127
        return $this->getHandler()->getResultCode() !== \Memcached::RES_NOTFOUND;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     *
133
     * @param  string $id
134
     * @return bool
135
     */
136
    protected function doDelete($id)
137
    {
138
        return $this->getHandler()->delete($id);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     *
144
     * @return bool
145
     */
146
    protected function doFlush()
147
    {
148
        return $this->getHandler()->flush();
149
    }
150
}
151