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

nette / caching / 8224967934

10 Mar 2024 09:44PM UTC coverage: 87.888% (-1.0%) from 88.889%
8224967934

push

github

dg
Bulk write implementation (#73)

31 of 42 new or added lines in 2 files covered. (73.81%)

1 existing line in 1 file now uncovered.

566 of 644 relevant lines covered (87.89%)

0.88 hits per line

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

92.25
/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 multiple items into cache
180
         *
181
         * @param array $items
182
         * @param array|null $dependencies
183
         * @return array Stored items
184
         *
185
         * @throws InvalidArgumentException
186
         */
187
        public function bulkSave(array $items, ?array $dependencies = null): array
1✔
188
        {
189
                $storedItems = [];
1✔
190

191
                if (!$this->storage instanceof BulkWriter) {
1✔
192

193
                        foreach ($items as $key => $data) {
1✔
194
                                $storedItems[$key] = $this->save($key, $data, $dependencies);
1✔
195
                        }
196
                        return $storedItems;
1✔
197
                }
198

199
                $dependencies = $this->completeDependencies($dependencies);
1✔
200

201
                if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
1✔
NEW
202
                        $this->storage->bulkRemove(array_map(fn($key): string => $this->generateKey($key), array_keys($items)));
×
NEW
203
                        return [];
×
204
                }
205

206
                $removals = [];
1✔
207
                $toCache = [];
1✔
208
                foreach ($items as $key => $data) {
1✔
209
                        $cKey = $this->generateKey($key);
1✔
210

211
                        if ($data === null) {
1✔
NEW
212
                                $removals[] = $cKey;
×
213
                        } else {
214
                                $storedItems[$key] = $toCache[$cKey] = $data;
1✔
215
                        }
216
                }
217

218
                if (!empty($removals)) {
1✔
NEW
219
                        $this->storage->bulkRemove($removals);
×
220
                }
221

222
                $this->storage->bulkWrite($toCache, $dependencies);
1✔
223

224
                return $storedItems;
1✔
225
        }
226

227

228
        /**
229
         * Writes item into the cache.
230
         * Dependencies are:
231
         * - Cache::Priority => (int) priority
232
         * - Cache::Expire => (timestamp) expiration, infinite if null
233
         * - Cache::Sliding => (bool) use sliding expiration?
234
         * - Cache::Tags => (array) tags
235
         * - Cache::Files => (array|string) file names
236
         * - Cache::Items => (array|string) cache items
237
         * - Cache::Constants => (array|string) cache items
238
         * @return mixed  value itself
239
         * @throws Nette\InvalidArgumentException
240
         */
241
        public function save(mixed $key, mixed $data, ?array $dependencies = null): mixed
1✔
242
        {
243
                $key = $this->generateKey($key);
1✔
244

245
                if ($data instanceof \Closure) {
1✔
246
                        $this->storage->lock($key);
1✔
247
                        try {
248
                                $data = $data(...[&$dependencies]);
1✔
249
                        } catch (\Throwable $e) {
×
250
                                $this->storage->remove($key);
×
251
                                throw $e;
×
252
                        }
253
                }
254

255
                if ($data === null) {
1✔
256
                        $this->storage->remove($key);
1✔
257
                        return null;
1✔
258
                } else {
259
                        $dependencies = $this->completeDependencies($dependencies);
1✔
260
                        if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
1✔
261
                                $this->storage->remove($key);
1✔
262
                        } else {
263
                                $this->storage->write($key, $data, $dependencies);
1✔
264
                        }
265

266
                        return $data;
1✔
267
                }
268
        }
269

270

271
        private function completeDependencies(?array $dp): array
1✔
272
        {
273
                // convert expire into relative amount of seconds
274
                if (isset($dp[self::Expire])) {
1✔
275
                        $dp[self::Expire] = Nette\Utils\DateTime::from($dp[self::Expire])->format('U') - time();
1✔
276
                }
277

278
                // make list from TAGS
279
                if (isset($dp[self::Tags])) {
1✔
280
                        $dp[self::Tags] = array_values((array) $dp[self::Tags]);
1✔
281
                }
282

283
                // make list from NAMESPACES
284
                if (isset($dp[self::Namespaces])) {
1✔
285
                        $dp[self::Namespaces] = array_values((array) $dp[self::Namespaces]);
×
286
                }
287

288
                // convert FILES into CALLBACKS
289
                if (isset($dp[self::Files])) {
1✔
290
                        foreach (array_unique((array) $dp[self::Files]) as $item) {
1✔
291
                                $dp[self::Callbacks][] = [[self::class, 'checkFile'], $item, @filemtime($item) ?: null]; // @ - stat may fail
1✔
292
                        }
293

294
                        unset($dp[self::Files]);
1✔
295
                }
296

297
                // add namespaces to items
298
                if (isset($dp[self::Items])) {
1✔
299
                        $dp[self::Items] = array_unique(array_map([$this, 'generateKey'], (array) $dp[self::Items]));
1✔
300
                }
301

302
                // convert CONSTS into CALLBACKS
303
                if (isset($dp[self::Constants])) {
1✔
304
                        foreach (array_unique((array) $dp[self::Constants]) as $item) {
1✔
305
                                $dp[self::Callbacks][] = [[self::class, 'checkConst'], $item, constant($item)];
1✔
306
                        }
307

308
                        unset($dp[self::Constants]);
1✔
309
                }
310

311
                if (!is_array($dp)) {
1✔
312
                        $dp = [];
1✔
313
                }
314

315
                return $dp;
1✔
316
        }
