refactor: Cache feed data for purchase form and observe Feed model changes
This commit is contained in:
parent
271d2eba6e
commit
6d77a94806
@ -11,19 +11,26 @@
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\View;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FeedPurchaseForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
$feeds = Feed::with('unit')
|
||||
->latest()
|
||||
->get()
|
||||
->map(function ($feed) {
|
||||
$feed->image = $feed->getFirstMediaUrl('feeds');
|
||||
$feeds = Cache::store('redis')->remember(
|
||||
'feeds_with_unit_for_purchase_form',
|
||||
now()->addMonth(),
|
||||
function () {
|
||||
return Feed::with('unit')
|
||||
->latest()
|
||||
->get()
|
||||
->map(function ($feed) {
|
||||
$feed->image = $feed->getFirstMediaUrl('feeds');
|
||||
|
||||
return $feed;
|
||||
});
|
||||
return $feed;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
$cartItems = FeedPurchaseItem::with('feed')
|
||||
->whereNull('feed_purchase_id')
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Observers\FeedObserver;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@ -11,6 +13,7 @@
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
|
||||
#[ObservedBy([FeedObserver::class])]
|
||||
class Feed extends Model implements HasMedia
|
||||
{
|
||||
use HasFactory, InteractsWithMedia, SoftDeletes;
|
||||
|
||||
57
app/Observers/FeedObserver.php
Normal file
57
app/Observers/FeedObserver.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Feed;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FeedObserver
|
||||
{
|
||||
/**
|
||||
* Handle events after any change that should invalidate feed cache.
|
||||
*/
|
||||
protected function clearFeedCache(): void
|
||||
{
|
||||
Cache::store('redis')->forget('feeds_with_unit_for_purchase_form');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Feed "created" event.
|
||||
*/
|
||||
public function created(Feed $feed): void
|
||||
{
|
||||
$this->clearFeedCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Feed "updated" event.
|
||||
*/
|
||||
public function updated(Feed $feed): void
|
||||
{
|
||||
$this->clearFeedCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Feed "deleted" event.
|
||||
*/
|
||||
public function deleted(Feed $feed): void
|
||||
{
|
||||
$this->clearFeedCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Feed "restored" event.
|
||||
*/
|
||||
public function restored(Feed $feed): void
|
||||
{
|
||||
$this->clearFeedCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Feed "force deleted" event.
|
||||
*/
|
||||
public function forceDeleted(Feed $feed): void
|
||||
{
|
||||
$this->clearFeedCache();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user