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

nette / caching / 27920089981

21 Jun 2026 10:53PM UTC coverage: 87.348% (+0.1%) from 87.202%
27920089981

push

github

dg
added CLAUDE.md

573 of 656 relevant lines covered (87.35%)

0.87 hits per line

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

78.79
/src/Bridges/Psr/PsrCacheAdapter.php
1
<?php declare(strict_types=1);
2

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

8
namespace Nette\Bridges\Psr;
9

10
use DateInterval;
11
use Nette;
12
use Psr;
13

14

15
/**
16
 * Adapts Nette Storage to the PSR-16 SimpleCache interface.
17
 */
18
class PsrCacheAdapter implements Psr\SimpleCache\CacheInterface
19
{
20
        public function __construct(
1✔
21
                private readonly Nette\Caching\Storage $storage,
22
        ) {
23
        }
1✔
24

25

26
        public function get(string $key, mixed $default = null): mixed
1✔
27
        {
28
                return $this->storage->read($key) ?? $default;
1✔
29
        }
30

31

32
        public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
1✔
33
        {
34
                $dependencies = [];
1✔
35
                if ($ttl !== null) {
1✔
36
                        $dependencies[Nette\Caching\Cache::Expire] = self::ttlToSeconds($ttl);
1✔
37
                }
38

39
                $this->storage->write($key, $value, $dependencies);
1✔
40

41
                return true;
1✔
42
        }
43

44

45
        public function delete(string $key): bool
46
        {
47
                $this->storage->remove($key);
×
48
                return true;
×
49
        }
50

51

52
        public function clear(): bool
53
        {
54
                $this->storage->clean([Nette\Caching\Cache::All => true]);
×
55
                return true;
×
56
        }
57

58

59
        /**
60
         * @return \Generator<string, mixed>
61
         */
62
        public function getMultiple(iterable $keys, mixed $default = null): \Generator
1✔
63
        {
64
                foreach ($keys as $name) {
1✔
65
                        yield $name => $this->get($name, $default);
1✔
66
                }
67
        }
1✔
68

69

70
        /**
71
         * @param iterable<mixed, mixed>  $values
72
         */
73
        public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
1✔
74
        {
75
                $ttl = self::ttlToSeconds($ttl);
1✔
76

77
                foreach ($values as $key => $value) {
1✔
78
                        $this->set((string) $key, $value, $ttl);
1✔
79
                }
80

81
                return true;
1✔
82
        }
83

84

85
        /** @param  iterable<string>  $keys */
86
        public function deleteMultiple(iterable $keys): bool
87
        {
88
                foreach ($keys as $value) {
×
89
                        $this->delete($value);
×
90
                }
91

92
                return true;
×
93
        }
94

95

96
        public function has(string $key): bool
1✔
97
        {
98
                return $this->storage->read($key) !== null;
1✔
99
        }
100

101

102
        private static function ttlToSeconds(null|int|DateInterval $ttl = null): ?int
1✔
103
        {
104
                if ($ttl instanceof DateInterval) {
1✔
105
                        $now = new \DateTimeImmutable('', new \DateTimeZone('UTC'));
1✔
106
                        return $now->add($ttl)->getTimestamp() - $now->getTimestamp();
1✔
107
                }
108

109
                return $ttl;
1✔
110
        }
111
}
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