317

318

319
        /**
320
         * Removes item from the cache.
321
         */
322
        public function remove(mixed $key): void
1✔
323
        {
324
                $this->save($key, null);
1✔
325
        }
1✔
326

327

328
        /**
329
         * Removes items from the cache by conditions.
330
         * Conditions are:
331
         * - Cache::Priority => (int) priority
332
         * - Cache::Tags => (array) tags
333
         * - Cache::All => true
334
         */
335
        public function clean(?array $conditions = null): void
1✔
336
        {
337
                $conditions = (array) $conditions;
1✔
338
                if (isset($conditions[self::Tags])) {
1✔
339
                        $conditions[self::Tags] = array_values((array) $conditions[self::Tags]);
1✔
340
                }
341

342
                $this->storage->clean($conditions);
1✔
343
        }
1✔
344

345

346
        /**
347
         * Caches results of function/method calls.
348
         */
349
        public function call(callable $function): mixed
1✔
350
        {
351
                $key = func_get_args();
1✔
352
                if (is_array($function) && is_object($function[0])) {
1✔
353
                        $key[0][0] = get_class($function[0]);
1✔
354
                }
355

356
                return $this->load($key, fn() => $function(...array_slice($key, 1)));
1✔
357
        }
358

359

360
        /**
361
         * Caches results of function/method calls.
362
         */
363
        public function wrap(callable $function, ?array $dependencies = null): \Closure
1✔
364
        {
365
                return function () use ($function, $dependencies) {
1✔
366
                        $key = [$function, $args = func_get_args()];
1✔
367
                        if (is_array($function) && is_object($function[0])) {
1✔
368
                                $key[0][0] = get_class($function[0]);
1✔
369
                        }
370

371
                        return $this->load($key, function (&$deps) use ($function, $args, $dependencies) {
1✔
372
                                $deps = $dependencies;
1✔
373
                                return $function(...$args);
1✔
374
                        });
1✔
375
                };
1✔
376
        }
377

378

379
        /**
380
         * Starts the output cache.
381
         */
382
        public function capture(mixed $key): ?OutputHelper
1✔
383
        {
384
                $data = $this->load($key);
1✔
385
                if ($data === null) {
1✔
386
                        return new OutputHelper($this, $key);
1✔
387
                }
388

389
                echo $data;
1✔
390
                return null;
1✔
391
        }
392

393

394
        /**
395
         * @deprecated  use capture()
396
         */
397
        public function start($key): ?OutputHelper
398
        {
399
                return $this->capture($key);
×
400
        }
401

402

403
        /**
404
         * Generates internal cache key.
405
         */
406
        protected function generateKey($key): string
407
        {
408
                return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key));
1✔
409
        }
410

411

412
        /********************* dependency checkers ****************d*g**/
413

414

415
        /**
416
         * Checks CALLBACKS dependencies.
417
         */
418
        public static function checkCallbacks(array $callbacks): bool
1✔
419
        {
420
                foreach ($callbacks as $callback) {
1✔
421
                        if (!array_shift($callback)(...$callback)) {
1✔
422
                                return false;
1✔
423
                        }
424
                }
425

426
                return true;
1✔
427
        }
428

429

430
        /**
431
         * Checks CONSTS dependency.
432
         */
433
        private static function checkConst(string $const, $value): bool
1✔
434
        {
435
                return defined($const) && constant($const) === $value;
1✔
436
        }
437

438

439
        /**
440
         * Checks FILES dependency.
441
         */
442
        private static function checkFile(string $file, ?int $time): bool
1✔
443
        {
444
                return @filemtime($file) == $time; // @ - stat may fail
1✔
445
        }
446
}
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