Completed
Push — b0.27.0 ( c529b3...34cc26 )
by Sebastian
05:07
created

MemcachedCache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 14
c 0
b 0
f 0
dl 0
loc 99
ccs 18
cts 18
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 2
A clear() 0 3 1
A delete() 0 3 1
A __construct() 0 7 2
A get() 0 11 2
A set() 0 3 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Cache;
13
14
use Memcached;
15
use InvalidArgumentException;
16
use Psr\SimpleCache\CacheInterface;
17
18
/**
19
 * PSR-16 Memcached.
20
 */
21
class MemcachedCache implements CacheInterface
22
{
23
    use ActionMultipleTrait;
24
25
    /**
26
     * @var Memcached Memcached instance
27
     */
28
    private Memcached $memcached;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param array<mixed> $options
34
     *
35
     * @throws InvalidArgumentException if options not contain memcached resource
36
     */
37 6
    public function __construct(array $options)
38
    {
39 6
        if (!($options['resource'] instanceof Memcached)) {
40 5
            throw new InvalidArgumentException('MemcachedCache class need instance of Memcached passed as option. [\'resource\' => $memcached].');
41
        }
42
43 1
        $this->memcached = $options['resource'];
44 1
    }
45
46
    /**
47
     * Fetches a value from the cache.
48
     *
49
     * @param string $key     The unique key of this item in the cache.
50
     * @param mixed  $default Default value to return if the key does not exist.
51
     *
52
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
53
     */
54 7
    public function get(string $key, $default = null)
55
    {
56
        //get value from memcached
57 7
        $value = $this->memcached->get($key);
58
59
        //check if value was retrived
60 7
        if ($value === false) {
61 4
            return $default;
62
        }
63
64 3
        return $value;
65
    }
66
67
    /**
68
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
69
     *
70
     * @param string $key   The key of the item to store.
71
     * @param mixed  $value The value of the item to store, must be serializable.
72
     * @param int    $ttl   Optional. The TTL (time to live) value in seconds of this item.
73
     *                      If no value is sent and the driver supports TTL then the
74
     *                      library may set a default value for it or let the driver take care of that.
75
     *
76
     * @return bool True on success and false on failure.
77
     */
78 13
    public function set(string $key, $value, int $ttl = 0): bool
79
    {
80 13
        return $this->memcached->set($key, $value, $ttl);
81
    }
82
83
    /**
84
     * Delete an item from the cache by its unique key.
85
     *
86
     * @param string $key The unique cache key of the item to delete.
87
     *
88
     * @return bool True if the item was successfully removed. False if there was an error.
89
     */
90 3
    public function delete(string $key): bool
91
    {
92 3
        return $this->memcached->delete($key);
93
    }
94
95
    /**
96
     * Wipes clean the entire cache's keys.
97
     *
98
     * @return bool True on success and false on failure.
99
     */
100 56
    public function clear(): bool
101
    {
102 56
        return $this->memcached->flush();
103
    }
104
105
    /**
106
     * Determines whether an item is present in the cache.
107
     *
108
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
109
     * and not to be used within your live applications operations for get/set, as this method
110
     * is subject to a race condition where your has() will return true and immediately after,
111
     * another script can remove it making the state of your app out of date.
112
     *
113
     * @param string $key The cache item key.
114
     *
115
     * @return bool
116
     */
117 10
    public function has(string $key): bool
118
    {
119 10
        return ($this->memcached->get($key) !== false) ? true : false;
120
    }
121
}
122