MemcacheDriver::fetch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Millennium\Cache\Drivers;
4
5
use Millennium\Cache\Interfaces\CacheDriverInterface;
6
7
class MemcacheDriver implements CacheDriverInterface
8
{
9
    /**
10
     * @var \Memcache
11
     */
12
    private $memcache;
13
14
    /**
15
     * @var int
16
     */
17
    private $expire;
18
19
    public function __construct($_options = [])
20
    {
21
        if (!class_exists('Memcache')) {
22
            throw new \Millennium\Cache\Exceptions\DriverNotFoundException('Memcached not installed on your system');
23
        }
24
        $options = array_merge(['host' => '127.0.0.1', 'port' => '11211'], $_options);
25
        $this->memcache = new \Memcache();
26
        $this->memcache->connect($options['host'], $options['port']);
27
        $this->expire = isset($options['expire']) && ctype_digit($options['expire']) ? $options['expire'] : 3600;
28
    }
29
30
    public function fetch($key)
31
    {
32
        if (null !== $this->memcache->get($key)) {
33
            return $this->memcache->get($key);
34
        }
35
36
        return false;
37
    }
38
39
    public function remove($key)
40
    {
41
        return $this->memcache->delete($key);
42
    }
43
44
    public function store($key, $data, $expire = null)
45
    {
46
        return $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $expire ? $expire : time() + $this->expire);
47
    }
48
}
49