Completed
Push — master ( e1b391...0629e4 )
by David
14s queued 10s
created

FileBoundMemoryAdapter::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\CacheUtils;
6
7
/**
8
 * An adapter around a FileBoundCacheInterface that stores values in memory for the current request (for maximum performance)
9
 */
10
class FileBoundMemoryAdapter implements FileBoundCacheInterface
11
{
12
    /** @var array<string, mixed> */
13
    private $items;
14
    /** @var FileBoundCacheInterface */
15
    private $fileBoundCache;
16
17
    public function __construct(FileBoundCacheInterface $fileBoundCache)
18
    {
19
        $this->fileBoundCache = $fileBoundCache;
20
    }
21
22
    /**
23
     * Fetches an element from the cache by key.
24
     *
25
     * @return mixed
26
     */
27
    public function get(string $key)
28
    {
29
        if (isset($this->items[$key])) {
30
            return $this->items[$key];
31
        }
32
33
        return $this->items[$key] = $this->fileBoundCache->get($key);
34
    }
35
36
    /**
37
     * Stores an item in the cache.
38
     *
39
     * @param mixed $item The item must be serializable.
40
     * @param array<int, string> $fileNames If one of these files is touched, the cache item is invalidated.
41
     */
42
    public function set(string $key, $item, array $fileNames, ?int $ttl = null): void
43
    {
44
        $this->items[$key] = $item;
45
        $this->fileBoundCache->set($key, $item, $fileNames, $ttl);
46
    }
47
}
48