-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCacheItem.php
69 lines (58 loc) · 1.42 KB
/
CacheItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Koded\Caching;
use Psr\Cache\CacheItemInterface;
abstract class CacheItem implements CacheItemInterface
{
protected bool $isHit = false;
private string $key;
private mixed $value = null;
private ?int $expiresAt;
public function __construct($key, ?int $ttl)
{
$this->key = $key;
$this->expiresAt = $ttl;
}
public function getKey(): string
{
return $this->key;
}
public function get()
{
return $this->value;
}
public function isHit(): bool
{
return $this->isHit;
}
public function set($value)
{
$this->value = $value;
return $this;
}
public function expiresAfter($time): static
{
// The TTL is calculated in the cache client instance
return $this->expiresAt($time);
}
public function expiresAt($expiration): static
{
$this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt);
if ($this->expiresAt < 1) {
$this->isHit = false;
}
return $this;
}
/**
* Returns expiration seconds for the cache item.
* NULL is reserved for clients who do not support expiry
* to implement some custom logic around the TTL.
*
* This method is not part of the PSR-6.
*
* @return int|null
*/
public function getExpiresAt(): ?int
{
return $this->expiresAt;
}
}