Issues (15)

src/SimpleCache/Driver/Memcache.php (1 issue)

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 Memcache as MemcacheServer;
18
use Exception;
19
use function array_keys;
20
21
/**
22
 * A cache driver class provided by Memcache
23
 *
24
 * @see https://www.php.net/manual/en/book.memcache.php
25
 */
26
class Memcache extends CacheProvider
27
{
28
    protected $type = 'memcache';
29
30
    /**
31
     * The Memcache instance.
32
     *
33
     * @var \Memcache|null
34
     */
35
    protected $memcache = 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('memcache')) {
75
            try {
76 10
                $this->memcache = new MemcacheServer();
77
78 10
                if (!empty($config['unix_socket'])) {
79
                    // @codeCoverageIgnoreStart
80
                    $this->memcache->addServer(
81
                        'unix://' . $config['unix_socket'],
82
                        0
83
                    );
84
                    // @codeCoverageIgnoreEnd
85
                } else {
86 10
                    $this->memcache->addServer(
87 10
                        $config['host'],
88 10
                        $config['port'],
89 10
                        true,
90 10
                        1
91
                    );
92
                }
93
94
            // @codeCoverageIgnoreStart
95
            } catch (Exception $e) {
96
                throw new CacheException($e->getMessage());
97
            }
98
            // @codeCoverageIgnoreEnd
99 10
            return;
100
        }
101
102
        // @codeCoverageIgnoreStart
103
        throw new CacheException(
104
            'PHP Memcache extension is not installed on your system.'
105
        );
106
        // @codeCoverageIgnoreEnd
107
    }
108
109
    /**
110
     * Fetch a cache by an extended Cache Driver.
111
     *
112
     * @param string $key The key of a cache.
113
     *
114
     * @return array
115
     */
116 8
    protected function doGet(string $key): array
117
    {
118 8
        $content = $this->memcache->get($key);
119
120 8
        if (empty($content)) {
121 8
            return [];
122
        }
123 8
        $data = $content;
124
125 8
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
128
    /**
129
     * Set a cache by an extended Cache Driver.
130
     *
131
     * @param string $key       The key of a cache.
132
     * @param mixed  $value     The value of a cache. (serialized)
133
     * @param int    $ttl       The time to live for a cache.
134
     * @param int    $timestamp The time to store a cache.
135
     *
136
     * @return bool
137
     */
138 8
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
139
    {
140
        $contents = [
141 8
            'timestamp' => $timestamp,
142 8
            'ttl'       => $ttl,
143 8
            'value'     => $value,
144
        ];
145
146 8
        $result = $this->memcache->set(
147 8
            $key,
148 8
            $contents,
149 8
            0,
150 8
            $ttl
151
        );
152
153 8
        return $result;
154
    }
155
156
    /**
157
     * Delete a cache by an extended Cache Driver.
158
     *
159
     * @param string $key The key of a cache.
160
     *
161
     * @return bool
162
     */
163 2
    protected function doDelete(string $key): bool
164
    {
165 2
        return $this->memcache->delete($key);
166
    }
167
168
    /**
169
     * Delete all caches by an extended Cache Driver.
170
     *
171
     * @return bool
172
     */
173 4
    protected function doClear(): bool
174
    {
175 4
        return $this->memcache->flush();
176
    }
177
178
    /**
179
     * Check if the cache exists or not.
180
     *
181
     * @param string $key The key of a cache.
182
     *
183
     * @return bool
184
     */
185 4
    protected function doHas(string $key): bool
186
    {
187 4
        $content = $this->memcache->get($key);
188
189 4
        if (empty($content)) {
190 4
            return false;
191
        }
192 4
        return true;
193
    }
194
}
195