|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Tracy (https://tracy.nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Bridges\Psr; |
| 11 | + |
| 12 | +use DateInterval; |
| 13 | +use Nette; |
| 14 | +use Psr; |
| 15 | + |
| 16 | + |
| 17 | +class PsrCacheAdapter implements Psr\SimpleCache\CacheInterface |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + private Nette\Caching\Storage $storage, |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + public function get(string $key, mixed $default = null): mixed |
| 26 | + { |
| 27 | + return $this->storage->read($key) ?? $default; |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool |
| 32 | + { |
| 33 | + $dependencies = []; |
| 34 | + if ($ttl !== null) { |
| 35 | + $dependencies[Nette\Caching\Cache::Expire] = self::ttlToSeconds($ttl); |
| 36 | + } |
| 37 | + |
| 38 | + $this->storage->write($key, $value, $dependencies); |
| 39 | + |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public function delete(string $key): bool |
| 45 | + { |
| 46 | + $this->storage->remove($key); |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public function clear(): bool |
| 52 | + { |
| 53 | + $this->storage->clean([Nette\Caching\Cache::All => true]); |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * @return \Generator<string, mixed> |
| 60 | + */ |
| 61 | + public function getMultiple(iterable $keys, mixed $default = null): \Generator |
| 62 | + { |
| 63 | + foreach ($keys as $name) { |
| 64 | + yield $name => $this->get($name, $default); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * @param iterable<string|int, mixed> $values |
| 71 | + */ |
| 72 | + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
| 73 | + { |
| 74 | + $ttl = self::ttlToSeconds($ttl); |
| 75 | + |
| 76 | + foreach ($values as $key => $value) { |
| 77 | + $this->set((string) $key, $value, $ttl); |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public function deleteMultiple(iterable $keys): bool |
| 85 | + { |
| 86 | + foreach ($keys as $value) { |
| 87 | + $this->delete($value); |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public function has(string $key): bool |
| 95 | + { |
| 96 | + return $this->storage->read($key) !== null; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private static function ttlToSeconds(null|int|DateInterval $ttl = null): ?int |
| 101 | + { |
| 102 | + if ($ttl instanceof DateInterval) { |
| 103 | + return self::dateIntervalToSeconds($ttl); |
| 104 | + } |
| 105 | + |
| 106 | + return $ttl; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + private static function dateIntervalToSeconds(DateInterval $dateInterval): int |
| 111 | + { |
| 112 | + $now = new \DateTimeImmutable; |
| 113 | + $expiresAt = $now->add($dateInterval); |
| 114 | + return $expiresAt->getTimestamp() - $now->getTimestamp(); |
| 115 | + } |
| 116 | +} |
0 commit comments