instancecache trait

pull/26/head
Ondřej Hruška 6 years ago
parent a3ba68ea98
commit 3b123d7f08
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 37
      app/Models/Concerns/InstanceCache.php
  2. 6
      app/Models/Table.php
  3. 2
      app/Models/User.php
  4. 29
      resources/views/user/_table-list.blade.php
  5. 31
      resources/views/user/view.blade.php

@ -0,0 +1,37 @@
<?php
namespace App\Models\Concerns;
use Illuminate\Database\Eloquent\Model;
/**
* Instance cache, used to avoid multiple retrieval of related models.
* Repeated calls to getCached() all return the same instance, without
* additional DB access.
*/
trait InstanceCache
{
private static $instanceCache = [];
public static function bootInstanceCache()
{
self::retrieved(function (Model $instance) {
self::$instanceCache[$instance->getKey()] = $instance;
});
}
/**
* Find entity by ID, either in DB, or in the internal cache.
*
* @param int $id
* @return Model|null
*/
public static function getCached($id)
{
return isset(self::$instanceCache[$id]) ?
self::$instanceCache[$id] :
(self::$instanceCache[$id] = self::find($id));
}
}

@ -37,6 +37,12 @@ class Table extends Model
use Reportable; use Reportable;
protected $guarded = []; protected $guarded = [];
/** Get owner from the instance cache (use when building table lists) */
public function cachedOwner()
{
return User::getCached($this->owner_id);
}
/** Owning user */ /** Owning user */
public function owner() public function owner()
{ {

@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use AdamWathan\EloquentOAuth\OAuthIdentity; use AdamWathan\EloquentOAuth\OAuthIdentity;
use App\Models\Concerns\InstanceCache;
use App\Models\Concerns\Reportable; use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
@ -38,6 +39,7 @@ class User extends Authenticatable
{ {
use Reportable; use Reportable;
use Notifiable; use Notifiable;
use InstanceCache;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.

@ -0,0 +1,29 @@
<div class="list-group list-group-flush">
@if(count($tables) == 0)
<span class="list-group-item">No tables yet.</span>
@else
@foreach($tables as $table)
<a class="list-group-item list-group-item-action"
href="{{route('table.view', ['user' => $table->cachedOwner()->name, 'table' => $table->name])}}">
<span class="d-block row">
<span class="d-inline-block col-10">
<i class="fa-table fa-pr"></i>{{ $table->title }}
</span>{{--
--}}<span class="d-inline-block col-2 small text-right">
{{ $table->revision->row_count }} rows
</span>
</span>
<span class="d-block small row">
<span class="d-inline-block col-8">
{{ ellipsis($table->description, 215) }}
</span>{{--
--}}<span class="d-inline-block col-4 text-right text-muted">
Last change {{ $table->updated_at->diffForHumans() }}
</span>
</span>
</a>
@endforeach
{{ $tables->links() }}
@endif
</div>

@ -65,7 +65,6 @@
</div> </div>
</div> </div>
</div> </div>
{{-- Table list card --}} {{-- Table list card --}}
<div class="col-md-8"> <div class="col-md-8">
<div class="card"> <div class="card">
@ -83,35 +82,7 @@
@endif @endif
</div> </div>
<div class="list-group list-group-flush"> @include('user._table-list')
@if(count($tables) == 0)
<span class="list-group-item">No tables yet.</span>
@else
@foreach($tables as $table)
<a class="list-group-item list-group-item-action"
href="{{route('table.view', ['user' => $user->name, 'table' => $table->name])}}">
<span class="d-block row">
<span class="d-inline-block col-10">
<i class="fa-table fa-pr"></i>{{ $table->title }}
</span>{{--
--}}<span class="d-inline-block col-2 small text-right">
{{ $table->revision->row_count }} rows
</span>
</span>
<span class="d-block small row">
<span class="d-inline-block col-8">
{{ ellipsis($table->description, 215) }}
</span>{{--
--}}<span class="d-inline-block col-4 text-right text-muted">
Last change {{ $table->updated_at->diffForHumans() }}
</span>
</span>
</a>
@endforeach
{{ $tables->links() }}
@endif
</div>
</div> </div>
</div> </div>

Loading…
Cancel
Save