CacheItems::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache\Metadata;
6
7
use Yiisoft\Cache\CacheInterface;
8
9
/**
10
 * CacheItems store the metadata of each cache item.
11
 *
12
 * @internal
13
 */
14
final class CacheItems
15
{
16
    /**
17
     * @var array<string, CacheItem>
18
     */
19
    private array $items = [];
20
21
    /**
22
     * Checks whether the dependency has been changed or whether the cache expired.
23
     *
24
     * @param string $key The key that identifies the cache item.
25
     * @param float $beta The value for calculating the range that is used for "Probably early expiration" algorithm.
26
     * @param CacheInterface $cache The actual cache handler.
27
     *
28
     * @return bool Whether the dependency has been changed or whether the cache expired.
29
     */
30 46
    public function expired(string $key, float $beta, CacheInterface $cache): bool
31
    {
32 46
        return isset($this->items[$key]) && $this->items[$key]->expired($beta, $cache);
33
    }
34
35
    /**
36
     * Adds or updates a cache item.
37
     *
38
     * @param CacheItem $item The cache item.
39
     */
40 45
    public function set(CacheItem $item): void
41
    {
42 45
        $key = $item->key();
43
44 45
        if (!isset($this->items[$key])) {
45 45
            $this->items[$key] = $item;
46 45
            return;
47
        }
48
49 15
        $this->items[$key]->update($item->expiry(), $item->dependency());
50
    }
51
52
    /**
53
     * Removes a cache item with the specified key.
54
     *
55
     * @param string $key The key that identifies the cache item.
56
     */
57 13
    public function remove(string $key): void
58
    {
59 13
        if (isset($this->items[$key])) {
60 13
            unset($this->items[$key]);
61
        }
62
    }
63
}
64