Skip to content

Commit

Permalink
Merge pull request #196 from leroy-merlin-br/feat/eager-loading
Browse files Browse the repository at this point in the history
chore: bypass cache if disabled
  • Loading branch information
orlandocavassani committed Oct 18, 2022
2 parents 741f0e7 + 269466f commit ef24af9
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Model/HasLegacyRelationsTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mongolid\Model;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Str;
use MongoDB\BSON\ObjectId;
use Mongolid\Container\Container;
Expand All @@ -14,6 +15,7 @@
use Mongolid\Schema\Schema;
use Mongolid\Util\CacheComponentInterface;
use Mongolid\Util\ObjectIdUtils;
use MongolidLaravel\MongolidModel;

/**
* It is supposed to be used on model classes in general.
Expand All @@ -25,11 +27,12 @@ trait HasLegacyRelationsTrait
/**
* Returns the referenced documents as objects.
*
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _id is stored
* @param bool $cacheable retrieves a CacheableCursor instead
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _id is stored
* @param bool $cacheable retrieves a CacheableCursor instead
*
* @return mixed
* @throws BindingResolutionException
*/
protected function referencesOne(string $entity, string $field, bool $cacheable = true)
{
Expand All @@ -41,14 +44,7 @@ protected function referencesOne(string $entity, string $field, bool $cacheable

$entityInstance = Container::make($entity);

/** @var CacheComponentInterface $cacheComponent */
$cacheComponent = Container::make(CacheComponentInterface::class);
$cacheKey = $this->generateCacheKey($entityInstance, $referencedId);

// Checks if the model was already eager loaded.
// if so, we don't need to query database to
// use the document.
if ($document = $cacheComponent->get($cacheKey)) {
if ($cacheable && $referencedId && $document = $this->getDocumentFromCache($entityInstance, $referencedId)) {
return $document;
}

Expand Down Expand Up @@ -192,4 +188,24 @@ public function detach(string $field, &$obj)
$embedder = Container::make(DocumentEmbedder::class);
$embedder->detach($this, $field, $obj);
}

/**
* @return mixed|null
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
private function getDocumentFromCache(ModelInterface $entityInstance, string $referencedId)
{
/** @var CacheComponentInterface $cacheComponent */
$cacheComponent = Container::make(CacheComponentInterface::class);
$cacheKey = $this->generateCacheKey($entityInstance, $referencedId);

// Checks if the model was already eager loaded.
// if so, we don't need to query database to
// use the document.
if (!$document = $cacheComponent->get($cacheKey)) {
return null;
}

return $document;
}
}

0 comments on commit ef24af9

Please sign in to comment.