使用缓存提供数据
This commit is contained in:
parent
f82007d59c
commit
26eb9050b8
@ -6,7 +6,6 @@
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Module\Module;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class HostController extends Controller
|
||||
@ -14,9 +13,7 @@ class HostController extends Controller
|
||||
public function index(Module $module)
|
||||
{
|
||||
//
|
||||
$hosts = Host::thisUser($module->id)->with('module', function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
})->get();
|
||||
$hosts = (new Host())->getUserHosts($module->id ?? null);
|
||||
|
||||
return $this->success($hosts);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ class TaskController extends Controller
|
||||
public function __invoke()
|
||||
{
|
||||
//
|
||||
$tasks = Task::user()->with('host')->get();
|
||||
$tasks = (new Task())->getCurrentUserTasks();
|
||||
|
||||
return $this->success($tasks);
|
||||
}
|
||||
|
@ -33,6 +33,17 @@ class Host extends Model
|
||||
];
|
||||
|
||||
|
||||
// get user hosts
|
||||
public function getUserHosts($module_id) {
|
||||
|
||||
return Cache::remember('user_hosts_' . auth()->id(), 3600, function () use ($module_id) {
|
||||
return $this->thisUser($module_id)->with('module', function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
})->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// user
|
||||
public function user()
|
||||
{
|
||||
|
@ -6,6 +6,7 @@
|
||||
use App\Exceptions\CommonException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class Task extends Model
|
||||
@ -35,6 +36,14 @@ public function scopeUser($query)
|
||||
}
|
||||
|
||||
|
||||
public function getCurrentUserTasks()
|
||||
{
|
||||
return Cache::remember('user_tasks_' . auth()->id(), 3600, function () {
|
||||
return $this->user()->with('host')->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public function host()
|
||||
{
|
||||
return $this->belongsTo(Host::class);
|
||||
@ -72,6 +81,8 @@ protected static function boot()
|
||||
|
||||
|
||||
$model->user_id = $model->host->user_id;
|
||||
|
||||
Cache::forget('user_tasks_' . auth()->id());
|
||||
}
|
||||
});
|
||||
|
||||
@ -81,5 +92,15 @@ protected static function boot()
|
||||
$model->status = 'done';
|
||||
}
|
||||
});
|
||||
|
||||
// updated and delete
|
||||
static::updated(function () {
|
||||
Cache::forget('user_tasks_' . auth()->id());
|
||||
});
|
||||
|
||||
|
||||
static::deleted(function () {
|
||||
Cache::forget('user_tasks_' . auth()->id());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user