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

nette / caching / 8225054646

10 Mar 2024 10:02PM UTC coverage: 87.869% (-0.02%) from 87.888%
8225054646

push

github

dg
Bulk write implementation (#73)

30 of 41 new or added lines in 2 files covered. (73.17%)

6 existing lines in 2 files now uncovered.

565 of 643 relevant lines covered (87.87%)

0.88 hits per line

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

92.2
/src/Caching/Cache.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;
11

12
use Nette;
13

14

15
/**
16
 * Implements the cache for a application.
17
 */
18
class Cache
19
{
20
        /** dependency */
21
        public const
22
                Priority = 'priority',
23
                Expire = 'expire',
24
                Sliding = 'sliding',
25
                Tags = 'tags',
26
                Files = 'files',
27
                Items = 'items',
28
                Constants = 'consts',
29
                Callbacks = 'callbacks',
30
                Namespaces = 'namespaces',
31
                All = 'all';
32

33
        /** @deprecated use Cache::Priority */
34
        public const PRIORITY = self::Priority;
35

36
        /** @deprecated use Cache::Expire */
37
        public const EXPIRATION = self::Expire;
38

39
        /** @deprecated use Cache::Expire */
40
        public const EXPIRE = self::Expire;
41

42
        /** @deprecated use Cache::Sliding */
43
        public const SLIDING = self::Sliding;
44

45
        /** @deprecated use Cache::Tags */
46
        public const TAGS = self::Tags;
47

48
        /** @deprecated use Cache::Files */
49
        public const FILES = self::Files;
50

51
        /** @deprecated use Cache::Items */
52
        public const ITEMS = self::Items;
53

54
        /** @deprecated use Cache::Constants */
55
        public const CONSTS = self::Constants;
56

57
        /** @deprecated use Cache::Callbacks */
58
        public const CALLBACKS = self::Callbacks;
59

60
        /** @deprecated use Cache::Namespaces */
61
        public const NAMESPACES = self::Namespaces;
62

63
        /** @deprecated use Cache::All */
64
        public const ALL = self::All;
65

66
        /** @internal */
67
        public const
68
                NamespaceSeparator = "\x00",
69
                NAMESPACE_SEPARATOR = self::NamespaceSeparator;
70

71
        private Storage $storage;
72
        private string $namespace;
73

74

75
        public function __construct(Storage $storage, ?string $namespace = null)
1✔
76
        {
77
                $this->storage = $storage;
1✔
78
                $this->namespace = $namespace . self::NamespaceSeparator;
1✔
79
        }
1✔
80

81

82
        /**
83
         * Returns cache storage.
84
         */
85
        final public function getStorage(): Storage
86
        {
87
                return $this->storage;
×
88
        }
89

90

91
        /**
92
         * Returns cache namespace.
93
         */
94
        final public function getNamespace(): string
95
        {
96
                return substr($this->namespace, 0, -1);
1✔
97
        }
98

99

100
        /**
101
         * Returns new nested cache object.
102
         */
103
        public function derive(string $namespace): static
1✔
104
        {
105
                return new static($this->storage, $this->namespace . $namespace);
1✔
106
        }
107

108

109
        /**
110
         * Reads the specified item from the cache or generate it.
111
         */
112
        public function load(mixed $key, ?callable $generator = null, ?array $dependencies = null): mixed
1✔
113
        {
114
                $storageKey = $this->generateKey($key);
1✔
115
                $data = $this->storage->read($storageKey);
1✔
116
                if ($data === null && $generator) {
1✔
117
                        $this->storage->lock($storageKey);
1✔
118
                        try {
119
                                $data = $generator(...[&$dependencies]);
1✔
120
                        } catch (\Throwable $e) {
1✔
121
                                $this->storage->remove($storageKey);
1✔
122
                                throw $e;
1✔
123
                        }
124

125
                        $this->save($key, $data, $dependencies);
1✔
126
                }
127

128
                return $data;
1✔
129
        }
130

131

132
        /**
133
         * Reads multiple items from the cache.
134
         */
135
        public function bulkLoad(array $keys, ?callable $generator = null): array
1✔
136
        {
137
                if (count($keys) === 0) {
1✔
138
                        return [];
×
139
                }
140

141
                foreach ($keys as $key) {
1✔
142
                        if (!is_scalar($key)) {
1✔
143
                                throw new Nette\InvalidArgumentException('Only scalar keys are allowed in bulkLoad()');
1✔
144
                        }
145
                }
146

147
                $result = [];
1✔
148
                if (!$this->storage instanceof BulkReader) {
1✔
149
                        foreach ($keys as $key) {
1✔
150
                                $result[$key] = $this->load(
1✔
151
                                        $key,
1✔
152
                                        $generator
1✔
153
                                                ? fn(&$dependencies) => $generator(...[$key, &$dependencies])
1✔
154
                                                : null,
1✔
155
                                );
156
                        }
157

158
                        return $result;
1✔
159
                }
160

161
                $storageKeys = array_map([$this, 'generateKey'], $keys);
1✔
162
                $cacheData = $this->storage->bulkRead($storageKeys);
1✔
163
                foreach ($keys as $i => $key) {
1✔
164
                        $storageKey = $storageKeys[$i];
1✔
165
                        if (isset($cacheData[$storageKey])) {
1✔
166
                                $result[$key] = $cacheData[$storageKey];
1✔
167
                        } elseif ($generator) {
1✔
168
                                $result[$key] = $this->load($key, fn(&$dependencies) => $generator(...[$key, &$dependencies]));
1✔
169
                        } else {
170
                                $result[$key] = null;
1✔
171
                        }
172
                }
173

174
                return $result;
1✔
175
        }
176

177

178
        /**
179
         * Writes item into the cache.
180
         * Dependencies are:
181
         * - Cache::Priority => (int) priority
182
         * - Cache::Expire => (timestamp) expiration, infinite if null
183
         * - Cache::Sliding => (bool) use sliding expiration?
184
         * - Cache::Tags => (array) tags
185
         * - Cache::Files => (array|string) file names
186
         * - Cache::Items => (array|string) cache items
187
         * - Cache::Constants => (array|string) cache items
188
         * @return mixed  value itself
189
         * @throws Nette\InvalidArgumentException
190
         */
191
        public function save(mixed $key, mixed $data, ?array $dependencies = null): mixed
1✔
192
        {
193
                $key = $this->generateKey($key);
1✔
194

195
                if ($data instanceof \Closure) {
1✔
196
                        $this->storage->lock($key);
1✔
197
                        try {
198
                                $data = $data(...[&$dependencies]);
1✔
UNCOV
199
                        } catch (\Throwable $e) {
×
UNCOV
200
                                $this->storage->remove($key);
×
UNCOV
201
                                throw $e;
×
202
                        }
203
                }
204

205
                if ($data === null) {
1✔
206
                        $this->storage->remove($key);
1✔
207
                        return null;
1✔
208
                } else {
209
                        $dependencies = $this->completeDependencies($dependencies);
1✔
210
                        if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
1✔
211
                                $this->storage->remove($key);
1✔
212
                        } else {
213
                                $this->storage->write($key, $data, $dependencies);
1✔
214
                        }
215

216
                        return $data;
1✔
217
                }
218
        }
219

220

221
        /**
222
         * Writes multiple items into cache
223
         */
224
        public function bulkSave(array $items, ?array $dependencies = null): void
1✔
225
        {
226
                $write = $remove = [];
1✔
227

228
                if (!$this->storage instanceof BulkWriter) {
1✔
229
                        foreach ($items as $key => $data) {
1✔
230
                                $this->save($key, $data, $dependencies);
1✔
231
                        }
232
                        return;
1✔
233
                }
234

235
                $dependencies = $this->completeDependencies($dependencies);
1✔
236
                if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
1✔
NEW
237
                        $this->storage->bulkRemove(array_map(fn($key) => $this->generateKey($key), array_keys($items)));
×
NEW
238
                        return;
×
239
                }
240

241
                foreach ($items as $key => $data) {
1✔
242
                        $key = $this->generateKey($key);
1✔
243
                        if ($data === null) {
1✔
NEW
244
                                $remove[] = $key;
×
245
                        } else {
246
                                $write[$key] = $data;
1✔
247
                        }
248
                }
249

250
                if ($remove) {
1✔
NEW
251
                        $this->storage->bulkRemove($remove);
×
252
                }
253

254
                if ($write) {
1✔
255
                        $this->storage->bulkWrite($write, $dependencies);
1✔
256
                }
257
        }
