Memcached   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 90
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connection() 0 7 2
A read() 0 11 2
A write() 0 8 2
A delete() 0 8 2
A purge() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2014-2019 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelSmarty\Cache;
21
22
use Memcached as MemcachedExtension;
23
24
/**
25
 * Class Memcached
26
 *
27
 * @author yuuki.takezawa<[email protected]>
28
 * @license http://opensource.org/licenses/MIT MIT
29
 */
30
class Memcached extends KeyValueStorage
31
{
32
    /** @var MemcachedExtension */
33
    protected $memcached;
34
35
    /**
36
     * @param MemcachedExtension $memcached
37
     */
38
    public function __construct(MemcachedExtension $memcached, array $servers)
39
    {
40
        $this->memcached = $this->connection($memcached, $servers);
41
    }
42
43
    /**
44
     * @param MemcachedExtension $memcached
45
     * @param array              $servers
46
     *
47
     * @return MemcachedExtension
48
     */
49
    protected function connection(MemcachedExtension $memcached, array $servers)
50
    {
51
        foreach ($servers as $server) {
52
            $memcached->addServer($server['host'], $server['port'], $server['weight']);
53
        }
54
        return $memcached;
55
    }
56
57
58
    /**
59
     * Read values for a set of keys from cache
60
     *
61
     * @param  array $keys list of keys to fetch
62
     *
63
     * @return array   list of values with the given keys used as indexes
64
     */
65
    protected function read(array $keys)
66
    {
67
        $map = $lookup = [];
68
        list($map, $lookup) = $this->eachKeys($keys, $map, $lookup);
69
        $result = [];
70
        $memcachedResult = $this->memcached->getMulti($map);
71
        foreach ($memcachedResult as $k => $v) {
72
            $result[$lookup[$k]] = $v;
73
        }
74
        return $result;
75
    }
76
77
    /**
78
     * Save values for a set of keys to cache
79
     *
80
     * @param  array $keys list of values to save
81
     * @param  int   $expire expiration time
82
     *
83
     * @return bool true on success, false on failure
84
     */
85
    protected function write(array $keys, $expire = null)
86
    {
87
        foreach ($keys as $k => $v) {
88
            $k = sha1($k);
89
            $this->memcached->set($k, $v, $expire);
90
        }
91
        return true;
92
    }
93
94
    /**
95
     * Remove values from cache
96
     *
97
     * @param  array $keys list of keys to delete
98
     *
99
     * @return bool true on success, false on failure
100
     */
101
    protected function delete(array $keys)
102
    {
103
        foreach ($keys as $k) {
104
            $k = sha1($k);
105
            $this->memcached->delete($k);
106
        }
107
        return true;
108
    }
109
110
    /**
111
     * Remove *all* values from cache
112
     *
113
     * @return bool true on success, false on failure
114
     */
115
    protected function purge()
116
    {
117
        return $this->memcached->flush();
118
    }
119
}
120