Passed
Push — main ( e7b3b2...eecacd )
by Dimitri
02:12
created

Wincache::clear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cache\Handlers;
13
14
use DateInterval;
15
use RuntimeException;
16
17
/**
18
 * Moteur de stockage Wincache pour le cache
19
 *
20
 * Prend en charge Wincache 1.1.0 et supérieur.
21
 */
22
class Wincache extends BaseHandler
23
{
24
    /**
25
     * Contient les noms de groupe compilés
26
     * (préfixé par le préfixe de configuration global)
27
     */
28
    protected array $_compiledGroupNames = [];
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function init(array $config = []): bool
34
    {
35
        if (! extension_loaded('wincache')) {
36
            throw new RuntimeException('L\'extension `wincache` doit être activée pour utiliser WincacheHandler.');
37
        }
38
39
        parent::init($config);
40
41
        return true;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
48
    {
49
        $key      = $this->_key($key);
50
        $duration = $this->duration($ttl);
51
52
        return wincache_ucache_set($key, $value, $duration);
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function get(string $key, mixed $default = null): mixed
59
    {
60
        $value = wincache_ucache_get($this->_key($key), $success);
61
        if ($success === false) {
62
            return $default;
63
        }
64
65
        return $value;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function increment(string $key, int $offset = 1)
72
    {
73
        $key = $this->_key($key);
74
75
        return wincache_ucache_inc($key, $offset);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function decrement(string $key, int $offset = 1)
82
    {
83
        $key = $this->_key($key);
84
85
        return wincache_ucache_dec($key, $offset);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function delete(string $key): bool
92
    {
93
        $key = $this->_key($key);
94
95
        return wincache_ucache_delete($key);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function clear(): bool
102
    {
103
        $info      = wincache_ucache_info();
104
        $cacheKeys = $info['ucache_entries'];
105
        unset($info);
106
107
        foreach ($cacheKeys as $key) {
108
            if (strpos($key['key_name'], $this->_config['prefix']) === 0) {
109
                wincache_ucache_delete($key['key_name']);
110
            }
111
        }
112
113
        return true;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function groups(): array
120
    {
121
        if (empty($this->_compiledGroupNames)) {
122
            foreach ($this->_config['groups'] as $group) {
123
                $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
124
            }
125
        }
126
127
        $groups = wincache_ucache_get($this->_compiledGroupNames);
128
        if (count($groups) !== count($this->_config['groups'])) {
129
            foreach ($this->_compiledGroupNames as $group) {
130
                if (! isset($groups[$group])) {
131
                    wincache_ucache_set($group, 1);
132
                    $groups[$group] = 1;
133
                }
134
            }
135
            ksort($groups);
136
        }
137
138
        $result = [];
139
        $groups = array_values($groups);
140
141
        foreach ($this->_config['groups'] as $i => $group) {
142
            $result[] = $group . $groups[$i];
143
        }
144
145
        return $result;
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151
    public function clearGroup(string $group): bool
152
    {
153
        $success = false;
154
        wincache_ucache_inc($this->_config['prefix'] . $group, 1, $success);
155
156
        return $success;
157
    }
158
}
159