Skip to content

Commit

Permalink
Cache relations to avoid duplicate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Jun 20, 2023
1 parent 17675f8 commit 35ee4fc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/Base/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Lunar\Base\Traits\HasModelExtending;
use Spatie\LaravelBlink\BlinkFacade as Blink;

abstract class BaseModel extends Model
{
Expand All @@ -22,4 +23,11 @@ public function __construct(array $attributes = [])
$this->setConnection($connection);
}
}

protected function getCachedRelation($attribute, $callback, $morphType = '')
{
return Blink::once('lunar:'.$attribute.$morphType.':'.$this->{$attribute}, function() use ($callback) {
return $callback();
});
}
}
14 changes: 14 additions & 0 deletions packages/core/src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ public function currency()
{
return $this->belongsTo(Currency::class);
}

public function getCurrencyAttribute()
{
return $this->getCachedRelation('currency_id', function () {
return $this->currency()->first();
});
}

/**
* Return the user relationship.
Expand All @@ -199,6 +206,13 @@ public function user()
{
return $this->belongsTo(config('auth.providers.users.model'));
}

public function getUserAttribute()
{
return $this->getCachedRelation('user_id', function () {
return $this->user()->first();
});
}

public function scopeUnmerged($query)
{
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/Models/CartLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ public function cart()
{
return $this->belongsTo(Cart::class);
}

public function getCartAttribute()
{
return $this->getCachedRelation('cart_id', function () {
return $this->cart()->first();
});
}

/**
* Return the tax class relationship.
Expand Down Expand Up @@ -154,4 +161,11 @@ public function purchasable()
{
return $this->morphTo();
}

public function getPurchasableAttribute()
{
return $this->getCachedRelation('purchasable_id', function () {
return $this->purchasable()->first();
}, 'purchasable_type');
}
}
21 changes: 21 additions & 0 deletions packages/core/src/Models/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public function priceable()
{
return $this->morphTo();
}

public function getPriceableAttribute()
{
return $this->getCachedRelation('priceable_id', function () {
return $this->priceable()->first();
}, 'priceable_type');
}

/**
* Return the currency relationship.
Expand All @@ -65,6 +72,13 @@ public function currency()
{
return $this->belongsTo(Currency::class);
}

public function getCurrencyAttribute()
{
return $this->getCachedRelation('currency_id', function () {
return $this->currency()->first();
});
}

/**
* Return the customer group relationship.
Expand All @@ -75,4 +89,11 @@ public function customerGroup()
{
return $this->belongsTo(CustomerGroup::class);
}

public function getCustomerGroupAttribute()
{
return $this->getCachedRelation('customer_group_id', function () {
return $this->customerGroup()->first();
});
}
}
14 changes: 14 additions & 0 deletions packages/core/src/Models/ProductVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public function product()
{
return $this->belongsTo(Product::class)->withTrashed();
}

public function getProductAttribute()
{
return $this->getCachedRelation('product_id', function () {
return $this->product()->first();
});
}

/**
* Return the tax class relationship.
Expand All @@ -95,6 +102,13 @@ public function taxClass()
{
return $this->belongsTo(TaxClass::class);
}

public function getTaxClassAttribute()
{
return $this->getCachedRelation('tax_class_id', function () {
return $this->taxClass()->first();
});
}

/**
* Return the related product option values.
Expand Down

0 comments on commit 35ee4fc

Please sign in to comment.