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?

More than 3 years have passed since last update.

Docker-compose上のLaravelでRedisキャッシュ

Last updated at Posted at 2021-05-15

Redisのセットアップ

  • composer require predis/predisする
  • docker-compose.yamlにRedisを足す。yamlファイルはインテンドの高さを間違えると動かないので注意。
redis:
    image: redis
    ports:
      - 6379:6379
  • config/database.phpのRedisクライアントをpredisに変更
config/database.php
//前略
    'redis' => [
        'client' => env('REDIS_CLIENT', 'predis'),
//後略
  • .envでキャッシュドライバとホストを変更
.env
CACHE_DRIVER=redis
REDIS_HOST=redis

以上でとりあえずRedisが動きます。

Cacheをコントローラで使う

とりえあずProduct::all()の結果をキャッシュする場合を考える。
この場合、

  • すでにキャッシュがあればそこからデータを取り出す
  • ない場合はProduct::all()のクエリを走らせ、結果をキャッシュする

という処理を行うこととなる。

public function frontend(){
         if($products = \Cache::get('products_frontend')){
             return $products;
         }

         $products = Product::all();
        \Cache::set('products_frontend',$products,30*60);
         return $products;
   }

Cache::get($cache_name)でキャッシュを呼び出せる。明快だ。Cache::set()は第一引数がキャッシュ名、第二引数がキャッシュするデータ、第三引数がキャッシュする時間(秒単位)だ。
なおファサードからは、先頭にバックスラッシュを付けることでクラスを呼び出せる。

また上記の処理は、\Cache::rememberを使うと一行で書くことが出来る。

$products = \Cache::remember('products_frontend',30*60,fn()=>Product::all());
return $products;

イベントを使ってキャッシュを消したい!

$productsをキャッシュしたが、商品をupdateしたりcreateしたりdeleteしたりしても、getする際にはキャッシュから引っ張ってくるので情報が変わらないという問題がある。
つまり、update、create、deleteを行う場合はキャッシュを消す必要があるわけだ。

    public function deleteCache(){
        \Cache::forget('products_frontend');
    }

とやるのも全然ありだが、ここはイベントを使って消す方法を見ていく。

まずはphp artisan make:event ProductUpdatedEventphp artisan make:listener ProductUpdatedListenerを作成する。
また、EventServiceProviderに両者を登録する。

App/Providers/EventServiceProvider.php
protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        ProductUpdatedEvent::class=>[
            ProductUpdatedListener::class
        ]
    ];

これで、ProductUpdatedEventのリスナとしてProductUpdatedListenerが登録される。

さて、ProductUpdatedEventのコードだが、今回はただ発火するだけのイベントなので、コードの大半は削っていい。

App/events/ProductUpdatedEvent.php
class ProductUpdatedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
}

そしてリスナ内に、必要な処理を書き込む。

App/Listeners/ProductUpdatedListener.php
class ProductUpdatedListener
{
    public function handle( ProductUpdatedEvent $event)
    {
        \Cache::forget('products_frontend');
        \Cache::forget('products_backend');
    }
}

これで、ProductUpdatedEventがアプリのどこかで発火し次第、リスナ内のキャッシュ消去処理が実行されることになる。
最後にコントローラ内の必要箇所で、以下のコードでイベントを発火させる。

event(new ProductUpdatedEvent);
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?