• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / caching / 22226743054

20 Feb 2026 01:53PM UTC coverage: 88.227% (+1.0%) from 87.202%
22226743054

Pull #85

github

web-flow
Merge c3f6b576c into f6698ea58
Pull Request #85: Extend MemoryStorage with Tags, Priority, Expire and Sliding modifiers

35 of 36 new or added lines in 1 file covered. (97.22%)

57 existing lines in 7 files now uncovered.

622 of 705 relevant lines covered (88.23%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/Caching/Storages/MemoryStorage.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Caching\Storages;
11

12
use Nette;
13
use Nette\Caching\Cache;
14
use function time;
15

16

17
/**
18
 * Memory cache storage.
19
 */
20
class MemoryStorage implements Nette\Caching\Storage
21
{
22
        private const
23
                // meta structure: array keys
24
                MetaTags = 'tags',
25
                MetaData = 'data', // value store
26
                MetaExpire = 'expire', // expiration timestamp
27
                MetaDelta = 'delta', // relative (sliding) expiration
28
                MetaPriority = 'priority';
29

30
        /** @var array<string, array{data: mixed, expire: ?int, delta: ?int, tags: string[], priority: ?int}>  key => entry */
31
        private array $data = [];
32

33

34
        public function read(string $key): mixed
1✔
35
        {
36
                if (!isset($this->data[$key])) {
1✔
37
                        return null;
1✔
38
                }
39

40
                $entry = $this->data[$key];
1✔
41

42
                if ($entry[self::MetaDelta] !== null) {
1✔
43
                        if ($entry[self::MetaExpire] < time()) {
1✔
44
                                unset($this->data[$key]);
1✔
45
                                return null;
1✔
46
                        }
47

48
                        $this->data[$key][self::MetaExpire] = time() + $entry[self::MetaDelta];
1✔
49
                } elseif ($entry[self::MetaExpire] !== null && $entry[self::MetaExpire] < time()) {
1✔
50
                        unset($this->data[$key]);
1✔
51
                        return null;
1✔
52
                }
53

54
                return $entry[self::MetaData];
1✔
55
        }
56

57

58
        public function lock(string $key): void
59
        {
60
        }
61

62

63
        public function write(string $key, $data, array $dependencies): void
1✔
64
        {
65
                $expire = isset($dependencies[Cache::Expire])
1✔
66
                        ? $dependencies[Cache::Expire] + time()
1✔
67
                        : null;
1✔
68
                $delta = isset($dependencies[Cache::Sliding])
1✔
69
                        ? (int) $dependencies[Cache::Expire]
1✔
70
                        : null;
1✔
71

72
                $this->data[$key] = [
1✔
73
                        self::MetaData => $data,
1✔
74
                        self::MetaExpire => $expire,
1✔
75
                        self::MetaDelta => $delta,
1✔
76
                        self::MetaTags => $dependencies[Cache::Tags] ?? [],
1✔
77
                        self::MetaPriority => $dependencies[Cache::Priority] ?? null,
1✔
78
                ];
79
        }
1✔
80

81

82
        public function remove(string $key): void
83
        {
UNCOV
84
                unset($this->data[$key]);
×
85
        }
86

87

88
        public function clean(array $conditions): void
1✔
89
        {
90
                if (!empty($conditions[Cache::All])) {
1✔
91
                        $this->data = [];
×
NEW
92
                        return;
×
93
                }
94

95
                if (!empty($conditions[Cache::Tags])) {
1✔
96
                        $tags = (array) $conditions[Cache::Tags];
1✔
97
                        foreach ($this->data as $key => $entry) {
1✔
98
                                if (array_intersect($tags, $entry[self::MetaTags])) {
1✔
99
                                        unset($this->data[$key]);
1✔
100
                                }
101
                        }
102
                }
103

104
                if (isset($conditions[Cache::Priority])) {
1✔
105
                        $limit = (int) $conditions[Cache::Priority];
1✔
106
                        foreach ($this->data as $key => $entry) {
1✔
107
                                if ($entry[self::MetaPriority] !== null && $entry[self::MetaPriority] <= $limit) {
1✔
108
                                        unset($this->data[$key]);
1✔
109
                                }
110
                        }
111
                }
112
        }
1✔
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc