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

nette / caching / 6311829630

26 Sep 2023 11:12AM UTC coverage: 88.889%. Remained the same
6311829630

push

github

dg
removed Nette\SmartObject

536 of 603 relevant lines covered (88.89%)

0.89 hits per line

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

98.36
/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->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
1✔
32
                $this->pdo->exec('
1✔
33
                        PRAGMA foreign_keys = ON;
34
                        CREATE TABLE IF NOT EXISTS cache (
35
                                key BLOB NOT NULL PRIMARY KEY,
36
                                data BLOB NOT NULL,
37
                                expire INTEGER,
38
                                slide INTEGER
39
                        );
40
                        CREATE TABLE IF NOT EXISTS tags (
41
                                key BLOB NOT NULL REFERENCES cache ON DELETE CASCADE,
42
                                tag BLOB NOT NULL
43
                        );
44
                        CREATE INDEX IF NOT EXISTS cache_expire ON cache(expire);
45
                        CREATE INDEX IF NOT EXISTS tags_key ON tags(key);
46
                        CREATE INDEX IF NOT EXISTS tags_tag ON tags(tag);
47
                        PRAGMA synchronous = OFF;
48
                ');
49
        }
1✔
50

51

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

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

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

67

68
        public function bulkRead(array $keys): array
1✔
69
        {
70
                $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✔
71
                $stmt->execute(array_merge($keys, [time()]));
1✔
72
                $result = [];
1✔
73
                $updateSlide = [];
1✔
74
                foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
1✔
75
                        if ($row['slide'] !== null) {
1✔
76
                                $updateSlide[] = $row['key'];
1✔
77
                        }
78

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

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

87
                return $result;
1✔
88
        }
89

90

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

95

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

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

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

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

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

122

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

129

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

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

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

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