Skip to content

Commit 489e3ce

Browse files
authored
Merge pull request #827 from utopia-php/fix-cache
2 parents 1bebb29 + f9df197 commit 489e3ce

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/Database/Database.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,12 @@ class Database
372372
protected string $cacheName = 'default';
373373

374374
/**
375-
* @var array<string, array{encode: callable, decode: callable}>
375+
* @var array<string, array{encode: callable, decode: callable, signature: string}>
376376
*/
377377
protected static array $filters = [];
378378

379379
/**
380-
* @var array<string, array{encode: callable, decode: callable}>
380+
* @var array<string, array{encode: callable, decode: callable, signature: string}>
381381
*/
382382
protected array $instanceFilters = [];
383383

@@ -471,6 +471,10 @@ public function __construct(
471471
) {
472472
$this->adapter = $adapter;
473473
$this->cache = $cache;
474+
foreach ($filters as $name => $callbacks) {
475+
$filters[$name]['signature'] = self::computeCallableSignature($callbacks['encode'])
476+
. ':' . self::computeCallableSignature($callbacks['decode']);
477+
}
474478
$this->instanceFilters = $filters;
475479

476480
$this->setAuthorization(new Authorization());
@@ -8610,6 +8614,7 @@ public static function addFilter(string $name, callable $encode, callable $decod
86108614
self::$filters[$name] = [
86118615
'encode' => $encode,
86128616
'decode' => $decode,
8617+
'signature' => self::computeCallableSignature($encode) . ':' . self::computeCallableSignature($decode),
86138618
];
86148619
}
86158620

@@ -9206,9 +9211,39 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
92069211
if ($documentId) {
92079212
$documentKey = $documentHashKey = "{$collectionKey}:{$documentId}";
92089213

9209-
if (!empty($selects)) {
9210-
$documentHashKey = $documentKey . ':' . \md5(\implode($selects));
9214+
$sortedSelects = $selects;
9215+
\sort($sortedSelects);
9216+
9217+
$filterSignatures = [];
9218+
if ($this->filter) {
9219+
$disabled = $this->disabledFilters ?? [];
9220+
9221+
foreach (self::$filters as $name => $callbacks) {
9222+
if (isset($disabled[$name])) {
9223+
continue;
9224+
}
9225+
if (\array_key_exists($name, $this->instanceFilters)) {
9226+
continue;
9227+
}
9228+
$filterSignatures[$name] = $callbacks['signature'];
9229+
}
9230+
9231+
foreach ($this->instanceFilters as $name => $callbacks) {
9232+
if (isset($disabled[$name])) {
9233+
continue;
9234+
}
9235+
$filterSignatures[$name] = $callbacks['signature'];
9236+
}
9237+
9238+
\ksort($filterSignatures);
92119239
}
9240+
9241+
$payload = \json_encode([
9242+
'selects' => $sortedSelects,
9243+
'relationships' => $this->resolveRelationships,
9244+
'filters' => $filterSignatures,
9245+
]) ?: '';
9246+
$documentHashKey = $documentKey . ':' . \md5($payload);
92129247
}
92139248

92149249
return [
@@ -9218,6 +9253,22 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
92189253
];
92199254
}
92209255

9256+
private static function computeCallableSignature(callable $callable): string
9257+
{
9258+
if (\is_string($callable)) {
9259+
return $callable;
9260+
}
9261+
9262+
if (\is_array($callable)) {
9263+
$class = \is_object($callable[0]) ? \get_class($callable[0]) : $callable[0];
9264+
return $class . '::' . $callable[1];
9265+
}
9266+
9267+
$closure = \Closure::fromCallable($callable);
9268+
$ref = new \ReflectionFunction($closure);
9269+
return ($ref->getFileName() ?: 'unknown') . ':' . $ref->getStartLine();
9270+
}
9271+
92219272
/**
92229273
* @param array<Query> $queries
92239274
* @return void

0 commit comments

Comments
 (0)