1
2

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 redis cache

Last updated at Posted at 2019-06-25

laravel で redis キャッシュを使いたい。
ほんとだっるい。

ついでに session も redis にしてやる。

.env

CACHE_DRIVER=redis
REDIS_HOST=133.230.222.333
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_DB=7
REDIS_READ_WRITE_TIMEOUT=60
SESSION_DRIVER=redis


database.php


    'redis' => [

        'client' => 'predis',
        'default' => [
            'host' => '133.230.222.333',
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 7),
        ],

        'cache' => [
            'host' => '133.230.222.333',
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 7),
        ],

    ],

cache.php 18行目あたり


    'default' => env('CACHE_DRIVER', 'redis'),

これでredisに入れられます。

use Illuminate\Support\Facades\Cache; を忘れずに。


        $name = 'hoge_1';//キャッシュの名前
        $time = 3;//秒

        //        キャッシュがあれば使う。なければ処理をして返す。

//        {}内で変数を使いたい場合は use を使う
//        $data = Cache::remember($name,$time, function () use ($repeat,$social_id) {
        $data = Cache::remember($name,$time, function () {
            $data = "ほげ".time();
            return $data;
        });

        Cache::forget($name);//キャッシュを削除

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?