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

HonkLegion / Psr16Storage / 22783359807

06 Mar 2026 09:49PM UTC coverage: 98.077%. First build
22783359807

push

github

web-flow
Merge pull request #1 from HonkLegion/initial-version

Initial version

51 of 52 new or added lines in 1 file covered. (98.08%)

51 of 52 relevant lines covered (98.08%)

2.37 hits per line

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

98.08
/src/Psr16Storage.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace HonkLegion\Psr16Storage;
6

7
use Nette;
8
use Nette\Caching\BulkReader;
9
use Nette\Caching\Cache;
10
use Nette\Caching\Storage;
11
use Psr\SimpleCache\CacheInterface;
12
use Psr\SimpleCache\InvalidArgumentException;
13

14
use function array_combine;
15
use function array_map;
16

17
/** @api */
18
class Psr16Storage implements Storage, BulkReader
19
{
20
        /** @internal cache structure */
21
        private const
22
                MetaCallbacks = 'callbacks',
23
                MetaData = 'data',
24
                MetaDelta = 'delta';
25

26
        public function __construct(
12✔
27
                private readonly CacheInterface $cache,
28
                private readonly int|null $maxTtl = null,
29
        ) {}
12✔
30

31
        /**
32
         * @throws InvalidArgumentException
33
         */
34
        public function read(string $key): mixed
4✔
35
        {
36
                /** @var array{data:string,delta:int,callbacks:list<array{0: callable(mixed...): bool, 1?: mixed, 2?: mixed}>}|null $meta */
37
                $meta = $this->cache->get($key);
4✔
38
                if (!$meta) {
4✔
39
                        return null;
1✔
40
                }
41

42
                if (!empty($meta[self::MetaCallbacks]) && !Cache::checkCallbacks($meta[self::MetaCallbacks])) {
3✔
43
                        $this->cache->delete($key);
1✔
44
                        return null;
1✔
45
                }
46

47
                if (!empty($meta[self::MetaDelta])) {
2✔
48
                        $this->cache->set($key, $meta, $meta[self::MetaDelta] + time());
1✔
49
                }
50

51
                return $meta[self::MetaData];
2✔
52
        }
53

NEW
54
        public function lock(string $key): void {}
×
55

56
        /**
57
         * @throws InvalidArgumentException
58
         */
59
        public function write(string $key, mixed $data, array $dependencies): void
4✔
60
        {
61
                if (isset($dependencies[Cache::Items])) {
4✔
62
                        throw new Nette\NotSupportedException('Dependent items are not supported by PSR16.');
1✔
63
                }
64

65
                $meta = [
3✔
66
                        self::MetaData => $data,
3✔
67
                ];
3✔
68

69
                $expire = null;
3✔
70
                if (isset($dependencies[Cache::Expire])) {
3✔
71
                        $expire = (int) $dependencies[Cache::Expire];
2✔
72
                        $expireMax = ($this->maxTtl ? time() + $this->maxTtl : null);
2✔
73

74
                        if ($expireMax !== null && $expire > $expireMax) {
2✔
75
                                $expire = $expireMax;
1✔
76
                        }
77

78
                        if (!empty($dependencies[Cache::Sliding])) {
2✔
79
                                $meta[self::MetaDelta] = $expire; // sliding time
1✔
80
                        }
81
                }
82

83
                if (isset($dependencies[Cache::Callbacks])) {
3✔
84
                        $meta[self::MetaCallbacks] = $dependencies[Cache::Callbacks];
1✔
85
                }
86

87
                if (isset($dependencies[Cache::Tags]) || isset($dependencies[Cache::Priority])) {
3✔
88
                        throw new Nette\InvalidStateException('PSR16 cache does not support Journal.');
1✔
89
                }
90

91
                $this->cache->set($key, $meta, $expire);
2✔
92
        }
93

94
        /**
95
         * @throws InvalidArgumentException
96
         */
97
        public function remove(string $key): void
1✔
98
        {
99
                $this->cache->delete($key);
1✔
100
        }
101

102
        public function clean(array $conditions): void
1✔
103
        {
104
                if (!empty($conditions[Cache::All])) {
1✔
105
                        $this->cache->clear();
1✔
106
                }
107
        }
108

109
        /**
110
         * @param array<array-key, mixed> $keys
111
         * @throws InvalidArgumentException
112
         */
113
        public function bulkRead(array $keys): array
2✔
114
        {
115
                $strKeys = array_map(static fn($key): string => (string) $key, $keys);
2✔
116
                $keys = array_combine($strKeys, $keys);
2✔
117

118
                $metas = $this->cache->getMultiple($strKeys);
2✔
119
                $result = [];
2✔
120
                $deleteKeys = [];
2✔
121
                foreach ($metas as $strKey => $meta) {
2✔
122
                        /** @var array{data:string,delta:int,callbacks:list<array{0: callable(mixed...): bool, 1?: mixed, 2?: mixed}>}|null $meta */
123
                        if (!empty($meta[self::MetaCallbacks]) && !Cache::checkCallbacks($meta[self::MetaCallbacks])) {
2✔
124
                                $deleteKeys[] = $strKey;
1✔
125
                        } else {
126
                                $result[$keys[$strKey]] = $meta[self::MetaData] ?? null;
2✔
127
                        }
128

129
                        if (!empty($meta[self::MetaDelta])) {
2✔
130
                                $this->cache->set($strKey, $meta, $meta[self::MetaDelta] + time());
1✔
131
                        }
132
                }
133

134
                if (!empty($deleteKeys)) {
2✔
135
                        $this->cache->deleteMultiple($deleteKeys);
1✔
136
                }
137

138
                return $result;
2✔
139
        }
140
}
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