Passed
Pull Request — master (#30)
by Alexander
02:06
created

Memcached::getMemcached()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 9.3554

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 11
cp 0.6364
rs 8.8333
cc 7
nc 10
nop 0
crap 9.3554
1
<?php
2
3
namespace Yiisoft\Cache;
4
5
final class Memcached extends BaseCache
6
{
7
    /**
8
     * @var \Memcached
9
     */
10
    private $cache;
11
12
    public function __construct()
13
    {
14
        $this->initCache();
15
        parent::__construct();
16
    }
17
18
    /**
19
     * Fetches a value from the cache.
20
     *
21
     * @param string $key The unique key of this item in the cache.
22
     * @param mixed $default Default value to return if the key does not exist.
23
     *
24
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
25
     *
26
     * @throws \Psr\SimpleCache\InvalidArgumentException
27
     *   MUST be thrown if the $key string is not a legal value.
28
     */
29
    public function get($key, $default = null)
30
    {
31
        $value = $this->cache->get($key);
32
33
        // TODO store error message
34
        if ($this->cache->getResultCode() === \Memcached::RES_SUCCESS) {
35
            return $value;
36
        }
37
38
        return $default;
39
    }
40
41
    /**
42
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
43
     *
44
     * @param string $key The key of the item to store.
45
     * @param mixed $value The value of the item to store, must be serializable.
46
     * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
47
     *                                      the driver supports TTL then the library may set a default value
48
     *                                      for it or let the driver take care of that.
49
     *
50
     * @return bool True on success and false on failure.
51
     *
52
     * @throws \Psr\SimpleCache\InvalidArgumentException
53
     *   MUST be thrown if the $key string is not a legal value.
54
     */
55
    public function set($key, $value, $ttl = null)
56
    {
57
        $expiration = $this->ttlToExpiration($ttl);
58
        if ($expiration < 0) {
59
            return $this->delete($key);
60
        }
61
        return $this->cache->set($key, $value, $expiration);
62
    }
63
64
    /**
65
     * Delete an item from the cache by its unique key.
66 17
     *
67
     * @param string $key The unique cache key of the item to delete.
68 17
     *
69
     * @return bool True if the item was successfully removed. False if there was an error.
70 17
     *
71 17
     * @throws \Psr\SimpleCache\InvalidArgumentException
72
     *   MUST be thrown if the $key string is not a legal value.
73
     */
74 17
    public function delete($key)
75
    {
76 17
        return $this->cache->delete($key);
77
    }
78
79
    /**
80
     * Wipes clean the entire cache's keys.
81
     *
82
     * @return bool True on success and false on failure.
83
     */
84
    public function clear()
85 17
    {
86
        return $this->cache->flush();
87 17
    }
88 17
89
    /**
90
     * Obtains multiple cache items by their unique keys.
91
     *
92
     * @param iterable $keys A list of keys that can obtained in a single operation.
93 17
     * @param mixed $default Default value to return for keys that do not exist.
94 17
     *
95 17
     * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
96 17
     *
97
     * @throws \Psr\SimpleCache\InvalidArgumentException
98
     *   MUST be thrown if $keys is neither an array nor a Traversable,
99
     *   or if any of the $keys are not a legal value.
100
     */
101
    public function getMultiple($keys, $default = null)
102
    {
103
        $values = $this->cache->getMulti((array)$keys);
104
        return array_merge(array_fill_keys((array)$keys, $default), $values);
105
    }
106 17
107
    /**
108 17
     * Persists a set of key => value pairs in the cache, with an optional TTL.
109 17
     *
110
     * @param iterable $values A list of key => value pairs for a multiple-set operation.
111
     * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
112
     *                                       the driver supports TTL then the library may set a default value
113 17
     *                                       for it or let the driver take care of that.
114 17
     *
115
     * @return bool True on success and false on failure.
116
     *
117
     * @throws \Psr\SimpleCache\InvalidArgumentException
118 17
     *   MUST be thrown if $values is neither an array nor a Traversable,
119
     *   or if any of the $values are not a legal value.
120
     */
121
    public function setMultiple($values, $ttl = null)
122
    {
123 17
        $expiration = $this->ttlToExpiration($ttl);
124
        return $this->cache->setMulti((array)$values, $expiration);
125
    }
126
127
    /**
128
     * Deletes multiple cache items in a single operation.
129
     *
130
     * @param iterable $keys A list of string-based keys to be deleted.
131
     *
132
     * @return bool True if the items were successfully removed. False if there was an error.
133
     *
134
     * @throws \Psr\SimpleCache\InvalidArgumentException
135
     *   MUST be thrown if $keys is neither an array nor a Traversable,
136
     *   or if any of the $keys are not a legal value.
137
     */
138
    public function deleteMultiple($keys)
139
    {
140
        foreach ($this->cache->deleteMulti((array)$keys) as $result) {
141
            if ($result === false) {
142
                return false;
143
            }
144
        }
145
        return true;
146
    }
147
148
    /**
149
     * Determines whether an item is present in the cache.
150
     *
151
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
152
     * and not to be used within your live applications operations for get/set, as this method
153
     * is subject to a race condition where your has() will return true and immediately after,
154
     * another script can remove it making the state of your app out of date.
155
     *
156
     * @param string $key The cache item key.
157
     *
158
     * @return bool
159
     *
160
     * @throws \Psr\SimpleCache\InvalidArgumentException
161
     *   MUST be thrown if the $key string is not a legal value.
162
     */
163
    public function has($key)
164
    {
165
        $this->cache->get($key);
166
        return $this->cache->getResultCode() === \Memcached::RES_SUCCESS;
167
    }
168
169
    private function initCache()
170
    {
171
        $this->cache = new \Memcached();
172
    }
173
174
    /**
175
     * @param MemcachedServer $server
176
     */
177
    public function addServer(MemcachedServer $server)
178
    {
179
        $this->cache->addServer($server->getHost(), $server->getPort(), $server->getWeight());
180
    }
181
}
182