Completed
Branch master (1afc22)
by Dan
09:51 queued 07:56
created

MemcacheStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 110
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A set() 0 5 1
A has() 0 6 2
A get() 0 4 1
A delete() 0 4 1
A flush() 0 4 1
A addServer() 0 4 1
A clear() 0 3 1
1
<?php
2
/**
3
 * Src/Cache/Storage/MemcacheStorage.php
4
 *
5
 * @package     Ds\Cache\Storage
6
 * @subpackage  Cache
7
 * @author      Dan Smith <[email protected]>
8
 * @version     v.1 (20/03/2017)
9
 * @copyright   Copyright (c) 2017, Dan Smith
10
 */
11
namespace Ds\Cache\Storage;
12
13
use DateInterval;
14
15
/**
16
 * Class MemcacheStorage
17
 *
18
 * @package Ds\Cache\Storage
19
 */
20
class MemcacheStorage extends AbstractStorage
21
{
22
    /**
23
     * @var \Memcached
24
     */
25
    private $mmc;
26
27
    /**
28
     * MemcacheStorage constructor.
29
     * @param \Memcached $memcache
30
     * @param string $server
31
     * @param int $port
32
     * @param DateInterval $ttl
33
     */
34 7
    public function __construct(\Memcached $memcache, $server = 'localhost', $port = 11211, DateInterval $ttl)
35
    {
36 7
        $this->mmc = $memcache;
37 7
        $this->mmc->addServer($server, $port);
38 7
        parent::__construct($ttl);
39 7
    }
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 $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 1
    public function set($key, $value, $ttl = null)
53
    {
54 1
        $expires = $this->getTtlTimestamp($ttl);
55 1
        return $this->mmc->set($key, $value, $expires);
56
    }
57
58
    /**
59
     * Determines whether an item is present in the cache.
60
     *
61
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
62
     * and not to be used within your live applications operations for get/set, as this method
63
     * is subject to a race condition where your has() will return true and immediately after,
64
     * another script can remove it making the state of your app out of date.
65
     *
66
     * @param string $key The cache item key.
67
     *
68
     * @return bool
69
     */
70 2
    public function has($key){
71 2
        if (!empty($this->mmc->get($key))){
72 1
            return true;
73
        }
74 1
        return false;
75
    }
76
77
    /**
78
     * Fetches a value from the cache.
79
     *
80
     * @param string $key     The unique key of this item in the cache.
81
     *
82
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
83
     *
84
     */
85 1
    public function get($key)
86
    {
87 1
        return $this->mmc->get($key);
88
    }
89
90
    /**
91
     * Delete an item from the cache by its unique key.
92
     *
93
     * @param string $key The unique cache key of the item to delete.
94
     *
95
     * @return bool True if the item was successfully removed. False if there was an error.
96
     */
97 1
    public function delete($key)
98
    {
99 1
        return $this->mmc->delete($key);
100
    }
101
102
    /**
103
     * @param int $delay
104
     * @return bool
105
     */
106 1
    public function flush($delay = 0)
107
    {
108 1
        return $this->mmc->flush($delay);
109
    }
110
111
    /**
112
     * Wipes clean the entire cache's keys.
113
     *
114
     * @return bool True on success and false on failure.
115
     */
116
    public function clear(){
117
        return $this->flush();
118
    }
119
120
    /**
121
     * Add new server to Memcache pool.
122
     * @param $server
123
     * @param $port
124
     */
125 1
    public function addServer($server, $port)
126
    {
127 1
        $this->mmc->addServer($server, $port);
128 1
    }
129
}
130