-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheStorage.php
More file actions
120 lines (107 loc) · 3.11 KB
/
Copy pathCacheStorage.php
File metadata and controls
120 lines (107 loc) · 3.11 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace ArrayAccess\DnsRecord\Cache;
use ArrayAccess\DnsRecord\Interfaces\Cache\CacheAdapterInterface;
use ArrayAccess\DnsRecord\Interfaces\Cache\CacheStorageInterface;
use ArrayAccess\DnsRecord\Interfaces\Packet\PacketRequestDataInterface;
use ArrayAccess\DnsRecord\Interfaces\Packet\PacketResponseInterface;
use DateInterval;
use function array_map;
use function array_values;
use function is_object;
use function sprintf;
class CacheStorage implements CacheStorageInterface
{
const PREFIX = 'php_dns_client_';
/**
* Cache adapter object
*
* @var ?CacheAdapterInterface
*/
private ?CacheAdapterInterface $adapter;
public function __construct(?CacheAdapterInterface $adapter = null)
{
$this->adapter = $adapter;
}
/**
* @inheritdoc
*/
public function getAdapter(): ?CacheAdapterInterface
{
return $this->adapter;
}
/**
* @inheritdoc
*/
public function setAdapter(CacheAdapterInterface $cacheAdapter): void
{
$this->adapter = $cacheAdapter;
}
/**
* @inheritdoc
*/
public function saveItem(PacketResponseInterface $response, int|DateInterval|null $ttl = self::DEFAULT_TTL): bool
{
$cacheItem = $this->getAdapter()?->getItem($this->getCacheName($response->getPacketData()));
if ($cacheItem === null) {
return false;
}
$cacheItem
->set($response)
->expiresAfter($ttl??self::DEFAULT_TTL);
return $this->getAdapter()?->saveItem(
$cacheItem
)?:false;
}
/**
* @inheritdoc
*/
public function hasItem(PacketRequestDataInterface|string $key): bool
{
return $this->getAdapter()?->hasItem($this->getCacheName($key))?:false;
}
/**
* @inheritdoc
*/
public function getItem(PacketRequestDataInterface|string $key): ?PacketResponseInterface
{
$key = $this->getCacheName($key);
if (!$this->getAdapter()?->hasItem($key)) {
return null;
}
$cacheData = $this->getAdapter()->getItem($key)->get();
return $cacheData instanceof PacketResponseInterface
? $cacheData
: null;
}
/**
* @inheritdoc
*/
public function deleteItem(PacketRequestDataInterface|string $key): bool
{
return $this->getAdapter()?->deleteItem($this->getCacheName($key))?:false;
}
/**
* @inheritdoc
*/
public function deleteItems(string|PacketRequestDataInterface ...$keys): bool
{
return $this->getAdapter()?->deleteItems(
...array_values(array_map([$this, 'getCacheName'], $keys))
)?:false;
}
/**
* @inheritdoc
*/
public function getCacheName(PacketRequestDataInterface|string $key): string
{
if (is_object($key)) {
// id is increment / random
// cache use id
$key = $key->withHeader($key->getHeader()->withId(1));
}
return is_object($key)
? sprintf('%s%s', self::PREFIX, md5($key->getQueryMessage()))
: $key;
}
}