ArrayHandler   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 5 1
A set() 0 7 1
A decrement() 0 9 2
A increment() 0 9 2
A get() 0 17 3
A clearGroup() 0 8 2
A delete() 0 6 1
A groups() 0 14 3
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
16
/**
17
 * Moteur de stockage de tableau pour le cache.
18
 *
19
 * Pas réellement un moteur de cache persistant. Toutes les données sont uniquement
20
 * stocké en mémoire pour la durée d'un seul processus. Bien que non
21
 * utile dans les paramètres de production ce moteur peut être utile dans les tests
22
 * ou des outils de console où vous ne voulez pas les frais généraux d'interaction
23
 * avec un serveur de cache, mais souhaitez que les propriétés d'enregistrement du travail soient un cache
24
 * fournit.
25
 */
26
class ArrayHandler extends BaseHandler
27
{
28
    /**
29
     * Données mises en cache.
30
     *
31
     * Structuré comme [clé => [exp => expiration, val => valeur]]
32
     */
33
    protected array $data = [];
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function set(string $key, mixed $value, null|DateInterval|int $ttl = null): bool
39
    {
40
        $key              = $this->_key($key);
41
        $expires          = time() + $this->duration($ttl);
42
        $this->data[$key] = ['exp' => $expires, 'val' => $value];
43
44
        return true;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function get(string $key, mixed $default = null): mixed
51
    {
52
        $key = $this->_key($key);
53
        if (! isset($this->data[$key])) {
54
            return $default;
55
        }
56
        $data = $this->data[$key];
57
58
        // Verifie l'expiration
59
        $now = time();
60
        if ($data['exp'] <= $now) {
61
            unset($this->data[$key]);
62
63
            return $default;
64
        }
65
66
        return $data['val'];
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function increment(string $key, int $offset = 1)
73
    {
74
        if ($this->get($key) === null) {
75
            $this->set($key, 0);
76
        }
77
        $key = $this->_key($key);
78
        $this->data[$key]['val'] += $offset;
79
80
        return $this->data[$key]['val'];
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function decrement(string $key, int $offset = 1)
87
    {
88
        if ($this->get($key) === null) {
89
            $this->set($key, 0);
90
        }
91
        $key = $this->_key($key);
92
        $this->data[$key]['val'] -= $offset;
93
94
        return $this->data[$key]['val'];
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function delete(string $key): bool
101
    {
102
        $key = $this->_key($key);
103
        unset($this->data[$key]);
104
105
        return true;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function clear(): bool
112
    {
113
        $this->data = [];
114
115
        return true;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function groups(): array
122
    {
123
        $result = [];
124
125
        foreach ($this->_config['groups'] as $group) {
126
            $key = $this->_config['prefix'] . $group;
127
            if (! isset($this->data[$key])) {
128
                $this->data[$key] = ['exp' => PHP_INT_MAX, 'val' => 1];
129
            }
130
            $value    = $this->data[$key]['val'];
131
            $result[] = $group . $value;
132
        }
133
134
        return $result;
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function clearGroup(string $group): bool
141
    {
142
        $key = $this->_config['prefix'] . $group;
143
        if (isset($this->data[$key])) {
144
            $this->data[$key]['val']++;
145
        }
146
147
        return true;
148
    }
149
}
150