Passed
Push — master ( ab102c...5f4e36 )
by Alexander
02:10
created

CacheItems::expired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache\Metadata;
6
7
use Psr\SimpleCache\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 50
    public function expired(string $key, float $beta, CacheInterface $cache): bool
31
    {
32 50
        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 43
    public function set(CacheItem $item): void
41
    {
42 43
        $key = $item->key();
43
44 43
        if (!isset($this->items[$key])) {
45 43
            $this->items[$key] = $item;
46 43
            return;
47
        }
48
49 7
        $this->items[$key]->update($item->expiry(), $item->dependency());
50 7
    }
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 13
    }
63
}
64