Memcached   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 45
c 4
b 0
f 0
dl 0
loc 165
ccs 40
cts 40
cp 1
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A doHas() 0 8 2
A doSet() 0 15 1
A __construct() 0 17 3
A doClear() 0 3 1
A doGet() 0 10 2
A connect() 0 32 4
A doDelete() 0 3 1
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache\Driver;
14
15
use Shieldon\SimpleCache\CacheProvider;
16
use Shieldon\SimpleCache\Exception\CacheException;
17
use Memcached as MemcachedServer;
18
use Exception;
19
use function array_keys;
20
21
/**
22
 * A cache driver class provided by libMemcached
23
 *
24
 * @see https://www.php.net/manual/en/book.memcached.php
25
 */
26
class Memcached extends CacheProvider
27
{
28
    protected $type = 'memcached';
29
30
    /**
31
     * The Memcached instance.
32
     *
33
     * @var \Memcached|null
34
     */
35
    protected $memcached = null;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param array $setting The settings.
41
     *
42
     * @throws CacheException
43
     */
44 10
    public function __construct(array $setting = [])
45
    {
46
        $config = [
47 10
            'host' => '127.0.0.1',
48
            'port' => 11211,
49
50
            // If the UNIX socket is set, host and port will be ignored.
51
            'unix_socket' => '',
52
        ];
53
54 10
        foreach (array_keys($config) as $key) {
55 10
            if (isset($setting[$key])) {
56 10
                $config[$key] = $setting[$key];
57
            }
58
        }
59
60 10
        $this->connect($config);
61 10
    }
62
63
    /**
64
     * Connect to Memchaced server.
65
     *
66
     * @param array $config The settings.
67
     *
68
     * @return void
69
     *
70
     * @throws CacheException
71
     */
72 10
    protected function connect(array $config): void
73
    {
74 10
        if (extension_loaded('memcached')) {
75
            try {
76 10
                $this->memcached = new MemcachedServer();
77
78 10
                if (!empty($config['unix_socket'])) {
79
                    // @codeCoverageIgnoreStart
80
                    $this->memcached->addServer(
81
                        $config['unix_socket'],
82
                        0
83
                    );
84
                    // @codeCoverageIgnoreEnd
85
                } else {
86 10
                    $this->memcached->addServer(
87 10
                        $config['host'],
88 10
                        $config['port'],
89 10
                        1
90
                    );
91
                }
92
93
            // @codeCoverageIgnoreStart
94
            } catch (Exception $e) {
95
                throw new CacheException($e->getMessage());
96
            }
97
            // @codeCoverageIgnoreEnd
98 10
            return;
99
        }
100
101
        // @codeCoverageIgnoreStart
102
        throw new CacheException(
103
            'PHP Memcached extension is not installed on your system.'
104
        );
105
        // @codeCoverageIgnoreEnd
106
    }
107
108
    /**
109
     * Fetch a cache by an extended Cache Driver.
110
     *
111
     * @param string $key The key of a cache.
112
     *
113
     * @return array
114
     */
115 8
    protected function doGet(string $key): array
116
    {
117 8
        $content = $this->memcached->get($key);
118
119 8
        if (empty($content)) {
120 8
            return [];
121
        }
122 8
        $data = $content;
123
124 8
        return $data;
125
    }
126
127
    /**
128
     * Set a cache by an extended Cache Driver.
129
     *
130
     * @param string $key       The key of a cache.
131
     * @param mixed  $value     The value of a cache. (serialized)
132
     * @param int    $ttl       The time to live for a cache.
133
     * @param int    $timestamp The time to store a cache.
134
     *
135
     * @return bool
136
     */
137 8
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
138
    {
139
        $contents = [
140 8
            'timestamp' => $timestamp,
141 8
            'ttl'       => $ttl,
142 8
            'value'     => $value,
143
        ];
144
145 8
        $result = $this->memcached->set(
146 8
            $key,
147 8
            $contents,
148 8
            $ttl
149
        );
150
151 8
        return $result;
152
    }
153
154
    /**
155
     * Delete a cache by an extended Cache Driver.
156
     *
157
     * @param string $key The key of a cache.
158
     *
159
     * @return bool
160
     */
161 2
    protected function doDelete(string $key): bool
162
    {
163 2
        return $this->memcached->delete($key);
164
    }
165
166
    /**
167
     * Delete all caches by an extended Cache Driver.
168
     *
169
     * @return bool
170
     */
171 4
    protected function doClear(): bool
172
    {
173 4
        return $this->memcached->flush();
174
    }
175
176
    /**
177
     * Check if the cache exists or not.
178
     *
179
     * @param string $key The key of a cache.
180
     *
181
     * @return bool
182
     */
183 4
    protected function doHas(string $key): bool
184
    {
185 4
        $content = $this->memcached->get($key);
186
187 4
        if (empty($content)) {
188 4
            return false;
189
        }
190 4
        return true;
191
    }
192
}
193