1✔
258

259

260
        private function completeDependencies(?array $dp): array
1✔
261
        {
262
                // convert expire into relative amount of seconds
263
                if (isset($dp[self::Expire])) {
1✔
264
                        $dp[self::Expire] = Nette\Utils\DateTime::from($dp[self::Expire])->format('U') - time();
1✔
265
                }
266

267
                // make list from TAGS
268
                if (isset($dp[self::Tags])) {
1✔
269
                        $dp[self::Tags] = array_values((array) $dp[self::Tags]);
1✔
270
                }
271

272
                // make list from NAMESPACES
273
                if (isset($dp[self::Namespaces])) {
1✔
UNCOV
274
                        $dp[self::Namespaces] = array_values((array) $dp[self::Namespaces]);
×
275
                }
276

277
                // convert FILES into CALLBACKS
278
                if (isset($dp[self::Files])) {
1✔
279
                        foreach (array_unique((array) $dp[self::Files]) as $item) {
1✔
280
                                $dp[self::Callbacks][] = [[self::class, 'checkFile'], $item, @filemtime($item) ?: null]; // @ - stat may fail
1✔
281
                        }
282

283
                        unset($dp[self::Files]);
1✔
284
                }
285

286
                // add namespaces to items
287
                if (isset($dp[self::Items])) {
1✔
288
                        $dp[self::Items] = array_unique(array_map([$this, 'generateKey'], (array) $dp[self::Items]));
1✔
289
                }
290

291
                // convert CONSTS into CALLBACKS
292
                if (isset($dp[self::Constants])) {
1✔
293
                        foreach (array_unique((array) $dp[self::Constants]) as $item) {
1✔
294
                                $dp[self::Callbacks][] = [[self::class, 'checkConst'], $item, constant($item)];
1✔
295
                        }
296

297
                        unset($dp[self::Constants]);
1✔
298
                }
299

300
                if (!is_array($dp)) {
1✔
301
                        $dp = [];
1✔
302
                }
303

304
                return $dp;
1✔
305
        }
