laravel redis 订阅 发布 (sub/pub), Illuminate\Support\Facades\Redis::command用法

* 设置装备摆设redis扩大​

用php -S 0.0.0.0:8090 -t public 这类web效劳, redis衔接失利。我用的xampp, extra/httpd-vhost.conf设置装备摆设

* composer装置redis衔接的组件

composer require predis/predis

检查composer.json, predis装置好了

 "predis/predis": "^1.1"

* 启动redis-server

D:\bin\redis-server D:\bin\redis.windows.conf

* 设置装备摆设laravel框架根目次的情况变量 .env

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=myredispassword
REDIS_PORT=6379

* config/database.php

    'redis' => [

'client' => 'predis',

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

],

* config\cache.php

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

以上的设置装备摆设参考 ​​Redis - Laravel - The PHP Framework For Web Artisans​​

* app\Http\Controllers\UserController.php

  公布音讯 触发事情 (这个该当放正在注册handler前面的)

<?php
namespace App\Http\Controllers;

use App\Http\Controllers;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;

class UserController extends Controller {
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function showProfile($id) {
$user = Redis::get("user:profile:".$id);
return $user;
}

public function getProfile(Request $request) {
$user = Redis::get("user:profile:".$request->get("id"));
return $user;
}

public function publish() {
$msg = \json_encode(['foo' => 'bar']);
return [
'code' => Redis::publish('test-channel', $msg),
'msg' => $msg
];
}

public function test() {
Redis::pipeline(function($pipe) {
for ($i = 0; $i < 100; $i++) {
$pipe->set("key:$i", $i);
}
});
}
}

* app\Console\Co妹妹ands\RedisSubscribe.php

  设定音讯处置listener函数

<?php
namespace App\Console\Co妹妹ands;

use Illuminate\Console\Co妹妹and;
use Illuminate\Support\Facades\Redis;

class RedisSubscribe extends Co妹妹and {
/**
* The name and signature of the console co妹妹and.
*
* @var string
*/
protected $signature = 'redis:subscribe';

/**
* The console co妹妹and description.
*
* @var string
*/
protected $description = "Subscribe to a Redis channel";

/**
* Execute the console co妹妹and.
*
* @return mixed
*/
public function handle() {
Redis::subscribe(['test-channel'], function($message) {
echo $message;
});
}

}

* app\Console\Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan co妹妹ands provided by your application.
*
* @var array
*/
protected $co妹妹ands = [
\App\Console\Co妹妹ands\RedisSubscribe::class,

];

/**
* Define the application's co妹妹and schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule) {
// $schedule->co妹妹and('inspire')->hourly();
$schedule->call(function() {
// ...
})->everyMinute();
}

/**
* Register the co妹妹ands for the application.
*
* @return void
*/
protected function co妹妹ands() {
$this->load(__DIR__.'/Co妹妹ands');

require base_path('routes/console.php');
}
}

* 设置装备摆设道由

 routes\web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

// Route::get('/user/profile/{$id}', "UserController@showProfile");
Route::get('/user/profile', "UserController@getProfile");
Route::get('/user/publish', "UserController@publish");
Route::get('/user/test', "UserController@test");

* 测试

** 从终端定阅音讯

php artisan redis:subscribe -v

** 从阅读器公布音讯

​​http://127.0.0.1:8090/user/publish​​

** 后果

阅读器表现

{"code":1,"msg":"{\"foo\":\"bar\"}"}

终端

E:\code\php\blog>php artisan redis:subscribe -v

{"foo":"bar"}{"foo":"bar"}

阅读器每一革新一次 终端打印一次

* Wildcard Subscriptions  通配符定阅  批量定阅

app\Console\Co妹妹ands\RedisSubscribe.php

修正handle办法

    public function handle()
{
/*
Redis::subscribe(['test-channel'], function ($message) {
echo $message;
});
*/
Redis::psubscribe(['test-*'], function($msg) {
echo 'handling Wildcard Subscriptions: '.$msg;
});
}

> php artisan redis:subscribe

阅读器再拜访 ​​http://127.0.0.1:8090/user/publish​​

终端输入:

handling Wildcard Subscriptions: {"foo":"bar"}​

laravel redis运用

=======================================================

没有晓得框架封装了哪些redis操纵办法每一次查手册费事

实在框架供给了 Redis::co妹妹and办法,就像正在redis-cli饬令利用用同样

use Illuminate\Support\Facades\Redis;

        // 从redis中掏出一切这个地区司理下的一切代办署理用户名
$REDIS_KEY = "PROXY:".$eigid;
$a = Redis::co妹妹and('lrange', [$REDIS_KEY, 0, -1]);
if ($a) {
$ret->list = $a;
return [
'code' => 0,
'data' => $ret,
'msg' =>'success'
];
}

// ...
// 缓存查进去的代办署理用户名
foreach ($ret->list as $name) {
Redis::co妹妹and('rpush', [$REDIS_KEY, $name]);
}
Redis::co妹妹and('expire', [$REDIS_KEY, 86400]);

您可以还会对下面的文章感兴趣:

最新评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。