* 参考资料

redis文档             http://www.redis.cn/documentation.html,  http://redisdoc.com/index.html

redis桌面客户端  https://redisdesktop.com/download

 

* 修改redis.conf

   添加一行

requirepass "自己设置的redis密码"

 去掉一行注释

bind 127.0.0.1 ::1

  

* 重启redis服务

lsof -i:6379

找到redis的进程id, kill

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 1573 Mch 4u IPv6 0xb521b0eb0f67f0ad 0t0 TCP *:6379 (LISTEN)
redis-ser 1573 Mch 5u IPv4 0xb521b0eb178de785 0t0 TCP *:6379 (LISTEN)

kill -SIGTERM 1573

切换到redis的安装路径,启动redis-server

./src/redis-server ./redis.conf

看到这一行说明成功:

[2114] 19 Jul 20:53:34.509 * The server is now ready to accept connections on port 6379

 

* 连接redis数据库

./src/redis-cli -h 127.0.0.1 -a shi_kuretto

  测试连接

localhost:6379> ping
PONG
localhost:6379>

  

* php redis配置

  参照这里 

* 下载依赖包

composer require predis/predis
composer update

 

* 修改laravel 配置参数

  .env

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=shi_kuretto
REDIS_PORT=6379

 config/database.php

<?php

return [

   /* ... */

    'redis' => [

        'client' => 'predis',

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

    ],

];

 

* 重启web服务

php artisan cache:clear
php artisan config:clear
php artisan serve --port 9000

  

  
* 创建测试Controller

php artisan make:controller WebController

  

WebController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redis;
// use Redis;

class WebController extends Controller {
    public function index() {
        Redis::set('string:user:name', 'wshuo');
        return Redis::get('string:user:name');

    }
    public function test() {
        $env = config('database.redis.default');
        $redis = new Redis();
        $redis->connect($env['host'], $env['port'], 5);
        $redis->auth($env['password']);
        $redis->set('string:user:name', 'wshuo');
        return $redis->get('string:user:name');
    }
}

  

* 配置路由 ./routes/web.php +1条路由

<?php
Route::get('/', function () {
    return view('welcome');
});
Route::get('/Web/index', 'WebController@index');

  

* 启动laravel项目

php -S 0.0.0.0:9000

  

* 浏览器打开 http://localhost:9000/Web/index

  wshuo

 

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

可以查看这几个文件代码

./vendor/laravel/framework/src/Illuminate/Support/Facades/Redis.php

./vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php

./config/app.php

'Redis' => Illuminate\Support\Facades\Redis::class,

  

vendor/laravel/framework/src/Illuminate/Redis/RedisServiceProvider.php

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('redis', function ($app) {
            $config = $app->make('config')->get('database.redis');

            return new RedisManager(Arr::pull($config, 'client', 'predis'), $config);
        });

        $this->app->bind('redis.connection', function ($app) {
            return $app['redis']->connection();
        });
    }

  vendor/laravel/framework/src/Illuminate/Redis/RedisManager.php

    /**
     * Create a new Redis manager instance.
     *
     * @param  string  $driver
     * @param  array  $config
     * @return void
     */
    public function __construct($driver, array $config)
    {
        $this->driver = $driver;
        $this->config = $config;
    }

  

PHP扩展开发:

http://www.cnblogs.com/52fhy/category/604746.html