PHP code example of brekitomasson / laravel-tagged-cache

1. Go to this page and download the library: Download brekitomasson/laravel-tagged-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brekitomasson / laravel-tagged-cache example snippets


public function getDisplayNameAttribute(): string
{
  return $this->taggedCache()->remember(
      key: 'displayname', 
      ttl: now()->addHour(), 
      callback: fn () => $this->nickname ?? $this->name ?? $this->email,
  );
}

public function getContentHtml(): string
{
    return $this->taggedCache('markdown')->remember(
        key: 'content:html',
        ttl: now()->addHour(),
        callback: fn () => Markdown::parse($this->content)->toHtml(),
    );
}

public function flushTaggedCacheOnAttributeUpdate(): array
{
    return ['name', 'display_name', 'status'];
}

public function updated(ModelName $model): void
{
    if ($model->isDirty(['name', 'display_name', 'status'])) {
        $model->taggedCache()->flush();
    }
}

public function getCacheTagIdentifier(): string
{
    return 'Comic Books';
}

public function getCountryNameAttribute(): string
{
    return $this->taggedCache()->remember(
        'country-name',
        TimeSpan::ONE_WEEK(),
        fn() => $this->country->name
    );
}

public function getOpenTicketCount(): int
{
    return $this->taggedCache()->remember(
        'open-tickets',
        TimeSpan::minutes(3),
        fn() => $this->tickets->whereNotIn('status', [TicketStatus::CLOSED, TicketStatus::PENDING])->count();
    );
}