* 參考資料php
redis文檔 http://www.redis.cn/documentation.html, http://redisdoc.com/index.htmlhtml
redis桌面客戶端 https://redisdesktop.com/downloadlaravel
* 修改redis.confweb
添加一行redis
requirepass "shi_kuretto"
去掉一行註釋數據庫
bind 127.0.0.1 ::1
* 重啓redis服務瀏覽器
lsof -i:6379
找到redis的進程id, killbash
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)app
kill -SIGTERM 1573
切換到redis的安裝路徑,啓動redis-servercomposer
./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