laravel配置redis。ThinkPHP5配置redis。

版权声明:== https://github.com/fyonecon == https://blog.csdn.net/weixin_41827162/article/details/84335918

官方教程:https://laravel-china.org/docs/laravel/5.5/redis/1331

centos7安装redis:https://blog.csdn.net/weixin_41827162/article/details/84379406

Mac安装redis:https://blog.csdn.net/jason_m_ho/article/details/80007330

-

1. laravel配置redis,稍微麻烦,除了安装好Redis,还要安装vendor/下的predis扩展:

# 电脑安装predis

在使用 Laravel 的 Redis 之前,你需要通过 Composer 安装 predis/predis 扩展包。

进入laravel主目录,执行下面命令,自动向vendor/下添加predis扩展

SeenochengdeMacBook-Pro:laravel55 see$ composer require predis/predis

即可成功。

如果你还不成功的话请下载(地址1:https://download.csdn.net/download/weixin_41827162/10800301地址2:https://makeoss.oss-cn-hangzhou.aliyuncs.com/%E7%BC%96%E7%A8%8B/predis.zip)离线包,然后解压后直接放在vendor目录下即可。

--拓展-------------

# 电脑安装composer:

官方教程:https://pkg.phpcomposer.com/#how-to-install-composer

根据官方教程安装即可。

--------------------

# laravel配置redis:

.env:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

config/database.php:

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

],

路由routes/web.php:

Route::get('cache_redis', 'HomeController@cache_redis');

控制器app/Http/Controller/HomeController.php:

use Illuminate\Support\Facades\Redis;

实例:

public function cache_redis(){

//        Redis::set('name', 'fuck');
//        $values = Redis::get('name');
//        print_r($values);

        $userinfo = "2018";
        Redis::set('user_key',$userinfo);
        if(Redis::exists('user_key')){
            $values = Redis::get('user_key');
        }else{
            $values = 8012;
        }
        print_r($values);

        echo "<br/>0<br/>";
    }

成功。

-

2. ThinkPHP5配置redis,安装好Redis就能直接使用:

# 配置config.php:

// +----------------------------------------------------------------------
    // | 缓存设置
    // +----------------------------------------------------------------------

//    'cache'                  => [
//        // 驱动方式
//        'type'   => 'File',
//        // 缓存保存目录
//        'path'   => CACHE_PATH,
//        // 缓存前缀
//        'prefix' => '',
//        // 缓存有效期 0表示永久缓存
//        'expire' => 0,
//    ],

    'cache' =>  [ // 配置复合类型缓存
        'type'      =>  'complex',
        'default'   =>      [
            'type'  =>  'File',
            'path'  =>  CACHE_PATH,
        ],
        'file'      =>      [
            'type'  =>  'file',
            'path'  =>  RUNTIME_PATH . 'file/',
        ],
        'redis'     =>      [
            'type'  =>  'redis',
            'prefix'=>  '',
            'host'  =>  '127.0.0.1',
        ],


    ],

# 控制器使用:

引入cache:

use think\Cache;

使用实例:

public function cache_redis(){
    $han = new Cache();
    $han->set('username', 'user2-4');
    $data = $han->get('username');
    print_r($data);

}

成功。

-

猜你喜欢

转载自blog.csdn.net/weixin_41827162/article/details/84335918