Skip to content

Commit

Permalink
refactor:cache flow
Browse files Browse the repository at this point in the history
  • Loading branch information
shimonewman committed Jun 2, 2024
1 parent 2640e41 commit d86e66f
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2702,13 +2702,14 @@ public function getDocument(string $collection, string $id, array $queries = [])
/**
* Cache hash keys
*/
$documentCacheHash = $documentCacheKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId() . ':' . $id;
$collectionCacheHash = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId() . ':collection';
$documentCacheBaseKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId();
$documentCacheKey = $documentCacheHash = $documentCacheBaseKey . ':' . $id;
$collectionCacheKey = $documentCacheBaseKey . ':collection';
if (!empty($selections)) {
$documentCacheKey .= ':' . \md5(\implode($selections));
$documentCacheHash .= ':' . \md5(\implode($selections));
}

if ($cache = $this->cache->load($documentCacheHash.'---'.$documentCacheKey, self::TTL)) {
if ($cache = $this->cache->load($documentCacheKey, self::TTL, $documentCacheHash)) {
$document = new Document($cache);

if ($collection->getId() !== self::METADATA) {
Expand Down Expand Up @@ -2772,21 +2773,21 @@ public function getDocument(string $collection, string $id, array $queries = [])
foreach ($this->map as $key => $value) {
[$k, $v] = \explode('=>', $key);
$ck = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':map:' . $k;
$cache = $this->cache->load($ck.'---'.$ck, self::TTL);
$cache = $this->cache->load($ck, self::TTL, $ck);
if (empty($cache)) {
$cache = [];
}
if (!\in_array($v, $cache)) {
$cache[] = $v;
$this->cache->save($ck.'---'.$ck, $cache);
$this->cache->save($ck, $cache, $ck);
}
}

// Don't save to cache if it's part of a two-way relationship or a relationship at all
if (!$hasTwoWayRelationship && empty($relationships)) {
$this->cache->save($documentCacheHash.'---'.$documentCacheKey, $document->getArrayCopy());
$this->cache->save($documentCacheKey, $document->getArrayCopy(), $documentCacheHash);
//add document reference to the collection hash
$this->cache->save($collectionCacheHash.'---'.$documentCacheHash, 'empty');
$this->cache->save($collectionCacheKey, 'empty', $documentCacheKey);

}

Expand Down Expand Up @@ -4730,11 +4731,12 @@ private function deleteCascade(Document $collection, Document $relatedCollection
*/
public function purgeCachedCollection(string $collection): bool
{
$collectionCacheHash = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':collection';
$documents = $this->cache->list($collectionCacheHash);
foreach ($documents as $document) {
$this->cache->purge($document);
$collectionKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':collection';
$documentKeys = $this->cache->list($collectionKey);
foreach ($documentKeys as $documentKey) {
$this->cache->purge($documentKey);
}

return true;
}

Expand All @@ -4748,9 +4750,14 @@ public function purgeCachedCollection(string $collection): bool
*/
public function purgeCachedDocument(string $collection, string $id): bool
{
$collectionCacheHash = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':collection';
// Todo we should implement HDEL on the collection
return $this->cache->purge($this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':' . $id);
$baseKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection;
$documentKey = $baseKey . ':' . $id;
$collectionKey = $baseKey . ':collection';

$this->cache->purge($collectionKey, $documentKey);
$this->cache->purge($documentKey);

return true;
}

/**
Expand Down Expand Up @@ -5436,7 +5443,7 @@ private function purgeRelatedDocuments(Document $collection, string $id): void
}

$key = $this->cacheName . '-cache-' . $this->getNamespace() . ':map:' . $collection->getId() . ':' . $id;
$cache = $this->cache->load($key.'---'.$key, self::TTL);
$cache = $this->cache->load($key, self::TTL, $key);
if (!empty($cache)) {
foreach ($cache as $v) {
list($collectionId, $documentId) = explode(':', $v);
Expand Down

0 comments on commit d86e66f

Please sign in to comment.