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

nette / caching / 16790525896

06 Aug 2025 10:53PM UTC coverage: 87.518%. Remained the same
16790525896

push

github

dg
cs

596 of 681 relevant lines covered (87.52%)

0.88 hits per line

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

98.33
/src/Caching/Storages/SQLiteStorage.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

15

16
/**
17
 * SQLite storage.
18
 */
19
class SQLiteStorage implements Nette\Caching\Storage, Nette\Caching\BulkReader
20
{
21
        private \PDO $pdo;
22

23

24
        public function __construct(string $path)
1✔
25
        {
26
                if ($path !== ':memory:' && !is_file($path)) {
1✔
27
                        touch($path); // ensures ordinary file permissions
1✔
28
                }
29

30
                $this->pdo = new \PDO('sqlite:' . $path);
1✔
31
                $this->pdo->exec('
1✔
32
                        PRAGMA foreign_keys = ON;
33
                        CREATE TABLE IF NOT EXISTS cache (
34
                                key BLOB NOT NULL PRIMARY KEY,
35
                                data BLOB NOT NULL,
36
                                expire INTEGER,
37
                                slide INTEGER
38
                        );
39
                        CREATE TABLE IF NOT EXISTS tags (
40
                                key BLOB NOT NULL REFERENCES cache ON DELETE CASCADE,
41
                                tag BLOB NOT NULL
42
                        );
43
                        CREATE INDEX IF NOT EXISTS cache_expire ON cache(expire);
44
                        CREATE INDEX IF NOT EXISTS tags_key ON tags(key);
45
                        CREATE INDEX IF NOT EXISTS tags_tag ON tags(tag);
46
                        PRAGMA synchronous = OFF;
47
                ');
48
        }
1✔
49

50

51
        public function read(string $key): mixed
1✔
52
        {
53
                $stmt = $this->pdo->prepare('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?)');
1✔
54
                $stmt->execute([$key, time()]);
1✔
55
                if (!$row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1✔
56
                        return null;
1✔
57
                }
58

59
                if ($row['slide'] !== null) {
1✔
60
                        $this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key=?')->execute([time(), $key]);
1✔
61
                }
62

63
                return unserialize($row['data']);
1✔
64
        }
65

66

67
        public function bulkRead(array $keys): array
1✔
68
        {
69
                $stmt = $this->pdo->prepare('SELECT key, data, slide FROM cache WHERE key IN (?' . str_repeat(',?', count($keys) - 1) . ') AND (expire IS NULL OR expire >= ?)');
1✔
70
                $stmt->execute(array_merge($keys, [time()]));
1✔
71
                $result = [];
1✔
72
                $updateSlide = [];
1✔
73
                foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
1✔
74
                        if ($row['slide'] !== null) {
1✔
75
                                $updateSlide[] = $row['key'];
1✔
76
                        }
77

78
                        $result[$row['key']] = unserialize($row['data']);
1✔
79
                }
80

81
                if (!empty($updateSlide)) {
1✔
82
                        $stmt = $this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key IN(?' . str_repeat(',?', count($updateSlide) - 1) . ')');
1✔
83
                        $stmt->execute(array_merge([time()], $updateSlide));
1✔
84
                }
85

86
                return $result;
1✔
87
        }
88

89

90
        public function lock(string $key): void
91
        {
92
        }
93

94

95
        public function write(string $key, $data, array $dependencies): void
1✔
96
        {
97
                $expire = isset($dependencies[Cache::Expire])
1✔
98
                        ? $dependencies[Cache::Expire] + time()
1✔
99
                        : null;
1✔
100
                $slide = isset($dependencies[Cache::Sliding])
1✔
101
                        ? $dependencies[Cache::Expire]
1✔
102
                        : null;
1✔
103

104
                $this->pdo->exec('BEGIN TRANSACTION');
1✔
105
                $this->pdo->prepare('REPLACE INTO cache (key, data, expire, slide) VALUES (?, ?, ?, ?)')
1✔
106
                        ->execute([$key, serialize($data), $expire, $slide]);
1✔
107

108
                if (!empty($dependencies[Cache::Tags])) {
1✔
109
                        foreach ($dependencies[Cache::Tags] as $tag) {
1✔
110
                                $arr[] = $key;
1✔
111
                                $arr[] = $tag;
1✔
112
                        }
113

114
                        $this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
1✔
115
                                ->execute($arr);
1✔
116
                }
117

118
                $this->pdo->exec('COMMIT');
1✔
119
        }
1✔
120

121

122
        public function remove(string $key): void
1✔
123
        {
124
                $this->pdo->prepare('DELETE FROM cache WHERE key=?')
1✔
125
                        ->execute([$key]);
1✔
126
        }
1✔
127

128

129
        public function clean(array $conditions): void
1✔
130
        {
131
                if (!empty($conditions[Cache::All])) {
1✔
132
                        $this->pdo->prepare('DELETE FROM cache')->execute();
×
133

134
                } else {
135
                        $sql = 'DELETE FROM cache WHERE expire < ?';
1✔
136
                        $args = [time()];
1✔
137

138
                        if (!empty($conditions[Cache::Tags])) {
1✔
139
                                $tags = $conditions[Cache::Tags];
1✔
140
                                $sql .= ' OR key IN (SELECT key FROM tags WHERE tag IN (?' . str_repeat(',?', count($tags) - 1) . '))';
1✔
141
                                $args = array_merge($args, $tags);
1✔
142
                        }
143

144
                        $this->pdo->prepare($sql)->execute($args);
1✔
145
                }
146
        }
1✔
147
}
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