Passed
Push — develop ( 7bbca0...8be0b9 )
by Mathieu
01:38
created

Memcache   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 52.83%

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 120
rs 10
ccs 28
cts 53
cp 0.5283
wmc 18

13 Methods

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