Skip to content

Commit 4069477

Browse files
committed
Rename list cache helpers to query cache
1 parent e89017f commit 4069477

3 files changed

Lines changed: 66 additions & 66 deletions

File tree

‎src/Database/Database.php‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8565,24 +8565,24 @@ public function find(string $collection, array $queries = [], string $forPermiss
85658565
}
85668566

85678567
/**
8568-
* Purge all cached list entries for a collection namespace.
8568+
* Purge all cached query entries for a collection namespace.
85698569
*
85708570
* @param string $collection
85718571
* @param string|null $namespace
85728572
* @return bool
85738573
*/
8574-
public function purgeListCache(string $collection, ?string $namespace = null): bool
8574+
public function purgeQueryCache(string $collection, ?string $namespace = null): bool
85758575
{
85768576
$collectionDocument = $this->silent(fn () => $this->getCollection($collection));
85778577
$collection = $collectionDocument->isEmpty() ? $collection : $collectionDocument->getId();
85788578

85798579
return $this->cache->purge(
8580-
$this->getListCacheKey($collection, $namespace)
8580+
$this->getQueryCacheKey($collection, $namespace)
85818581
);
85828582
}
85838583

85848584
/**
8585-
* Restore a cached list document payload into database documents.
8585+
* Restore a cached query document payload into database documents.
85868586
*
85878587
* Returns false when the cached payload is invalid or stale and should be
85888588
* refreshed by the caller.
@@ -8594,7 +8594,7 @@ public function purgeListCache(string $collection, ?string $namespace = null): b
85948594
* @throws AuthorizationException
85958595
* @throws Exception
85968596
*/
8597-
public function restoreListCacheDocuments(
8597+
public function restoreQueryCacheDocuments(
85988598
Document $collection,
85998599
mixed $payload,
86008600
string $forPermission = Database::PERMISSION_READ,
@@ -9597,20 +9597,20 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
95979597
}
95989598

95999599
/**
9600-
* Stable cache key for cached list entries on a collection.
9600+
* Stable cache key for cached query entries on a collection.
96019601
*
96029602
* @param string $collectionId
96039603
* @param string|null $namespace
96049604
* @return string
96059605
*/
9606-
public function getListCacheKey(string $collectionId, ?string $namespace = null): string
9606+
public function getQueryCacheKey(string $collectionId, ?string $namespace = null): string
96079607
{
96089608
$hostname = $this->adapter->getSupportForHostname()
96099609
? $this->adapter->getHostname()
96109610
: '';
96119611

96129612
return \sprintf(
9613-
'%s-cache-%s:%s:%s:collection:%s:list',
9613+
'%s-cache-%s:%s:%s:collection:%s:query',
96149614
$this->cacheName,
96159615
$hostname,
96169616
$namespace ?? $this->getNamespace(),
@@ -9620,15 +9620,15 @@ public function getListCacheKey(string $collectionId, ?string $namespace = null)
96209620
}
96219621

96229622
/**
9623-
* Stable cache field for cached list entries on a collection.
9623+
* Stable cache field for cached query entries on a collection.
96249624
*
96259625
* @param Document|null $collection
96269626
* @param array<Query> $queries
96279627
* @param string $field
96289628
* @param string $forPermission
96299629
* @return string
96309630
*/
9631-
public function getListCacheField(
9631+
public function getQueryCacheField(
96329632
?Document $collection = null,
96339633
array $queries = [],
96349634
string $field = 'documents',
@@ -9648,7 +9648,7 @@ public function getListCacheField(
96489648
'database' => $this->getDatabase(),
96499649
'permission' => $forPermission,
96509650
'queries' => \array_map(
9651-
fn (Query $query): array => $this->serializeListCacheQuery($query),
9651+
fn (Query $query): array => $this->serializeQueryCacheQuery($query),
96529652
$queries,
96539653
),
96549654
'relationships' => $this->resolveRelationships,
@@ -9657,7 +9657,7 @@ public function getListCacheField(
96579657

96589658
return \sprintf(
96599659
'%s:%s:%s',
9660-
$this->getListCacheSchemaHash($collection),
9660+
$this->getQueryCacheSchemaHash($collection),
96619661
\md5(\json_encode($queryPayload) ?: ''),
96629662
$field,
96639663
);
@@ -9666,7 +9666,7 @@ public function getListCacheField(
96669666
/**
96679667
* @return array<string, mixed>
96689668
*/
9669-
private function serializeListCacheQuery(Query $query): array
9669+
private function serializeQueryCacheQuery(Query $query): array
96709670
{
96719671
$serialized = [
96729672
'method' => $query->getMethod(),
@@ -9679,19 +9679,19 @@ private function serializeListCacheQuery(Query $query): array
96799679
$values = [];
96809680
foreach ($query->getValues() as $value) {
96819681
if ($value instanceof Query) {
9682-
$values[] = $this->serializeListCacheQuery($value);
9682+
$values[] = $this->serializeQueryCacheQuery($value);
96839683
continue;
96849684
}
96859685

9686-
$values[] = $this->normalizeListCacheQueryValue($value);
9686+
$values[] = $this->normalizeQueryCacheQueryValue($value);
96879687
}
96889688

96899689
$serialized['values'] = $values;
96909690

96919691
return $serialized;
96929692
}
96939693

9694-
private function normalizeListCacheQueryValue(mixed $value): mixed
9694+
private function normalizeQueryCacheQueryValue(mixed $value): mixed
96959695
{
96969696
if ($value instanceof Document) {
96979697
$value = $value->getArrayCopy();
@@ -9702,13 +9702,13 @@ private function normalizeListCacheQueryValue(mixed $value): mixed
97029702
}
97039703

97049704
foreach ($value as $key => $item) {
9705-
$value[$key] = $this->normalizeListCacheQueryValue($item);
9705+
$value[$key] = $this->normalizeQueryCacheQueryValue($item);
97069706
}
97079707

97089708
return $value;
97099709
}
97109710

9711-
private function getListCacheSchemaHash(?Document $collection): string
9711+
private function getQueryCacheSchemaHash(?Document $collection): string
97129712
{
97139713
if ($collection === null || $collection->isEmpty()) {
97149714
return '';

‎tests/unit/CacheKeyTest.php‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void
135135
$this->assertNotEquals($hashEnabled, $hashDisabled);
136136
}
137137

138-
public function testListCacheKeyUsesListCacheShape(): void
138+
public function testQueryCacheKeyUsesQueryCacheShape(): void
139139
{
140140
$adapter = $this->createMock(Adapter::class);
141141
$adapter->method('getSupportForHostname')->willReturn(true);
@@ -146,12 +146,12 @@ public function testListCacheKeyUsesListCacheShape(): void
146146
$db = new Database($adapter, new Cache(new None()), []);
147147

148148
$this->assertSame(
149-
'default-cache-mysql-console:_39::collection:ttl_cache_table:list',
150-
$db->getListCacheKey('ttl_cache_table'),
149+
'default-cache-mysql-console:_39::collection:ttl_cache_table:query',
150+
$db->getQueryCacheKey('ttl_cache_table'),
151151
);
152152
}
153153

154-
public function testListCacheKeyCanOverrideNamespaceSegment(): void
154+
public function testQueryCacheKeyCanOverrideNamespaceSegment(): void
155155
{
156156
$adapter = $this->createMock(Adapter::class);
157157
$adapter->method('getSupportForHostname')->willReturn(true);
@@ -162,12 +162,12 @@ public function testListCacheKeyCanOverrideNamespaceSegment(): void
162162
$db = new Database($adapter, new Cache(new None()), []);
163163

164164
$this->assertSame(
165-
'default-cache-mysql-console:_39::collection:wafrules:list',
166-
$db->getListCacheKey('wafrules', '_39'),
165+
'default-cache-mysql-console:_39::collection:wafrules:query',
166+
$db->getQueryCacheKey('wafrules', '_39'),
167167
);
168168
}
169169

170-
public function testListCacheFieldUsesListCacheShape(): void
170+
public function testQueryCacheFieldUsesQueryCacheShape(): void
171171
{
172172
$db = $this->createDatabase();
173173
$collection = new Document([
@@ -192,18 +192,18 @@ public function testListCacheFieldUsesListCacheShape(): void
192192
. (\json_encode($collection->getAttribute('$permissions', [])) ?: '')
193193
. (\json_encode($collection->getAttribute('documentSecurity', false)) ?: '')
194194
);
195-
$field = $db->getListCacheField($collection, $queries);
195+
$field = $db->getQueryCacheField($collection, $queries);
196196

197197
$this->assertStringStartsWith("{$schemaHash}:", $field);
198198
$this->assertStringEndsWith(':documents', $field);
199199
$this->assertSame(2, \substr_count($field, ':'));
200200
}
201201

202-
public function testListCacheFieldChangesWithInputs(): void
202+
public function testQueryCacheFieldChangesWithInputs(): void
203203
{
204204
$db = $this->createDatabase();
205205

206-
$field = $db->getListCacheField(
206+
$field = $db->getQueryCacheField(
207207
new Document([
208208
'attributes' => [new Document(['$id' => 'name', 'type' => Database::VAR_STRING])],
209209
'indexes' => [],
@@ -213,50 +213,50 @@ public function testListCacheFieldChangesWithInputs(): void
213213

214214
$this->assertNotSame(
215215
$field,
216-
$db->getListCacheField(
216+
$db->getQueryCacheField(
217217
new Document([
218218
'attributes' => [new Document(['$id' => 'status', 'type' => Database::VAR_STRING])],
219219
'indexes' => [],
220220
]),
221221
[Query::limit(10)],
222222
),
223223
);
224-
$this->assertNotSame($field, $db->getListCacheField(null, [Query::limit(20)]));
225-
$this->assertNotSame($field, $db->getListCacheField(null, [Query::limit(10)], forPermission: Database::PERMISSION_UPDATE));
226-
$this->assertStringEndsWith(':total', $db->getListCacheField(null, [Query::limit(10)], 'total'));
224+
$this->assertNotSame($field, $db->getQueryCacheField(null, [Query::limit(20)]));
225+
$this->assertNotSame($field, $db->getQueryCacheField(null, [Query::limit(10)], forPermission: Database::PERMISSION_UPDATE));
226+
$this->assertStringEndsWith(':total', $db->getQueryCacheField(null, [Query::limit(10)], 'total'));
227227
}
228228

229-
public function testListCacheFieldChangesWithActiveAuthorizationContext(): void
229+
public function testQueryCacheFieldChangesWithActiveAuthorizationContext(): void
230230
{
231231
$db = $this->createDatabase();
232232

233-
$field = $db->getListCacheField(null, [Query::limit(10)]);
233+
$field = $db->getQueryCacheField(null, [Query::limit(10)]);
234234

235235
$this->assertNotSame(
236236
$field,
237-
$db->getAuthorization()->skip(fn () => $db->getListCacheField(null, [Query::limit(10)])),
237+
$db->getAuthorization()->skip(fn () => $db->getQueryCacheField(null, [Query::limit(10)])),
238238
);
239239

240240
$db->getAuthorization()->addRole('user:1');
241241

242242
$this->assertNotSame(
243243
$field,
244-
$db->getListCacheField(null, [Query::limit(10)]),
244+
$db->getQueryCacheField(null, [Query::limit(10)]),
245245
);
246246
}
247247

248-
public function testListCacheFieldIncludesCursorDocumentPayload(): void
248+
public function testQueryCacheFieldIncludesCursorDocumentPayload(): void
249249
{
250250
$db = $this->createDatabase();
251251

252-
$fieldA = $db->getListCacheField(null, [
252+
$fieldA = $db->getQueryCacheField(null, [
253253
Query::orderAsc('name'),
254254
Query::cursorAfter(new Document([
255255
'$id' => 'cursor',
256256
'name' => 'alpha',
257257
])),
258258
]);
259-
$fieldB = $db->getListCacheField(null, [
259+
$fieldB = $db->getQueryCacheField(null, [
260260
Query::orderAsc('name'),
261261
Query::cursorAfter(new Document([
262262
'$id' => 'cursor',
@@ -267,31 +267,31 @@ public function testListCacheFieldIncludesCursorDocumentPayload(): void
267267
$this->assertNotSame($fieldA, $fieldB);
268268
}
269269

270-
public function testListCacheFieldIncludesAmbientState(): void
270+
public function testQueryCacheFieldIncludesAmbientState(): void
271271
{
272272
$db = $this->createDatabase();
273273

274-
$field = $db->getListCacheField(null, [Query::limit(10)]);
274+
$field = $db->getQueryCacheField(null, [Query::limit(10)]);
275275

276276
$this->assertNotSame(
277277
$field,
278-
$db->skipFilters(fn () => $db->getListCacheField(null, [Query::limit(10)]), ['json']),
278+
$db->skipFilters(fn () => $db->getQueryCacheField(null, [Query::limit(10)]), ['json']),
279279
);
280280
$this->assertNotSame(
281281
$field,
282-
$db->skipRelationships(fn () => $db->getListCacheField(null, [Query::limit(10)])),
282+
$db->skipRelationships(fn () => $db->getQueryCacheField(null, [Query::limit(10)])),
283283
);
284284
}
285285

286-
public function testListCacheFieldValidatesQueryTypes(): void
286+
public function testQueryCacheFieldValidatesQueryTypes(): void
287287
{
288288
$this->expectException(QueryException::class);
289289

290290
$db = $this->createDatabase();
291291
$queries = ['invalid'];
292292

293293
/** @phpstan-ignore-next-line intentionally passing invalid query type */
294-
$db->getListCacheField(null, $queries);
294+
$db->getQueryCacheField(null, $queries);
295295
}
296296

297297

0 commit comments

Comments
 (0)