Completed
Pull Request — master (#2)
by Joao
02:35
created

MemcachedEngine::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace ByJG\Cache\Psr16;
4
5
use ByJG\Cache\CacheEngineInterface;
6
use Memcached;
7
use Psr\Log\NullLogger;
8
9
class MemcachedEngine extends BaseCacheEngine
10
{
11
12
    /**
13
     *
14
     * @var Memcached
15
     */
16
    protected $memCached = null;
17
18
    protected $logger = null;
19
20
    protected $servers = null;
21
22 View Code Duplication
    public function __construct($servers = null, $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $this->servers = (array)$servers;
25
        if (is_null($servers)) {
26
            $this->servers = [
27
                '127.0.0.1:11211'
28
            ];
29
        }
30
31
        $this->logger = $logger;
32
        if (is_null($logger)) {
33
            $this->logger = new NullLogger();
34
        }
35
    }
36
37
    protected function fixKey($key) {
38
        return "cache-" . $key;
39
    }
40
41
    protected function lazyLoadMemCachedServers()
42
    {
43
        if (is_null($this->memCached)) {
44
            $this->memCached = new Memcached();
45
            foreach ($this->servers as $server) {
46
                $data = explode(":", $server);
47
                $this->memCached->addServer($data[0], $data[1]);
48
49
                $stats = $this->memCached->getStats();
50
                if (!isset($stats[$server]) || $stats[$server]['pid'] === -1) {
51
                    throw new \Exception("Memcached server $server is down");
52
                }
53
            }
54
        }
55
    }
56
57
    /**
58
     * @param string $key The object KEY
59
     * @param int $default IGNORED IN MEMCACHED.
60
     * @return mixed Description
61
     */
62
    public function get($key, $default = null)
63
    {
64
        $this->lazyLoadMemCachedServers();
65
66
        $value = $this->memCached->get($this->fixKey($key));
67 View Code Duplication
        if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            $this->logger->info("[Memcached] Cache '$key' missed with status " . $this->memCached->getResultCode());
69
            return $default;
70
        }
71
72
        return unserialize($value);
73
    }
74
75
    /**
76
     * @param string $key The object Key
77
     * @param object $value The object to be cached
78
     * @param int $ttl The time to live in seconds of this objects
79
     * @return bool If the object is successfully posted
80
     */
81
    public function set($key, $value, $ttl = null)
82
    {
83
        $this->lazyLoadMemCachedServers();
84
85
        $this->memCached->set($this->fixKey($key), serialize($value), $ttl);
86
        $this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode());
87 View Code Duplication
        if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            $this->logger->error("[Memcached] Set '$key' failed with status " . $this->memCached->getResultCode());
89
        }
90
91
        return $this->memCached->getResultCode() === Memcached::RES_SUCCESS;
92
    }
93
94
    /**
95
     * @param string $key
96
     * @return bool
97
     */
98
    public function delete($key)
99
    {
100
        $this->lazyLoadMemCachedServers();
101
102
        $this->memCached->delete($this->fixKey($key));
103
        return true;
104
    }
105
106
    public function isAvailable()
107
    {
108
        if (!class_exists('\Memcached')) {
109
            return false;
110
        }
111
112
        try {
113
            $this->lazyLoadMemCachedServers();
114
            return true;
115
        } catch (\Exception $ex) {
116
            return false;
117
        }
118
    }
119
120
    public function clear()
121
    {
122
        $this->lazyLoadMemCachedServers();
123
        $result = $this->memCached->flush();
124
        return $result;
125
    }
126
127
    public function has($key)
128
    {
129
        $this->lazyLoadMemCachedServers();
130
131
        $this->memCached->get($this->fixKey($key));
132
        return ($this->memCached->getResultCode() === Memcached::RES_SUCCESS);
133
    }
134
}
135