-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
CachingResult.php
185 lines (150 loc) · 3.9 KB
/
CachingResult.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Doctrine\DBAL\Cache;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Result;
use Psr\Cache\CacheItemPoolInterface;
use function array_map;
use function array_values;
/**
* A result is saved in multiple cache keys, there is the originally specified
* cache key which is just pointing to result rows by key. The following things
* have to be ensured:
*
* 1. lifetime of the original key has to be longer than that of all the individual rows keys
* 2. if any one row key is missing the query has to be re-executed.
*
* Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
* This means that the memory usage for cached results might increase by using this feature.
*
* @internal The class is internal to the caching layer implementation.
*/
class CachingResult implements DriverResult
{
/** @var CacheItemPoolInterface */
private $cache;
/** @var string */
private $cacheKey;
/** @var string */
private $realKey;
/** @var int */
private $lifetime;
/** @var Result */
private $result;
/** @var array<int,array<string,mixed>>|null */
private $data;
/**
* @param string $cacheKey
* @param string $realKey
* @param int $lifetime
*/
public function __construct(Result $result, CacheItemPoolInterface $cache, $cacheKey, $realKey, $lifetime)
{
$this->result = $result;
$this->cache = $cache;
$this->cacheKey = $cacheKey;
$this->realKey = $realKey;
$this->lifetime = $lifetime;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->fetch();
if ($row === false) {
return false;
}
return array_values($row);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->fetch();
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric(): array
{
return array_map('array_values', $this->fetchAllAssociative());
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative(): array
{
$data = $this->result->fetchAllAssociative();
$this->store($data);
return $data;
}
/**
* {@inheritdoc}
*/
public function fetchFirstColumn(): array
{
return FetchUtils::fetchFirstColumn($this);
}
public function rowCount(): int
{
return $this->result->rowCount();
}
public function columnCount(): int
{
return $this->result->columnCount();
}
public function free(): void
{
$this->data = null;
}
/**
* @return array<string,mixed>|false
*
* @throws Exception
*/
private function fetch()
{
if ($this->data === null) {
$this->data = [];
}
$row = $this->result->fetchAssociative();
if ($row !== false) {
$this->data[] = $row;
return $row;
}
$this->saveToCache();
return false;
}
/**
* @param array<int,array<string,mixed>> $data
*/
private function store(array $data): void
{
$this->data = $data;
$this->saveToCache();
}
private function saveToCache(): void
{
if ($this->data === null) {
return;
}
$item = $this->cache->getItem($this->cacheKey);
$data = $item->isHit() ? $item->get() : [];
$data[$this->realKey] = $this->data;
$item->set($data);
if ($this->lifetime > 0) {
$item->expiresAfter($this->lifetime);
}
$this->cache->save($item);
}
}