Completed
Push — master ( 663fca...c5e23c )
by Arman
17s queued 14s
created

MemcachedAdapter::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Libraries\Cache\Adapters;
16
17
use Quantum\Exceptions\CacheException;
18
use Psr\SimpleCache\CacheInterface;
19
use InvalidArgumentException;
20
use Memcached;
21
use Exception;
22
23
/**
24
 * Class MemcachedAdapter
25
 * @package Quantum\Libraries\Cache\Adapters
26
 */
27
class MemcachedAdapter implements CacheInterface
28
{
29
30
    /**
31
     * @var int
32
     */
33
    private $ttl = 30;
34
35
    /**
36
     * @var \Memcached
37
     */
38
    private $memcached;
39
40
    /**
41
     * MemcachedAdapter constructor
42
     * @param array $params
43
     */
44
    public function __construct(array $params)
45
    {
46
        $this->ttl = $params['ttl'];
47
48
        $this->memcached = new Memcached();
49
        $this->memcached->addServer($params['host'], $params['port']);
50
51
        if (!$this->memcached->getStats()) {
52
            throw CacheException::cantConnect('Memcached server');
53
        }
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function get($key, $default = null)
60
    {
61
        if ($this->has($key)) {
62
            $cacheItem = $this->memcached->get(sha1($key));
63
64
            try {
65
                return unserialize($cacheItem);
66
            } catch (Exception $e) {
67
                $this->delete($key);
68
                return $default;
69
            }
70
        }
71
72
        return $default;
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function getMultiple($keys, $default = null)
79
    {
80
        if (!is_array($keys)) {
81
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
82
        }
83
84
        $result = [];
85
86
        foreach ($keys as $key) {
87
            $result[$key] = $this->get($key, $default);
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function has($key): bool
97
    {
98
        $cacheItem = $this->memcached->get(sha1($key));
99
100
        if (!$cacheItem) {
101
            return false;
102
        }
103
104
        return true;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function set($key, $value, $ttl = null)
111
    {
112
        return $this->memcached->set(sha1($key), serialize($value), $this->ttl);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     * @throws InvalidArgumentException
118
     */
119
    public function setMultiple($values, $ttl = null)
120
    {
121
        if (!is_array($values)) {
122
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
123
        }
124
125
        $results = [];
126
127
        foreach ($values as $key => $value) {
128
            $results[] = $this->set($key, $value, $ttl);
129
        }
130
131
        return !in_array(false, $results, true);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function delete($key)
138
    {
139
        return $this->memcached->delete(sha1($key));
140
    }
141
142
    /**
143
     * @inheritDoc
144
     * @throws InvalidArgumentException
145
     */
146
    public function deleteMultiple($keys)
147
    {
148
        if (!is_array($keys)) {
149
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
150
        }
151
152
        $results = [];
153
154
        foreach ($keys as $key) {
155
            $results[] = $this->delete($key);
156
        }
157
158
        return !in_array(false, $results, true);
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function clear()
165
    {
166
        return $this->memcached->flush();
167
    }
168
169
}
170