Completed
Push — master ( dfda1d...2f160c )
by Lars
04:17 queued 18s
created

AdapterApcu::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterApcu: a APCu-Cache adapter
9
 *
10
 * @see http://php.net/manual/de/book.apcu.php
11
 */
12
class AdapterApcu implements iAdapter
13
{
14
    /**
15
     * @var bool
16
     */
17
    public $installed = false;
18
19
    /**
20
     * @var bool
21
     */
22
    public $debug = false;
23
24
    /**
25
     * __construct()
26
     */
27 1 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        if (
30 1
            \PHP_SAPI !== 'cli'
31
            &&
32
            \function_exists('apcu_store') === true
33
            &&
34
            \ini_get('apc.enabled')
35
        ) {
36
            $this->installed = true;
37
        }
38
39
        if (
40
            \PHP_SAPI === 'cli'
41
            &&
42 1
            \function_exists('apcu_store') === true
43
            &&
44 1
            \ini_get('apc.enable_cli')
45
        ) {
46
            \ini_set('apc.use_request_time', '0');
47
48
            $this->installed = true;
49
        }
50 1
    }
51
52
    /**
53
     * Check if apcu-cache exists.
54
     *
55
     * WARNING: we only keep this method for compatibly-reasons
56
     *          -> use ->exists($key)
57
     *
58
     * @param string $key
59
     *
60
     * @return bool
61
     *
62
     * @deprecated
63
     */
64
    public function apcu_cache_exists($key): bool
65
    {
66
        return $this->exists($key);
67
    }
68
69
    /**
70
     * Clears the APCu cache by type.
71
     *
72
     * @param string $type <p>WARNING: is not used in APCu only valid for APC</p>
73
     *
74
     * @return bool
75
     *
76
     * @internal
77
     */
78
    public function cacheClear(string $type): bool
0 ignored issues
show
Unused Code introduced by
The parameter $type 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...
79
    {
80
        return (bool) \apcu_clear_cache();
81
    }
82
83
    /**
84
     * Retrieves cached information from APCu's data store
85
     *
86
     * @param bool $limited    - If $limited is TRUE, the return value will exclude the individual list of cache
87
     *                         entries. This is useful when trying to optimize calls for statistics gathering
88
     *
89
     * @return array|false
90
     *                    <p>Array of cached data (and meta-data) or FALSE on failure.</p>
91
     */
92
    public function cacheInfo(bool $limited = false): array
93
    {
94
        return \apcu_cache_info($limited);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function exists(string $key): bool
101
    {
102
        return (bool) \apcu_exists($key);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function get(string $key)
109
    {
110
        if ($this->exists($key)) {
111
            return \apcu_fetch($key);
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function installed(): bool
121
    {
122
        return $this->installed;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function remove(string $key): bool
129
    {
130
        return (bool) \apcu_delete($key);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function removeAll(): bool
137
    {
138
        return (bool) ($this->cacheClear('system') && $this->cacheClear('user'));
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function set(string $key, $value): bool
145
    {
146
        return (bool) \apcu_store($key, $value);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function setExpired(string $key, $data, int $ttl = 0): bool
153
    {
154
        return (bool) \apcu_store($key, $data, $ttl);
155
    }
156
}
157