4
4

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 5 years have passed since last update.

laravel で redis を使おうぜ

Last updated at Posted at 2019-03-04

#predis をインスコ


composer require predis/predis

#database.php


    'redis' => [

        'client' => 'predis',
        'default' => [
            'host' => env('REDIS_HOST'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT'),
            'database' => env('REDIS_DB'),
        ],

        'cache' => [
            'host' => env('REDIS_HOST'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT'),
            'database' => env('REDIS_DB'),
        ],

    ],

env にも同様に設定


CACHE_DRIVER=redis
REDIS_HOST=133.130.103.167
REDIS_PASSWORD=
REDIS_PORT=6379
REDIS_DB=2
REDIS_READ_WRITE_TIMEOUT=60

キャッシュクリア


php artisan config:clear
php artisan config:cache

使い方


use Illuminate\Support\Facades\Redis;


class HogeController extends Controller
{
    public function test()
    {

//        文字列を挿入
        $data = 'ほげまんちょ';
        Redis::command('SET', [3, $data]);//キー3に $data を入れる
        $res = Redis::command('GET', [3]);//キー3から読み出す
        print_r($res);//ほげまんちょ


//        インクリメント
        $incr = Redis::command('GET',[7]);
        $incr++;
        Redis::command('SET',[7,$incr]);

        print_r($incr);//17
        die;

    }


4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?