Memcache::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Cache;
6
7
use Suricate;
8
9
/**
10
 * Memcache extension for Suricate
11
 *
12
 * @package Suricate
13
 * @author  Mathieu LESNIAK <[email protected]>
14
 *
15
 * @property string $host           Memcache host (default: localhost)
16
 * @property int    $port           Memcache port (default: 11211)
17
 * @property int    $defaultExpiry  Key default expiry
18
 * @property bool   $useCompression Use memcache compression (default: false)
19
 */
20
21
class Memcache extends Suricate\Cache
22
{
23
    protected $parametersList = [
24
        'host',
25
        'port',
26
        'defaultExpiry',
27
        'useCompression'
28
    ];
29
    private $handler;
30
31 9
    public function __construct()
32
    {
33 9
        parent::__construct();
34
35 9
        $this->handler = false;
36 9
        $this->host = 'localhost';
37 9
        $this->port = 11211;
38 9
        $this->useCompression = false;
39 9
        $this->defaultExpiry = 3600;
40 9
    }
41
42 2
    public function getHost()
43
    {
44 2
        return $this->host;
45
    }
46
47 2
    public function setHost($host)
48
    {
49 2
        $this->host = $host;
50
51 2
        return $this;
52
    }
53
54 2
    public function getPort()
55
    {
56 2
        return $this->port;
57
    }
58
59 2
    public function setPort($port)
60
    {
61 2
        $this->port = $port;
62
63 2
        return $this;
64
    }
65
66 2
    public function getDefaultExpiry()
67
    {
68 2
        return $this->defaultExpiry;
69
    }
70
71 2
    public function setDefaultExpiry($expiry)
72
    {
73 2
        $this->defaultExpiry = $expiry;
74
75 2
        return $this;
76
    }
77
78 1
    public function getUseCompression()
79
    {
80 1
        return $this->useCompression;
81
    }
82
83 1
    public function setUseCompression($useCompression)
84
    {
85 1
        $this->useCompression = $useCompression;
86
87 1
        return $this;
88
    }
89
90
    private function connect()
91
    {
92
        if ($this->handler === false) {
93
            if (class_exists('Memcache')) {
94
                try {
95
                    $this->handler = new \Memcache();
96
                    $this->handler->connect($this->host, $this->port);
97
                } catch (\Exception $e) {
98
                    throw new \Exception('Can\'t connect to memcache server');
99
                }
100
            } else {
101
                throw new \BadMethodCallException(
102
                    'Can\'t find Memcache extension'
103
                );
104
            }
105
        } else {
106
            return $this;
107
        }
108
    }
109
110
    /**
111
     * Put a value into memcache
112
     * @param string $variable Variable name
113
     * @param mixed $value    Value
114
     * @param int $expiry   Cache expiry
115
     */
116
    public function set(string $variable, $value, $expiry = null)
117
    {
118
        $this->connect();
119
120
        if ($expiry === null) {
121
            $expiry = $this->defaultExpiry;
122
        }
123
124
        if ($this->useCompression !== false) {
125
            $flag = MEMCACHE_COMPRESSED;
126
        } else {
127
            $flag = null;
128
        }
129
130
        $this->handler->set($variable, $value, $flag, $expiry);
131
    }
132
133
    public function get(string $variable)
134
    {
135
        $this->connect();
136
        return $this->handler->get($variable);
137
    }
138
139
    public function delete(string $variable)
140
    {
141
        $this->connect();
142
        return $this->handler->delete($variable);
143
    }
144
}
145