• 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

94.34
/src/Caching/Storages/SQLiteJournal.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 count, extension_loaded, implode, is_file, str_repeat, touch;
15

16

17
/**
18
 * SQLite based journal.
19
 */
20
class SQLiteJournal implements Journal
21
{
22
        private \PDO $pdo;
23

24

25
        public function __construct(
1✔
26
                private readonly string $path,
27
        ) {
28
                if (!extension_loaded('pdo_sqlite')) {
1✔
UNCOV
29
                        throw new Nette\NotSupportedException('SQLiteJournal requires PHP extension pdo_sqlite which is not loaded.');
×
30
                }
31
        }
1✔
32

33

34
        private function open(): void
35
        {
36
                if ($this->path !== ':memory:' && !is_file($this->path)) {
1✔
37
                        touch($this->path); // ensures ordinary file permissions
1✔
38
                }
39

40
                $this->pdo = new \PDO('sqlite:' . $this->path);
1✔
41
                $this->pdo->exec('
1✔
42
                        PRAGMA foreign_keys = OFF;
43
                        PRAGMA journal_mode = WAL;
44
                        CREATE TABLE IF NOT EXISTS tags (
45
                                key BLOB NOT NULL,
46
                                tag BLOB NOT NULL
47
                        );
48
                        CREATE TABLE IF NOT EXISTS priorities (
49
                                key BLOB NOT NULL,
50
                                priority INT NOT NULL
51
                        );
52
                        CREATE INDEX IF NOT EXISTS idx_tags_tag ON tags(tag);
53
                        CREATE UNIQUE INDEX IF NOT EXISTS idx_tags_key_tag ON tags(key, tag);
54
                        CREATE UNIQUE INDEX IF NOT EXISTS idx_priorities_key ON priorities(key);
55
                        CREATE INDEX IF NOT EXISTS idx_priorities_priority ON priorities(priority);
56
                        PRAGMA synchronous = NORMAL;
57
                ');
58
        }
1✔
59

60

61
        public function write(string $key, array $dependencies): void
1✔
62
        {
63
                if (!isset($this->pdo)) {
1✔
64
                        $this->open();
1✔
65
                }
66

67
                $this->pdo->exec('BEGIN');
1✔
68

69
                if (!empty($dependencies[Cache::Tags])) {
1✔
70
                        $this->pdo->prepare('DELETE FROM tags WHERE key = ?')->execute([$key]);
1✔
71

72
                        foreach ($dependencies[Cache::Tags] as $tag) {
1✔
73
                                $arr[] = $key;
1✔
74
                                $arr[] = $tag;
1✔
75
                        }
76

77
                        $this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
1✔
78
                                ->execute($arr);
1✔
79
                }
80

81
                if (!empty($dependencies[Cache::Priority])) {
1✔
82
                        $this->pdo->prepare('REPLACE INTO priorities (key, priority) VALUES (?, ?)')
1✔
83
                                ->execute([$key, (int) $dependencies[Cache::Priority]]);
1✔
84
                }
85

86
                $this->pdo->exec('COMMIT');
1✔
87
        }
1✔
88

89

90
        public function clean(array $conditions): ?array
1✔
91
        {
92
                if (!isset($this->pdo)) {
1✔
UNCOV
93
                        $this->open();
×
94
                }
95

96
                if (!empty($conditions[Cache::All])) {
1✔
97
                        $this->pdo->exec('
1✔
98
                                BEGIN;
99
                                DELETE FROM tags;
100
                                DELETE FROM priorities;
101
                                COMMIT;
102
                        ');
103

104
                        return null;
1✔
105
                }
106

107
                $unions = $args = [];
1✔
108
                if (!empty($conditions[Cache::Tags])) {
1✔
109
                        $tags = (array) $conditions[Cache::Tags];
1✔
110
                        $unions[] = 'SELECT DISTINCT key FROM tags WHERE tag IN (?' . str_repeat(', ?', count($tags) - 1) . ')';
1✔
111
                        $args = $tags;
1✔
112
                }
113

114
                if (!empty($conditions[Cache::Priority])) {
1✔
115
                        $unions[] = 'SELECT DISTINCT key FROM priorities WHERE priority <= ?';
1✔
116
                        $args[] = (int) $conditions[Cache::Priority];
1✔
117
                }
118

119
                if (empty($unions)) {
1✔
UNCOV
120
                        return [];
×
121
                }
122

123
                $unionSql = implode(' UNION ', $unions);
1✔
124

125
                $this->pdo->exec('BEGIN IMMEDIATE');
1✔
126

127
                $stmt = $this->pdo->prepare($unionSql);
1✔
128
                $stmt->execute($args);
1✔
129
                $keys = $stmt->fetchAll(\PDO::FETCH_COLUMN, 0);
1✔
130

131
                if (empty($keys)) {
1✔
132
                        $this->pdo->exec('COMMIT');
1✔
133
                        return [];
1✔
134
                }
135

136
                $this->pdo->prepare("DELETE FROM tags WHERE key IN ($unionSql)")->execute($args);
1✔
137
                $this->pdo->prepare("DELETE FROM priorities WHERE key IN ($unionSql)")->execute($args);
1✔
138
                $this->pdo->exec('COMMIT');
1✔
139

140
                return $keys;
1✔
141
        }
142
}
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