1 | <?php |
||
2 | /* |
||
3 | * This file is part of the Koded package. |
||
4 | * |
||
5 | * (c) Mihail Binev <[email protected]> |
||
6 | * |
||
7 | * Please view the LICENSE distributed with this source code |
||
8 | * for the full copyright and license information. |
||
9 | */ |
||
10 | |||
11 | namespace Koded\Caching\Client; |
||
12 | |||
13 | use function Koded\Caching\normalize_ttl; |
||
14 | use function Koded\Stdlib\now; |
||
15 | |||
16 | trait ClientTrait |
||
17 | { |
||
18 | /** @var int|null Global TTL for caching, used as default expiration time in cache clients */ |
||
19 | private ?int $ttl; |
||
20 | |||
21 | /** @var \Memcached | \Redis | \Predis\Client | \Koded\Caching\Client\FileClient | \Koded\Caching\Client\MemoryClient | \Koded\Caching\Client\ShmopClient */ |
||
22 | private $client; |
||
23 | |||
24 | 2 | public function getTtl(): ?int |
|
25 | { |
||
26 | 2 | return $this->ttl; |
|
27 | } |
||
28 | |||
29 | 132 | public function client() |
|
30 | { |
||
31 | 132 | return $this->client ?? $this; |
|
32 | } |
||
33 | |||
34 | 142 | private function timestampWithGlobalTtl($ttl, int $default = 0): int |
|
35 | { |
||
36 | 142 | $explicit = normalize_ttl($ttl); |
|
37 | 142 | $now = now()->getTimestamp(); |
|
38 | 142 | if (null === $explicit && $this->ttl > 0) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
39 | 2 | return $now + $this->ttl; |
|
40 | } |
||
41 | 141 | if ($explicit > 0) { |
|
42 | 8 | return $now + $explicit; |
|
43 | } |
||
44 | 133 | return $explicit ?? $default; |
|
45 | } |
||
46 | |||
47 | 225 | private function secondsWithGlobalTtl($ttl): int |
|
48 | { |
||
49 | 225 | $seconds = normalize_ttl($ttl); |
|
50 | 225 | if (null === $seconds && $this->ttl > 0) { |
|
0 ignored issues
–
show
|
|||
51 | return (int)$this->ttl; |
||
52 | } |
||
53 | 225 | return (int)$seconds; |
|
54 | } |
||
55 | } |