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

nette / caching / 40596724178

27 Oct 2026 10:33PM UTC coverage: 89.89% (+0.02%) from 89.872%
40596724178

push

github

dg
SQLiteStorage: added support for cleaning namespace

3 of 3 new or added lines in 1 file covered. (100.0%)

23 existing lines in 2 files now uncovered.

898 of 999 relevant lines covered (89.89%)

0.9 hits per line

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

86.25
/src/Bridges/Psr/Psr16Storage.php
1
<?php declare(strict_types=1);
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
namespace Nette\Bridges\Psr;
9

10
use Nette;
11
use Nette\Caching\Cache;
12
use Nette\Caching\Storages\Journal;
13
use Psr;
14
use function is_array;
15

16

17
/**
18
 * Adapts any PSR-16 SimpleCache implementation to the Nette Storage interface,
19
 * making all PSR-16 backends usable as Nette cache storage.
20
 *
21
 * Keys are hashed to fit the character set that PSR-16 guarantees to support,
22
 * the optional prefix must therefore contain only characters A-Z a-z 0-9 _ and dot.
23
 * Note that clean(Cache::All) calls clear() on the underlying PSR-16 cache, so regardless of the prefix
24
 * it also wipes the data of anyone else sharing that cache.
25
 */
26
class Psr16Storage implements Nette\Caching\Storage, Nette\Caching\BulkReader, Nette\Caching\BulkWriter
27
{
28
        /** @internal cache structure */
29
        private const
30
                MetaData = 'data',
31
                MetaCallbacks = 'callbacks'; // array of callbacks (function, args)
32

33

34
        public function __construct(
1 ✔
35
                private readonly Psr\SimpleCache\CacheInterface $cache,
36
                private readonly string $prefix = '',
37
                private readonly ?Journal $journal = null,
38
        ) {
39
        }
1 ✔
40

41

42
        public function read(string $key): mixed
43
        {
44
                $key = $this->formatKey($key);
1 ✔
45
                $meta = $this->cache->get($key);
1 ✔
46
                if (!is_array($meta)) {
1 ✔
47
                        return null;
1 ✔
48
                }
49

50
                // verify dependencies
51
                if (!empty($meta[self::MetaCallbacks]) && !Cache::checkCallbacks($meta[self::MetaCallbacks])) {
1 ✔
52
                        $this->cache->delete($key);
1 ✔
53
                        return null;
1 ✔
54
                }
55

56
                return $meta[self::MetaData];
1 ✔
57
        }
58

59

60
        public function bulkRead(array $keys): array
61
        {
62
                $formattedKeys = [];
1 ✔
63
                foreach ($keys as $key) {
1 ✔
64
                        $formattedKeys[$this->formatKey($key)] = $key;
1 ✔
65
                }
66

67
                $result = [];
1 ✔
68
                foreach ($this->cache->getMultiple(array_keys($formattedKeys)) as $formattedKey => $meta) {
1 ✔
69
                        if (!is_array($meta)) {
1 ✔
70
                                continue;
1 ✔
71

72
                        } elseif (!empty($meta[self::MetaCallbacks]) && !Cache::checkCallbacks($meta[self::MetaCallbacks])) {
1 ✔
UNCOV
73
                                $this->cache->delete($formattedKey);
×
UNCOV
74
                                continue;
×
75
                        }
76

77
                        $result[$formattedKeys[$formattedKey]] = $meta[self::MetaData];
1 ✔
78
                }
79

80
                return $result;
1 ✔
81
        }
82

83

84
        public function lock(string $key): void
85
        {
UNCOV
86
        }
×
87

88

89
        public function write(string $key, mixed $data, array $dependencies): void
90
        {
91
                $this->checkDependencies($dependencies);
1 ✔
92
                $key = $this->formatKey($key);
1 ✔
93
                [$meta, $ttl] = $this->buildMeta($data, $dependencies);
1 ✔
94
                if ($ttl !== null && $ttl <= 0) {
1 ✔
95
                        $this->cache->delete($key);
×
UNCOV
96
                        return;
×
97
                }
98

99
                if (isset($dependencies[Cache::Tags]) || isset($dependencies[Cache::Priority])) {
1 ✔
100
                        if (!$this->journal) {
1 ✔
101
                                throw new Nette\InvalidStateException('CacheJournal has not been provided.');
1 ✔
102
                        }
103

104
                        $this->journal->write($key, $dependencies);
1 ✔
105
                }
106

107
                $this->cache->set($key, $meta, $ttl);
1 ✔
108
        }
1 ✔
109

110

111
        public function bulkWrite(array $items, array $dependencies): void
112
        {
113
                $this->checkDependencies($dependencies);
1 ✔
114
                $ttl = isset($dependencies[Cache::Expire]) ? (int) $dependencies[Cache::Expire] : null;
1 ✔
115
                if ($ttl !== null && $ttl <= 0) {
1 ✔
UNCOV
116
                        $this->bulkRemove(array_map(strval(...), array_keys($items)));
×
UNCOV
117
                        return;
×
118
                }
119

120
                $records = [];
1 ✔
121
                foreach ($items as $key => $data) {
1 ✔
122
                        $key = $this->formatKey((string) $key);
1 ✔
123
                        [$records[$key]] = $this->buildMeta($data, $dependencies);
1 ✔
124

125
                        if (isset($dependencies[Cache::Tags]) || isset($dependencies[Cache::Priority])) {
1 ✔
126
                                if (!$this->journal) {
×
UNCOV
127
                                        throw new Nette\InvalidStateException('CacheJournal has not been provided.');
×
128
                                }
129

UNCOV
130
                                $this->journal->write($key, $dependencies);
×
131
                        }
132
                }
133

134
                $this->cache->setMultiple($records, $ttl);
1 ✔
135
        }
1 ✔
136

137

138
        /**
139
         * Rejects the dependencies it cannot honor. Sliding expiration would need to extend the TTL without writing
140
         * the item again, which PSR-16 cannot do: a read followed by set() would store back an item removed in between.
141
         * @param  array<string, mixed>  $dependencies
142
         */
143
        private function checkDependencies(array $dependencies): void
144
        {
145
                if (isset($dependencies[Cache::Items])) {
1 ✔
146
                        throw new Nette\NotSupportedException('Dependency Cache::Items is not supported by ' . static::class . '.');
1 ✔
147
                } elseif (isset($dependencies[Cache::Expire]) && !empty($dependencies[Cache::Sliding])) {
1 ✔
148
                        throw new Nette\NotSupportedException('Dependency Cache::Sliding is not supported by ' . static::class . '.');
1 ✔
149
                }
150
        }
1 ✔
151

152

153
        /**
154
         * @param  array<string, mixed>  $dp
155
         * @return array{array<string, mixed>, ?int}  meta structure and TTL in seconds
156
         */
157
        private function buildMeta(mixed $data, array $dp): array
158
        {
159
                $meta = [
1 ✔
160
                        self::MetaData => $data,
1 ✔
161
                ];
162

163
                $ttl = isset($dp[Cache::Expire]) ? (int) $dp[Cache::Expire] : null;
1 ✔
164
                if (isset($dp[Cache::Callbacks])) {
1 ✔
165
                        $meta[self::MetaCallbacks] = $dp[Cache::Callbacks];
1 ✔
166
                }
167

168
                return [$meta, $ttl];
1 ✔
169
        }
170

171

172
        public function remove(string $key): void
173
        {
174
                $this->cache->delete($this->formatKey($key));
1 ✔
175
        }
1 ✔
176

177

178
        public function bulkRemove(array $keys): void
179
        {
180
                $formattedKeys = [];
1 ✔
181
                foreach ($keys as $key) {
1 ✔
182
                        $formattedKeys[] = $this->formatKey($key);
1 ✔
183
                }
184

185
                $this->cache->deleteMultiple($formattedKeys);
1 ✔
186
        }
1 ✔
187

188

189
        public function clean(array $conditions): void
190
        {
191
                if (isset($conditions[Cache::Namespaces])) {
1 ✔
UNCOV
192
                        throw new Nette\NotSupportedException('Cleaning by Cache::Namespaces is not supported by ' . static::class . '.');
×
193
                }
194

195
                if (!empty($conditions[Cache::All])) {
1 ✔
196
                        $this->cache->clear();
1 ✔
197
                        $this->journal?->clean($conditions);
1 ✔
198

199
                } elseif ($this->journal) {
1 ✔
200
                        $keys = $this->journal->clean($conditions) ?? [];
1 ✔
201
                        if ($keys) {
1 ✔
202
                                $this->cache->deleteMultiple($keys);
1 ✔
203
                        }
204
                }
205
        }
1 ✔
206

207

208
        /**
209
         * Converts the key to characters that PSR-16 implementations are guaranteed to support.
210
         */
211
        private function formatKey(string $key): string
212
        {
213
                return $this->prefix . hash('xxh128', $key);
1 ✔
214
        }
215
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc