Memcached   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 127
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getObject() 0 4 1
A delete() 0 5 1
A clear() 0 4 1
A __construct() 0 11 1
A __destruct() 0 5 1
A getKeyCode() 0 4 1
A put() 0 7 1
A get() 0 6 1
A has() 0 12 2
1
<?php
2
/**
3
 * Cache using Memcached
4
 *
5
 * @package     erdiko/core
6
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
7
 * @author      Varun Brahme
8
 * @author      John Arroyo <[email protected]>
9
 */
10
namespace erdiko\core\cache;
11
12
use erdiko\core\cache\CacheInterface;
13
14
15
class Memcached implements CacheInterface
16
{
17
    /**
18
     * Memcached Object
19
     */
20
    protected $memcacheObj;
21
    
22
    /**
23
     * Constructor
24
     */
25
    public function __construct()
26
    {
27
        // Connection creation
28
        $this->memcacheObj = new \Memcached;
29
30
        $config = \erdiko\core\Helper::getConfig("local/cache");
31
        $host=$config["memcached"]["host"];
32
        $port=$config["memcached"]["port"];
33
        $cacheAvailable = $this->memcacheObj->addServer($host, $port);
0 ignored issues
show
Unused Code introduced by
$cacheAvailable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
    }
36
37
    /**
38
     * Destructor
39
     */
40
    public function __destruct()
41
    {
42
        $this->memcacheObj->quit();
43
        unset($this->memcacheObj);
44
    }
45
46
    /**
47
     *  Get Cache Object
48
     *
49
     *  @return Memcache $memcacheObj
50
     *  @note   Please do not modify data if you use the put function in this class
51
     */
52
    public function getObject()
53
    {
54
        return $this->memcacheObj;
55
    }
56
57
    /**
58
     *  MD5 encode
59
     *
60
     *  @parm mixed $key
61
     *  @return string $key
62
     */
63
    public function getKeyCode($key)
64
    {
65
        return md5($key);
66
    }
67
68
69
    /**
70
     *  Put data into cache
71
     *
72
     *  @parm mixed $key
73
     *  @parm mixed $data
74
     *  @note If you put a object into cache,
75
     *        the json_encode function will ignore any private property.
76
     */
77
    public function put($key, $data)
78
    {
79
80
        $key = $this->getKeyCode($key);
81
        $data = json_encode($data);
82
        $this->memcacheObj->set($key, $data);
83
    }
84
    
85
    /**
86
     *  Get value from cache
87
     *
88
     *  @parm mixed $key
89
     *  @return string $value
90
     *  @note Any cache array will get return as object
91
     *  @note If you need an array, use (array) $object
92
     *
93
     */
94
    public function get($key)
95
    {
96
        $key = $this->getKeyCode($key);
97
        $value = $this->memcacheObj->get($key);
98
        return json_decode($value);
99
    }
100
101
    /**
102
     *  Check if the key exists in cache
103
     *
104
     *  @parm mixed $key
105
     *  @return true if the key exist in cache
106
     *  @return false if the key does not exist in cache
107
     *
108
     */
109
    public function has($key)
110
    {
111
        $key = $this->getKeyCode($key);
112
113
        $value = $this->memcacheObj->get($key);
114
        
115
        if (!$value) {
116
            return false;
117
        } else {
118
            return true;
119
        }
120
    }
121
    
122
    /**
123
     *  Remove a key from cache
124
     *
125
     *  @parm mixed $key
126
     *
127
     */
128
    public function delete($key)
129
    {
130
        $filename = $this->getKeyCode($key);
131
        $this->memcacheObj->delete($filename);
132
    }
133
134
    /**
135
     *  Flush all the cache
136
     */
137
    public function clear()
138
    {
139
        $this->memcacheObj->flush();
140
    }
141
}
142