Mem_cache::delete_func()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * Image CMS
9
 * Memcache Class
10
 *
11
 */
12
class Mem_cache extends Memcache
13
{
14
15
    public $CI;
16
17
    public $get = 0;
18
19
    public $set = 0;
20
21
    public $key_prefix = '';
22
23
    //Cache config
24
    public $_Config = [
25
                       'store' => 'cache',
26
                       'ttl'   => 3600,
27
                      ];
28
29
    public function __construct() {
30
31
        $this->CI = & get_instance();
32
        $this->connect('localhost', 11211) or die('Could not connect to memcache server.');
33
        $this->key_prefix = base_url();
34
    }
35
36
    /**
37
     * Fetch Cache
38
     *
39
     * @param string $key
40
     *
41
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|string|string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
42
     */
43
    public function fetch($key, $group = FALSE) {
0 ignored issues
show
Unused Code introduced by
The parameter $group is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
45
        if (($ret = $this->_fetch($key)) === false) {
46
            return false;
47
        } else {
48
            return $ret;
49
        }
50
    }
51
52
    public function _fetch($key) {
53
54
        $key = $this->generatekey($key);
55
56
        $this->get++;
57
58
        return $this->get($key);
59
    }
60
61
    /**
62
     * Fetch cached function
63
     */
64
    public function fetch_func($object, $func, $args = []) {
65
66
        $key = $this->generatekey(get_class($object) . '::' . $func . '::' . serialize($args));
67
68
        $this->get++;
69
70
        return $this->get($key);
71
    }
72
73
    /**
74
     * Store Cache Item
75
     *
76
     * @param string  $key
77
     * @param mixed   $data
78
     * @param int     $ttl
79
     *
80
     * @return bool
81
     */
82
    public function store($key, $data, $ttl = false, $group = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $group is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
84
        if (!$ttl) {
85
            $ttl = $this->_Config['ttl'];
86
        }
87
88
        $this->set($this->generatekey($key), $data, FALSE, $ttl);
89
90
        $this->set++;
91
        return TRUE;
92
    }
93
94
    /**
95
     * Cache Function
96
     *
97
     * @return mixed
98
     * @access public
99
     */
100
    public function call($func = [], $args = [], $ttl = FALSE) {
0 ignored issues
show
Unused Code introduced by
The parameter $func is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
102
        if ($ttl === FALSE) {
103
            $ttl = $this->_Config['ttl'];
0 ignored issues
show
Unused Code introduced by
$ttl 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...
104
        }
105
106
        $arguments = func_get_args();
107
108
        //class_name::metohd
109
        $key = get_class($arguments[0][0]) . '::' . $arguments[0][1] . '::' . serialize($args);
110
111 View Code Duplication
        if (($cache = $this->fetch($key)) !== false) {
112
            return $cache;
113
        } else {
114
            $target = array_shift($arguments);
115
            $result = call_user_func_array($target, $args);
116
117
            if (!$this->store($key, $result, false)) {
118
                return FALSE;
119
            }
120
121
            return $result;
122
        }
123
    }
124
125
    /**
126
     * Clean all cache objects
127
     *
128
     * @return void
129
     */
130
    public function Clean() {
131
132
        $this->flush();
133
    }
134
135
    /**
136
     * Clean all cache objects
137
     *
138
     * @return void
139
     */
140
    public function delete_group() {
141
142
        $this->flush();
143
    }
144
145
    /**
146
     * Delete Cache Item
147
     *
148
     * @param string $key - cache item key
149
     *
150
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
151
     */
152
    public function delete($key) {
153
154
        $this->delete($this->generatekey($key));
155
    }
156
157
    /**
158
     * Delete Cached Function
159
     *
160
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
161
     */
162
    public function delete_func($object, $func, $args = []) {
163
164
        $file = $this->generatekey(get_class($object) . '::' . $func . '::' . serialize($args));
165
        $this->delete($file);
166
    }
167
168
    /**
169
     * Delete All Cache Items
170
     *
171
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
172
     * @access public
173
     */
174
    public function delete_all() {
175
176
        $this->flush();
177
    }
178
179
    /**
180
     * Generate key
181
     *
182
     * @param  $key
183
     * @return string */
184
    public function generatekey($key) {
185
186
        return md5($this->key_prefix . $key);
187
    }
188
189
}
190
191
/* End of cache.php */