Completed
Push — master ( 6d0805...e0c924 )
by Lars
01:52
created

AdapterApc::cacheInfo()   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 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterApc: a APC-Cache adapter
9
 *
10
 * @see http://php.net/manual/de/book.apc.php
11
 */
12
class AdapterApc 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 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
            \PHP_SAPI !== 'cli'
31
            &&
32
            \function_exists('apc_store') === true
33
            &&
34
            \ini_get('apc.enabled')
35
        ) {
36
            $this->installed = true;
37
        }
38
39
        if (
40
            \PHP_SAPI === 'cli'
41
            &&
42
            \function_exists('apc_store') === true
43
            &&
44
            \ini_get('apc.enable_cli')
45
        ) {
46
            $this->installed = true;
47
        }
48
    }
49
50
    /**
51
     * Check if apc-cache exists.
52
     *
53
     * WARNING: use $this->exists($key) instead
54
     *
55
     * @param string $key
56
     *
57
     * @return bool
58
     *
59
     * @internal
60
     */
61
    public function apc_cache_exists($key): bool
62
    {
63
        return (bool) \apc_fetch($key);
64
    }
65
66
    /**
67
     * Clears the APC cache by type.
68
     *
69
     * @param string $type   - If $type is "user", the user cache will be cleared; otherwise,
70
     *                       the system cache (cached files) will be cleared
71
     *
72
     * @return bool
73
     *
74
     * @internal
75
     */
76
    public function cacheClear(string $type): bool
77
    {
78
        return (bool) \apc_clear_cache($type);
79
    }
80
81
    /**
82
     * Retrieves cached information from APC's data store
83
     *
84
     * @param string $type     - If $type is "user", information about the user cache will be returned
85
     * @param bool   $limited  - If $limited is TRUE, the return value will exclude the individual list of cache
86
     *                         entries. This is useful when trying to optimize calls for statistics gathering
87
     *
88
     * @return array|bool <p>Array of cached data (and meta-data) or FALSE on failure.</p>
89
     */
90
    public function cacheInfo(string $type = '', bool $limited = false): array
91
    {
92
        return \apc_cache_info($type, $limited);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function exists(string $key): bool
99
    {
100
        if (\function_exists('apc_exists')) {
101
            return (bool) \apc_exists($key);
102
        }
103
104
        return $this->apc_cache_exists($key);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function get(string $key)
111
    {
112
        if ($this->exists($key)) {
113
            return \apc_fetch($key);
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function installed(): bool
123
    {
124
        return $this->installed;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function remove(string $key): bool
131
    {
132
        return (bool) \apc_delete($key);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function removeAll(): bool
139
    {
140
        return (bool) ($this->cacheClear('system') && $this->cacheClear('user'));
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function set(string $key, $value): bool
147
    {
148
        return (bool) \apc_store($key, $value);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function setExpired(string $key, $data, int $ttl = 0): bool
155
    {
156
        return (bool) \apc_store($key, $data, $ttl);
157
    }
158
}
159