0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel Sanctumのlast_used_atを自動更新しない設定にして、DB負荷を軽減する

Last updated at Posted at 2024-12-29

はじめに

本記事では、Laravel Sanctumでlast_used_atの自動更新を無効化する方法について解説します。デフォルトでは、Sanctumはアクセストークンの使用時にlast_used_atを自動的に更新しますが、これを防ぎたい場合の手順を示します。
特に、last_used_atを使用していない高トラフィックの環境では、DBへの負荷軽減が期待できます。

環境

  • PHP 8.2.3
  • Laravel 10.48.25

手順

カスタムモデルを作成

Sanctum\PersonalAccessTokenクラスを継承したカスタムモデルを作成し、last_used_atが更新されないように設定します。

App\Models\PersonalAccessToken
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class PersonalAccessToken extends SanctumPersonalAccessToken
{
    protected function lastUsedAt(): Attribute
    {
        return Attribute::make(
            set: function (mixed $value): void {
                return;
            },
        );
    }
}

AppServiceProviderで登録

次に、AppServiceProvider内で作成したカスタムモデルをSanctumに登録します。これにより、last_used_atの自動更新が無効化されます。

App\Providers\AppServiceProvider
<?php

declare(strict_types=1);

namespace App\Providers;

use App\Models\PersonalAccessToken;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
    }
}

結果

変更前

デフォルトの挙動では、アクセストークン使用時にpersonal_access_tokensテーブルのlast_used_atが更新されます。

2024-12-28T22:54:02.895405Z	   25 Prepare	select * from `users` where `users`.`id` = ? limit 1
2024-12-28T22:54:02.895736Z	   25 Prepare	select * from `users` where `users`.`id` = ? limit 1
2024-12-28T22:54:02.895839Z	   25 Execute	select * from `users` where `users`.`id` = 1 limit 1
2024-12-28T22:54:02.896110Z	   25 Close stmt	
2024-12-28T22:54:02.897823Z	   25 Prepare	update `personal_access_tokens` set `last_used_at` = ?, `personal_access_tokens`.`updated_at` = ? where `id` = ?
2024-12-28T22:54:02.897992Z	   25 Prepare	update `personal_access_tokens` set `last_used_at` = ?, `personal_access_tokens`.`updated_at` = ? where `id` = ?
2024-12-28T22:54:02.898010Z	   25 Execute	update `personal_access_tokens` set `last_used_at` = '2024-12-29 07:54:02', `personal_access_tokens`.`updated_at` = '2024-12-29 07:54:02' where `id` = 1
2024-12-28T22:54:02.901085Z	   25 Close stmt	
2024-12-28T22:54:02.940796Z	   25 Quit	

変更後

上記の手順を実施した後、last_used_atの自動更新がなくなり、以下のように更新クエリが実行されなくなります。

2024-12-28T22:56:12.968722Z	   27 Prepare	select * from `users` where `users`.`id` = ? limit 1
2024-12-28T22:56:12.968979Z	   27 Prepare	select * from `users` where `users`.`id` = ? limit 1
2024-12-28T22:56:12.969116Z	   27 Execute	select * from `users` where `users`.`id` = 1 limit 1
2024-12-28T22:56:12.969498Z	   27 Close stmt	
2024-12-28T22:56:12.999318Z	   27 Quit	

まとめ

この記事では、Laravel Sanctumでlast_used_atの自動更新を無効化する方法について解説しました。具体的には、以下の手順を実施しました。

  1. Sanctum\PersonalAccessTokenを継承したカスタムモデルを作成し、last_used_atの更新ロジックを上書き
  2. AppServiceProviderでカスタムモデルをSanctumに登録

これにより、アクセストークンの使用時にlast_used_atが自動的に更新される問題を防ぐことができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?