306

307

308
        /**
309
         * Removes item from the cache.
310
         */
311
        public function remove(mixed $key): void
1✔
312
        {
313
                $this->save($key, null);
1✔
314
        }
1✔
315

316

317
        /**
318
         * Removes items from the cache by conditions.
319
         * Conditions are:
320
         * - Cache::Priority => (int) priority
321
         * - Cache::Tags => (array) tags
322
         * - Cache::All => true
323
         */
324
        public function clean(?array $conditions = null): void
1✔
325
        {
326
                $conditions = (array) $conditions;
1✔
327
                if (isset($conditions[self::Tags])) {
1✔
328
                        $conditions[self::Tags] = array_values((array) $conditions[self::Tags]);
1✔
329
                }
330

331
                $this->storage->clean($conditions);
1✔
332
        }
1✔
333

334

335
        /**
336
         * Caches results of function/method calls.
337
         */
338
        public function call(callable $function): mixed
1✔
339
        {
340
                $key = func_get_args();
1✔
341
                if (is_array($function) && is_object($function[0])) {
1✔
342
                        $key[0][0] = get_class($function[0]);
1✔
343
                }
344

345
                return $this->load($key, fn() => $function(...array_slice($key, 1)));
1✔
346
        }
347

348

349
        /**
350
         * Caches results of function/method calls.
351
         */
352
        public function wrap(callable $function, ?array $dependencies = null): \Closure
1✔
353
        {
354
                return function () use ($function, $dependencies) {
1✔
355
                        $key = [$function, $args = func_get_args()];
1✔
356
                        if (is_array($function) && is_object($function[0])) {
1✔
357
                                $key[0][0] = get_class($function[0]);
1✔
358
                        }
359

360
                        return $this->load($key, function (&$deps) use ($function, $args, $dependencies) {
1✔
361
                                $deps = $dependencies;
1✔
362
                                return $function(...$args);
1✔
363
                        });
1✔
364
                };
1✔
365
        }
366

367

368
        /**
369
         * Starts the output cache.
370
         */
371
        public function capture(mixed $key): ?OutputHelper
1✔
372
        {
373
                $data = $this->load($key);
1✔
374
                if ($data === null) {
1✔
375
                        return new OutputHelper($this, $key);
1✔
376
                }
377

378
                echo $data;
1✔
379
                return null;
1✔
380
        }
381

382

383
        /**
384
         * @deprecated  use capture()
385
         */
386
        public function start($key): ?OutputHelper
387
        {
UNCOV
388
                return $this->capture($key);
×
389
        }
390

391

392
        /**
393
         * Generates internal cache key.
394
         */
395
        protected function generateKey($key): string
396
        {
397
                return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key));
1✔
398
        }
399

400

401
        /********************* dependency checkers ****************d*g**/
402

403

404
        /**
405
         * Checks CALLBACKS dependencies.
406
         */
407
        public static function checkCallbacks(array $callbacks): bool
1✔
408
        {
409
                foreach ($callbacks as $callback) {
1✔
410
                        if (!array_shift($callback)(...$callback)) {
1✔
411
                                return false;
1✔
412
                        }
413
                }
414

415
                return true;
1✔
416
        }
417

418

419
        /**
420
         * Checks CONSTS dependency.
421
         */
422
        private static function checkConst(string $const, $value): bool
1✔
423
        {
424
                return defined($const) && constant($const) === $value;
1✔
425
        }
426

427

428
        /**
429
         * Checks FILES dependency.
430
         */
431
        private static function checkFile(string $file, ?int $time): bool
1✔
432
        {
433
                return @filemtime($file) == $time; // @ - stat may fail
1✔
434
        }
435
}
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